<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class InfosController extends GameController {
	/**
	 * @return
	 */
	function Index(Req $req) {
        $request['game']=$req->game ? $req->game : 'jspk10';
        $request['no']=$req->no ? $req->no : '';
        $request['star_time']=$req->star_time ? $req->star_time : date('Y-m-d');
        $request['end_time']=$req->end_time ? $req->end_time : date('Y-m-d');
        //$request['status'] = Request::has('status') ? Request::get('status') : 1;
		$this->getGame();
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/Infos/getMsg?game='.$request['game']);
		$dt->setLang('game_prize');
		$dt->addColsFields('name');
		$dt->addColsFields('info_no');
		$dt->addColsFields('codes');
		$dt->addColsFields('open_time');
		if (checkRriv('/admin/Infos/Edit')) {
			$dt->addColsFields('edit',array('sort'=>false,'templet'=>'#edit'));
			//$dt->setToolBar(array('edit'));
		}
		return view('admin.infos/openMoney', $dt->render($request));
	}
	//获取开奖数据信息
	function getMsg(Req $req) {
		$game = $req->game ? $req->game : 'jspk10';
		$no = $req->no ? $req->no : '';
        $star_time=$req->star_time ? $req->star_time.' 00:00:00' : date('Y-m-d 00:00:00');
        $end_time=$req->end_time ? $req->end_time.' 23:59:59' :  date('Y-m-d 23:59:59');
		$list = Request::has('limit') ? Request::get('limit') : '';
		$page = Request::has('page') ? Request::get('page') : '1';
        $fild = Request::has('fild') ? Request::get('fild') : 'open_time';
        //$status = Request::get('status') ? Request::get('status') : 1;
        $order = Request::has('order') ? Request::get('order') : 'desc';
		if (empty($game)) {
			return responseToJson(-5030025022);
		}
		$where=array();
		/*if(!empty($game)){
		    $where[]=array('game_name','=',$game);
        }*/
        if(!empty($no)){
            $where[]=array('info_no',$no);
        }
        if(!empty($star_time)){
            $where[]=array('open_time','>=',$star_time);
        }
        if(!empty($end_time)){
            $where[]=array('open_time','<=',$end_time);
        }
       // $where[] = ($status==1)?array('status','<>',-1):array('status','-1');
		$db = '\App\Models\GamePrize';
		$data = $db::getMsgs($where,$game, $list,$fild,$order);
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);

	}
	/**获取游戏名**/
	function getGame() {
		$db = new \App\Models\GamePrize;
		$info = $db->getgame();
		return responseToJson($info);
	}
	//修改
	function Edit(Req $req) {
		$id = $req->id;
		if (empty($id)) {
			abort(404);
		}
		$status = $req->status;
		$status = empty($status)?2:$status;
		$game = $req->game;
		$db = '\App\Models\Game' . ucfirst($game);
		$data = $db::where('id',$id)->first();
        if(!$data){
        	return responseToJson(-5030002922);//没有数据
        }
        $datas = $data->toArray();
        $datas['game_name'] = $game;
		if (!$req->isMethod('post')) {
			return view('admin/infos/edit', ['data' => $datas]);
		} else {
			if (empty($req->info_no) || empty($req->codes) || empty($req->open_time)) {
				return responseToJson(-5030012022);
			}
			$data = array(
				'info_no' => $req->info_no,
				'codes' => $req->codes,
				'open_time' => $req->open_time,
//                'time_area'=>date('Y-m-d H:i:s',time()),
			);
			$res = $db::where('id',$id)->where('status','<>',2)->update($data);
	        if(!$res){
	        	return responseToJson(-5030003022);//修改失败
	        }
			$log = array(
				session('adminInfo.admin_name'),
				trans('common.' . $datas['game_name']),
				$req->info_no,
				$req->codes,
				$req->open_time,
			);
			OperationLog(session('adminInfo.admin_id'), 'edit_game_prize', $log);
			return responseToJson($res);
		}
	}
	//添加
	function add(Req $req) {
		$game = $req->game;
		if (empty($game)) {
			abort(404);
		}
		$model = "\App\Models\Game" . ucfirst($game);
		$datas = ['game_name' => $game];
		if (!$req->isMethod('post')) {
		    //当前游戏下一期期号
            $info_nos = $model::select('info_no')->orderBy('info_no','desc')->first();
	        if(!$info_nos){
	            $info_no = rand(1000, 2000);
	        }else{
            	$info_no = $info_nos['info_no']+1;
            }
            $datas['info_no']=$info_no;
			return view('admin/infos/editinfo', ['data' => $datas,]);
		} else {
			if (empty($req->codes) || empty($req->open_time)) {
				return responseToJson(-5030012022);
			}
			$time = strtotime($req->open_time) ? strtotime($req->open_time) : '';
			if (empty($time)) {
				return responseToJson(-5030014022); //请输入正确的时间格式
			}
			//获取期号
            $info_no = $req->info_no;
			if (empty($info_no) || !is_numeric($info_no)) {
				return responseToJson(-5030013022); //请填写正确的期号
			}
			$data = array(
				'identity' => UUID(),
				'info_identity' => UUID(),
				'info_no' => $info_no,
				'codes' => $req->codes,
				'open_time' => $req->open_time,
                'time'=>date('Y-m-d H:i:s',time()),
			);
			$res = $this->insetMsg($data,$game);
			$log = array(
				session('adminInfo.admin_name'),
				trans('common.' . $datas['game_name']),
				$req->info_no,
				$req->codes,
				$req->open_time,
			);
			OperationLog(session('adminInfo.admin_id'), 'add_game_prize', $log);
			return responseToJson($res);

		}
	}

	//插入数据
    function insetMsg($data,$game){
    	$model = "\App\Models\Game" . ucfirst($game);
    	$info = $model::where('info_no',$data['info_no'])->first();
        if(!$info){
            $res = $model::insert($data);
        }else{
            return -5030114122;	//当期游戏已存在
        }

        if(!$res){
            return -5030003122;//添加失败
        }
        return 1;
    }

    //获取游戏最新期号
	function getInfos(Req $req){
        $game = $req->game;
        if (empty($game)) {
            return responseToJson(-5030012022);
        }
        $db = new \App\Models\GamePrize;
        $info_no = $db->getInfoNo($game);
        if ($info_no < 0) {
            $info_no = rand(1000, 2000);
        }
        return responseToJson(1,'',$info_no);

    }

    //最后一期开奖时间
    public function getGameLastOpenTime(Req $req)
    {
        $game = $req->game;
        if (empty($game)) {
            return responseToJson(-5030012022);
        }
        $data = $this->getGameTime($game);
        $model = '\App\Models\Game'.ucfirst($data['table_name']);
        $db = new $model;
        $game_info = $db->getOne($game);
        if ($game_info < 0) {
            return responseToJson($game_info,'获取最后期数失败','');
        }else{
            $data['last_time'] = $game_info['open_time']; //最后一期已开奖开奖时间
            $time = strtotime($game_info['open_time'])+$data['cycle_time']+10; //时间处理最后一期已开奖开奖时间+当前游戏开奖时间周期+10秒
            $data['change_time'] = date('Y-m-d H:i:s',$time);
        }
        return responseToJson(1,'获取成功',$data);
    }

    //获取游戏周期秒数
    public function getGameTime($game)
    {
        $db = new \App\Models\GameType;
        return $db->getOpenTime($game);
    }
}
