<?php
// +----------------------------------------------------------------------
// | WANCMS游戏平台管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://www.wancms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 顽皮蛋 <shf_l@163.com> <http://www.wancms.com>
// +----------------------------------------------------------------------
// | 修改时间: 2015/12/15
// +----------------------------------------------------------------------

namespace Phonegame\Controller;
use Common\Controller\AdminbaseController;
/**
 * 区服管理控制器
 * @author 顽皮蛋 <shf_l@163.com>
 */
class ServeradminController extends AdminbaseController{
	/**
     * 区服管理
     * @author 顽皮蛋 <shf_l@163.com>
     */
	public function index(){
		$gid = I('gid');
        $status = I('status');
        $isstop = I('isstop');
        $servername = I('servername');
        $map['servername']    =   array('like', '%'.(string)$servername.'%');
        if($gid)$map['gid'] = $gid;
        if($status||$status=='0')$map['status'] = $status;
        if($isstop||$isstop=='0')$map['isstop'] = $isstop;
        $count=M("m_server")->where($map)->count();
        $page       = new \Think\Page($count,20);//实例化分页类 传入总记录数和每页显示的记录数
        /*条件*/
        $parameter['servername'] = $servername;
        $parameter['gid'] = $gid;
        $parameter['status'] = $status;
        $parameter['isstop'] = $isstop;
        foreach($parameter as $key=>$val){
            $page->parameter[$key]   =   urlencode($val);
        }
        $show  = $page->show();// 分页显示输出
        $lists = M("m_server")
        ->where($map)
        ->order("add_time DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
        $this->assign('lists', $lists);
        $this->assign("page", $show);
        $this->assign('gid', $gid);
        $this->assign('status1', $status);
        $this->assign('isstop1', $isstop);
        $this->assign('servername', $servername);
        /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
        $this->assign('g_list', $g_list);
        $this->display();
	}
	/**
     * 增加区服
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function add(){
        if(IS_POST){
           $_POST['start_time'] = strtotime($_POST['start_time']);
           $_POST['zsid'] =  I('sid');
            if (D("Common/MServer")->create()){
                if (D("Common/MServer")->add()!==false) {
                    $this->success("添加成功！", U("Serveradmin/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error(D("Common/MServer")->getError());
            }
        }else{
        $this->assign('info', '');
        /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
        $this->assign('g_list', $g_list);  
        $this->display();
        }
    }
    /* ajax获取游戏区服 */
    public function getSid($gid) {
        $server = M('m_server' );
        $mar ['gid'] = $gid;
        $sid= $server->where($mar)->order('sid desc')->getField('sid');
        if(empty($sid))$sid = 0;
        $this->ajaxReturn($sid+1);
    }
    /**
     * 修改区服
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function edit(){
        if(IS_POST){
            $_POST['start_time'] = strtotime($_POST['start_time']);
            /*sid在充值记录表、新手卡、内充中若有记录不能修改*/
            $id=  intval(I("post.id"));
            $sid = M('m_server')->where(array("id"=>$id))->getField('sid');
            if($sid !=I("post.sid")){
                /*由于前台控制不可编辑其实可以去掉判断*/
                $pay = M('pay_ok')->where(array('sid'=>$sid))->find();
                $card =M('m_card')->where(array('sid'=>$sid))->find();
                $game_log =M('game_log')->where(array('sid'=>$sid))->find();
                $charge =M('charge')->where(array('sid'=>$sid))->find();
                if($pay||$card||$charge||$game_log)$this->error("保存失败！区服（{$sid}）内容不能修改");  
                /*如果该区服未*/
                $_POST['zsid'] =  I('sid');
            }
            if (D("Common/MServer")->create()) {
                if (D("Common/MServer")->save()!==false) {
                    $this->success("保存成功！", U("Serveradmin/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error(D("Common/MServer")->getError());
            }
        }else{
        $id=  intval(I("get.id"));
        $info = M('m_server')->where(array("id"=>$id))->find(); 
        $this->assign('info', $info);
        /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
        $this->assign('g_list', $g_list);  
        $this->display('add');
        }    	
    }
    /**
     * 删除区服
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function del(){
        if(isset($_POST['ids'])){
            $ids = implode(",", $_POST['ids']);
            /*判断该区服下没有充值、登陆记录*/
            if (M('m_server')->where("id in ($ids)")->delete()!==false) {
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        }else{
            $id = intval(I("get.id"));
            /*判断该区服下没有充值、登陆记录*/
            if (M('m_server')->delete($id)!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }    	
    }
    /**
     * 开启、关闭
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function toggle(){
        if(isset($_POST['ids']) && $_GET["display"]){
            $ids = implode(",", $_POST['ids']);
            $data['isstop']=1;
            if (M('m_server')->where("id in ($ids)")->save($data)!==false) {
                $this->success("开启成功！");
            } else {
                $this->error("开启失败！");
            }
        }
        if(isset($_POST['ids']) && $_GET["hide"]){
            $ids = implode(",", $_POST['ids']);
            $data['isstop']=0;
            if (M('m_server')->where("id in ($ids)")->save($data)!==false) {
                $this->success("停服成功！");
            } else {
                $this->error("停服失败！");
            }
        }

    }
    /**
     * 推荐、不推荐
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function hot(){
        if(isset($_POST['ids']) && $_GET["display"]){
            $ids = implode(",", $_POST['ids']);
            $data['status']=1;
            if (M('m_server')->where("id in ($ids)")->save($data)!==false) {
                $this->success("开启成功！");
            } else {
                $this->error("开启失败！");
            }
        }
        if(isset($_POST['ids']) && $_GET["hide"]){
            $ids = implode(",", $_POST['ids']);
            $data['status']=0;
            if (M('m_server')->where("id in ($ids)")->save($data)!==false) {
                $this->success("关闭成功！");
            } else {
                $this->error("关闭失败！");
            }
        }

    }
    /**
     * 合区
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function hequ(){
        /*获取主区服和子区服*/
        if(IS_POST){
            $gid = I('gid');
            $sid = I('sid');
            $name = I('name');
            $status = I('status');
            if($status =='1'){
            $arr = explode('-', $name);
            if(empty($sid))$this->error("参数错误！");
            if($sid!=$arr['0'])$this->error("主区服需要和开始区服相同");
            if(empty($arr['0'])||$arr['0']>=$arr['1']||empty($arr['1']))$this->error("参数错误！");
            $map['gid'] = $gid;
            $map['sid'] = array('between',array($arr['0'],$arr['1']));
            M('m_server')->where($map)->save(array('zsid'=>$sid));  
            }else{
            if(!is_numeric($name))$this->error("参数错误！");   
            $map['gid'] = $gid;
            $map['sid'] = $name;
             M('m_server')->where($map)->save(array('zsid'=>$sid));    
            }
            $this->success("修改成功！");
        }else{
            /*所有游戏*/
            $g_list = M('m_game')->getField('id,gamename,listorder');
            $this->assign('g_list', $g_list);  
            $this->display();  
        }
    }
}