<?php
/**
 * Created by PhpStorm.
 * User: xmy 280564871@qq.com
 * Date: 2017/4/24
 * Time: 8:59
 */

namespace Open\Controller;

use Open\Model\GameModel;
use Open\Model\GameSetModel;
use Open\Model\GameSourceModel;

class GameController extends CenterController{

	public function _initialize()
	{

		$this->p_title = "管理中心";
		$this->p_url = U('Stats/overview');
		parent::_initialize(); // TODO: Change the autogenerated stub
	}

	public function index($p=1)
	{
		$this->title = "游戏管理";
		$model = new GameModel();
		$map['developers'] = UID;
		empty(I("game_name")) || $map['g.game_name'] = ['like','%'.I("game_name").'%'];
		empty(I("game_type_id")) || $map['g.game_type_id'] = I("game_type_id");
		empty(I("sdk_version")) || $map['g.sdk_version'] = I("sdk_version");
		I("apply_status") == "" || $map['g.apply_status'] = I("apply_status");
		$result = $model->getDataLists($map,$p);
		$this->showPage($result['count']);
		$this->assign("data",$result['data']);
		$this->display();
	}

	/**
	 * 添加游戏
	 * author: xmy 280564871@qq.com
	 */
	public function add(){
		$this->title = "创建游戏";
		$model = new GameModel();
		if(IS_POST){
			$result = $model->upDate(UID);
			if($result !== false){
				$this->success("保存成功",U('index'));
			}else{
				$this->error("保存失败:".$model->getError());
			}
		}else{
			$this->display();
		}
	}

	/**
	 * 编辑游戏
	 * @param $id
	 * author: xmy 280564871@qq.com
	 */
	public function edit($id){
		$this->title = "编辑游戏";
		$model = new GameModel();
		if(IS_POST){
			$result = $model->upDate(UID,$id);
			if($result !== false){
				$this->success("保存成功",U('index'));
			}else{
				$this->error("保存失败:".$model->getError());
			}
		}else{
			$data = $model->getUserData(UID,$id);
			if(empty($data)){
				$this->error("游戏不存在");
			}
			$this->assign("data",$data);
			$this->display();
		}
	}

	/**
	 * 原包上传
	 * @param $game_id
	 * author: xmy 280564871@qq.com
	 */
	public function source($game_id){
		$this->title = "原包上传";
		$model = new GameSourceModel();
		if(IS_POST){
			$result = $model->upDate(UID,$game_id);
			if($result !== false){
				$this->success("保存成功",U('index'));
			}else{
				$this->error("保存失败:".$model->getError());
			}
		}else{
			$data = $model->getUserData(UID,$game_id);
			$game = D("Game")->getUserData(UID,$game_id);
			
			$remark = implode('@@@',json_decode($data['remark']));	
			
			$data['remark'] = str_replace("@@@","\r\n",$remark);			
			
			$this->assign("game",$game);
			$this->assign("data",$data);
			$this->display();
		}
	}


	/**
	 * 素材包上传
	 * @param $game_id
	 * author: xmy 280564871@qq.com
	 */
	public function material($game_id){
		$this->title = "素材包上传";
		$model = new GameModel();
		if(IS_POST){
			$result = $model->addMaterial(UID,$game_id);
			if($result !== false){
				$this->success("保存成功",U('index'));
			}else{
				$this->error("保存失败:".$model->getError());
			}
		}else{
			$data = $model->getUserData(UID,$game_id);
			if(empty($data)){
				$this->error("游戏不存在");
			}
			$this->assign("data",$data);
			$this->display();
		}
	}

	/**
	 * 获取游戏参数
	 * @param $game_id
	 * author: xmy 280564871@qq.com
	 */
	public function get_game_param($game_id){
		$model = new GameSetModel();
		$data = $model->getGameParam($game_id);
		$this->assign("data",$data);
		$this->display("game_param");
	}

	/**
	 * 更改游戏显示状态
	 * @param $game_id
	 * @param $status
	 * author: xmy 280564871@qq.com
	 */
	public function set_game_status($game_id){
		$model = new GameModel();
		$result = $model->setStatus(UID,$game_id);
		if($result !== false){
			$this->success("操作成功",U('index'));
		}else{
			$this->error("操作失败:".$model->getError());
		}
	}

	/**
	 * 数据图表
	 * @param string $game_id
	 * @param int $p
	 * author: xmy 280564871@qq.com
	 */
	public function data_table($game_id="",$p=1){
		$this->title = "游戏管理";
		//游戏列表
		$Game = new GameModel();
		empty($game_id) || $game_map['g.id'] = $game_id;
		$game_map['developers'] = UID;
		$game_map['online_status'] = 1;
		$game_data = $Game->getDataLists($game_map,$p);
		$this->showPage($game_data['count']);
		$game_list = $game_data['data'];
		//遍历游戏查询每个游戏的数据
		foreach ($game_list as $key=>$value){
			$game_list[$key]['data'] = $Game->data_table($value['id']);
		}
		$this->assign("data",$game_list);
		$this->assign('uid',UID);
		$this->display();
	}
	
	/**
	 * 删除游戏
	 * @author 鹿文学
	 */
	public function del() {
		$ids = array_unique((array)I('request.ids',null));
		if ( empty($ids) ) {
				$this->error('请选择要操作的数据!');
		}
		
		
		
		
		
	}
}