<?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 CardadminController extends AdminbaseController{
	/**
     * 卡类管理
     * @author 顽皮蛋 <shf_l@163.com>
     *TODO 所属专员查找
     */
	public function index(){
        $card_type_id = I('card_type_id');
        $gid = I('gid');
        $vip = I('vip');
        $name = I('name');
        $map['name']    =   array('like', '%'.(string)$name.'%');
        if($card_type_id)$map['card_type_id'] = $card_type_id;
        if($gid)$map['gid'] = $gid;
        if($vip|$vip=='0')$map['vip'] = $vip;
        $count=M("m_card")->where($map)->count();
        $page       = new \Think\Page($count,20);//实例化分页类 传入总记录数和每页显示的记录数
        
		/*条件*/
        $parameter['name'] = $name;
        $parameter['card_type_id'] = $card_type_id;
        $parameter['gid'] = $gid;
        $parameter['vip'] = $vip;
        foreach($parameter as $key=>$val){
            $page->parameter[$key]   =   urlencode($val);
        }
        $show  = $page->show();// 分页显示输出
        $lists = M("m_card")
        ->where($map)
        ->order("id DESC")
        ->limit($page->firstRow . ',' . $page->listRows)
        ->select();
		 /**
         *修改时间2016/01/19
         *@author 郑建斌 
         */

        $this->assign('lists', $lists);
        $this->assign("page", $show);
        $this->assign('card_type_id', $card_type_id);
        $this->assign('gid', $gid);
        $this->assign('vip', $vip);
        $this->assign('name', $name);
      
	    /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
        /**
         *修改时间2016/01/06
         *@author yu <thebestyu@126.com>
         */
        $result = M('m_card_type');
        $card =$result->where('tid=1')->getField('id,cardname,tid');
        $this->assign('card',$card);
        $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']);
            if($_POST['cps_uid']){
                $_POST['cps_status']=1;
            }
            if (D("Common/MCard")->create()){
                if (D("Common/MCard")->add()!==false) {
                    $this->success("添加成功！", U("Cardadmin/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error(D("Common/MCard")->getError());
            }
        }else{
        $this->assign('info', '');
        /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
 
        /**
         *修改时间2016/01/06
         *@author yu <thebestyu@126.com>
         */
        $card = M('m_card_type')->where('tid=1')->select();
        $this->assign('card',$card);
        $this->assign('$cardtypes',$cardtypes);
        $this->assign('g_list', $g_list);  
        $this->display();
        }
    }
    /**
     * 修改卡类
     * @author 顽皮蛋 <shf_l@163.com>
     */
    public function edit(){
        if(IS_POST){
            $_POST['start_time'] = strtotime($_POST['start_time']);  
             if($_POST['cps_uid']){
                $_POST['cps_status']=1;
            }
            if (D("Common/MCard")->create()) {
                if (D("Common/MCard")->save()!==false) {
                    $this->success("保存成功！", U("Cardadmin/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error(D("Common/MCard")->getError());
            }
        }else{
        $id=  intval(I("get.id"));
        $info = M('m_card')->where(array("id"=>$id))->find();
        $info['card_context'] = htmlspecialchars_decode($info['card_context']);
        $this->assign('info', $info);
        /*所有游戏*/
        $g_list = M('m_game')->getField('id,gamename,listorder');
        /**
         *修改时间2016/01/06
         *@author yu <thebestyu@126.com>
         */
        $card = M('m_card_type')->where('tid=1')->select();
        $this->assign('card',$card);
        $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_card')->where("id in ($ids)")->delete()!==false) {
                $this->success("删除成功！");
            }else{
                $this->error("删除失败！");
            }
        }else{
            $id = intval(I("get.id"));
            if (M('m_card')->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['status']=1;
            if (M('m_card')->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_card')->where("id in ($ids)")->save($data)!==false) {
                $this->success("关闭成功！");
            } else {
                $this->error("关闭失败！");
            }
        }

    }
}