<?php
/**
 * 游戏未结算注单
 */

namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Illuminate\Support\Facades\DB;
use Request;

class UnsetController extends Controller {
	public $point = [
        '1001'=>'pc投注',
        '1002'=>'wap投注',
        '1003'=>'长龙投注'
    ];
	/**
	 * 游戏开关
	 */
	function Index() {
//		$game = Request::has('game') ? Request::get('game') : '';
//		$data = Request::has('data') ? Request::get('data') : '';
//		$no = Request::has('no') ? Request::get('no') : '';
//		$name = Request::has('name') ? Request::get('name') : '';

        $request=array();
        $request['game'] = Request::has('game') ? Request::get('game') : 'xy28';
        $request['account_name'] = Request::has('account_name') ? Request::get('account_name') : '';
        $request['order_id'] = Request::has('order_id') ? Request::get('order_id') : '';
//        $request['game'] = Request::get('name') ? Request::get('name') : $name;
        $request['game_no'] = Request::has('game_no') ? Request::get('game_no') : '';
        $request['bet_small'] = Request::has('bet_small') ? Request::get('bet_small') : '';
        $request['bet_big'] = Request::has('bet_big') ? Request::get('bet_big') : '';
        $request['winfail_small'] = Request::has('winfail_small') ? Request::get('winfail_small') : '';
        $request['winfail_big'] = Request::has('winfail_big') ? Request::get('winfail_big') : '';
        $request['money_big'] = Request::has('money_big') ? Request::get('money_big') : '';
        $request['money_small'] = Request::has('money_small') ? Request::get('money_small') : '';
        $request['star_time'] = Request::has('star_time') ?Request::get('star_time'): trans('status.default_time.seven_day');
        $request['end_time'] = Request::has('end_time') ? Request::get('end_time') : trans('status.default_time.etime');
        $request['status'] = Request::has('status') ? Request::get('status') : '';
        $request['game_point'] = Request::has('game_point') ? Request::get('game_point') : '';
        $request['gameList']=\App\Models\GameType::getGameType();
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->enableCheckBox();
		$dt->setDataSource('/admin/Unset/dataSource?game=' .  $request['game'] . '&account_name=' .  $request['account_name'] . '&game_no=' . $request['game_no'] . '&game_status=0');
		$dt->setLang('bet_order');
		$dt->addColsFields('name', array('sort' => false, 'width' => 130));
		$dt->addColsFields('game_no', array('width' => 120, 'templet' => '#game_no'));
		$dt->addColsFields('money_time', array('sort' => true, 'width' => 235));
//		$dt->addColsFields('prizes_code', array('width' => 220));
		$dt->addColsFields('money', array('width' => 120,'templet' => '#moneynew'));
		$dt->addColsFields('get_money', array('width' => 120, 'templet' => '#money'));
		$dt->addColsFields('winmoney', array('sort' => false,'width' => 120));
//		$dt->addColsFields('money_time', array('width' => 160));
		$dt->addColsFields('account_getw', array('sort' => false, 'templet' => '#account', 'width' => 145));
		$dt->addColsFields('game_status', array('templet' => '#acc', 'sort' => false, 'width' => 100));
		$dt->addColsFields('bet_', array('sort' => false));
		//加在表头
		//		$table = new \App\Lib\Biz\Game\Table\GameTable;
		//		$table->gameTable($dt, $game);
		//$dt->setToolBar(array('view'), array('width' => 100,'align' => 'left')); 
		$dt->addColsFields('tool',array('width'=>110,'templet'=>'#tool','sort' => false));
		if (!empty($data)) {
			return responseToJson($dt->render());
		}
		return view('admin.unset/index', $dt->render($request));
	}

	function dataSource(Req $req) {
		$game = Request::has('game') ? Request::get('game') : '';
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : '';
		$field = !empty(Request::get('field')) ? Request::get('field') : 'money_time';
		$order = !empty(Request::get('order')) ? Request::get('order') : 'desc';

		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$order_id = Request::has('order_id') ? Request::get('order_id') : '';
//		$game = Request::has('name') ? Request::get('name') : '';
		$game_no = Request::has('game_no') ? Request::get('game_no') : '';
		$bet_small = Request::has('bet_small') ? Request::get('bet_small') : '';
		$bet_big = Request::has('bet_big') ? Request::get('bet_big') : '';
		$winfail_small = Request::has('winfail_small') ? Request::get('winfail_small') : '';
		$winfail_big = Request::has('winfail_big') ? Request::get('winfail_big') : '';
		$money_big = Request::has('money_big') ? Request::get('money_big') : '';
		$money_small = Request::has('money_small') ? Request::get('money_small') : '';
		$game_status = !empty(Request::get('game_status')) ? Request::get('game_status') : 0;
		$star_time = Request::get('star_time') ? Request::get('star_time').' 00:00:00' : '';
		$end_time = Request::get('end_time') ? Request::get('end_time').' 23:59:59' : '';
		$status = !empty(Request::get('status')) ? Request::get('status') : '';
		$game_point = Request::get('game_point') ? Request::get('game_point') : '';
		$where = array();
		$where[] = array('status', '<>', 4);
        //排除六合彩
        $where[]=array('game_name','<>','sixlottery');
//		if (!empty($name)) {
//			$game = $name;
//			$where[] = array('game_name', '=', $name);
//		}
        //权限检测
        if(!checkRriv('/bet/game/'.$game)){
            return \App\Lib\DataTable\DataTable::init()->toJson(array(), 1, 0, $where);
        }
		if (!empty($game)) {
			$where[] = array('game_name', '=', $game);
		}
		if (!empty($bet_small)) {
			$where[] = array('money', '>=', $bet_small);
		}
		if (!empty($bet_big)) {
			$where[] = array('money', '<=', $bet_big);
		}
		if (!empty($winfail_small)) {
			$where[] = array('get_money', '>=', $winfail_small);
		}
		if (!empty($winfail_big)) {
			$where[] = array('get_money', '<=', $winfail_big);
		}
		if (!empty($money_small)) {
			$where[] = array('prize_money', '>=', $money_small);
		}
		if (!empty($money_big)) {
			$where[] = array('prize_money', '<=', $money_big);
		}
		if (is_numeric($game_status)) {
			$where[] = array('game_status', '=', $game_status);
		}
		if (!empty($star_time)) {
			$star_time = date('Y-m-d H:i:s', strtotime($star_time));
			$where[] = array('money_time', '>=', $star_time);
		}
		if (!empty($end_time)) {
			$end_time = date('Y-m-d H:i:s', strtotime($end_time));
			$where[] = array('money_time', '<=', $end_time);
		}
		if (!empty($status)) {
			$where[] = array('status', '=', $status);
		}

		if (!empty($order_id)) {
			$where = array(array('order_id', '=', $order_id));
		}

		if (!empty($game_no)) {
			$where = array(array('game_no', '=', $game_no));
		}

		if (!empty($account_name)) {
			$where[] = array('account_name', 'like', '%' . $account_name . '%');
		}
		if(!empty($game_point)){
            $where[] = array('remark', 'like', '%|'.$game_point.'|%');
        }

//        $where[] = array('status', '!=', 4);
		$db = new \App\Models\MoneyBuy();
		$data = $db->getBuy($where, $list, $page, $field, $order);
		$gameParse = new \App\Lib\Biz\Game\Parse\GameParse;
//        $total = array('total_bet_money' => 0, 'win_money' => 0);
		foreach ($data['data'] as $k => $v) {
			$data['data'][$k] = $gameParse->packbet($v, $v['game_name']); //开奖号码解析
			$data['data'][$k]['winmoney'] = $gameParse->winMomey($v['codes']); //可盈金额
			$data['data'][$k]['money_time'] = $v['order_id'] . '<br>' . $v['money_time'];
			$data['data'][$k]['game_no_prize'] = $v['game_no'] . '<br/>';
            if(isset($data['data'][$k]['prizes_code'])){
                $data['data'][$k]['game_no_prize'].=$data['data'][$k]['prizes_code'];
            }
//            $total['total_bet_money'] += $v['money'];
			//            $total['win_money'] += $v['get_money'];
		}
		$data['data'] = arrayToNumber($data['data'], ['money', 'get_money', 'winmoney']);
		$long = $this->getLongtype();
		foreach ($data['data'] as $k => $v) {
			if(!empty($v['remark'])){
				$remark = explode('|', $v['remark']);
				if(isset($remark[3]) && isset($long[$remark[3]])){
                    $data['data'][$k]['remark'] = $long[$remark[3]];
                }else if(isset($remark[1]) && !empty($remark[1])){
                    $data['data'][$k]['remark'] = $this->point[$remark[1]];
                }else{
                    $data['data'][$k]['remark'] = '';
                }
			}else{
				$data['data'][$k]['remark'] = '';
			}
		}
		return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total'], 0, $where);
//        return responseToJson($data);
	}

    //获取投注来源
    private function getLongtype(){
        return $long = array(
            '3' => '长龙助手',
            '4' => '路珠长龙',
        );
    }

	function Code(Req $req) {
		$game = isset($req->game) ? $req->game : '';
		$no = $req->no;
		if (empty($game)) {
			abort(404);
		}
		$path = '\App\Models\Game' . ucfirst($game);
		$db = new $path;
		$prize_data = $db->getPrizeCodes($no);
		if ($prize_data < 0) {
			echo '没有该期开奖数据';
		}
		$parse = new \App\Lib\Biz\Game\Parse\GameParse;
		$data = $parse->prizeParse($prize_data, $game);
		return view('admin.unset/prizeCode', ['data' => $data]);
	}
	//整期作废
	function Cancel(Req $req) {
		$type = $req->type;
		if (!$req->isMethod('post')) {
			return view('admin.unset/unset_del', ['type' => $type]);
		} else {
			$game = $req->game;
			$info_no = $req->no;
			$password = $req->password;
			if (!is_numeric($info_no)) {
				return responseToJson(-5030013022);
			}
			if (empty($password)) {
				return responseToJson(-7010000622);
			}
			if (empty($game)) {
				return responseToJson(-5040000201);
			}
			//验证密码
			$admin_info = \App\Models\System_user::where('id', session('adminInfo.admin_id'))->first();
			if (!$admin_info) {
				return responseToJson(-7010100202);
			}
			$admin_info = $admin_info->toArray();
			if (!VerPassword($password, $admin_info['encryption_2'], $admin_info['password_2'])) {
				return responseToJson(-7010001622);
			}
			if (($check = $this->checkNo($game, $info_no)) < 0) {
				return responseToJson($check);
			}
			DB::beginTransaction();
			$path = '\App\Models\Game' . ucfirst($game) . '_Buy';
			$game_buy_table = new $path;
			$where[] = array('no', '=', $info_no);
			$res = $game_buy_table->Cancel($where);
			if ($res < 0) {
				DB::rollBack();
				return responseToJson($res);
			}
			$buy_table = new \App\Models\MoneyBuy;
			$whe[] = array('game_name', '=', $game);
			$whe[] = array('game_no', '=', $info_no);
			$res2 = $buy_table->Cancel($whe);
			if ($res2 < 0) {
				DB::rollBack();
				return responseToJson($res2);
			}
			DB::commit();
			return responseToJson(1);
		}
	}
	//检测改期是否开奖
	private function checkNo($game, $info_no) {
		$path = '\App\Models\Game' . ucfirst($game);
		$game_table = new $path;
		$res = $game_table->where('info_no', $info_no)->where('status', 0)->frist();
		if (!$res) {
			return 1;
		}
		return -5051263522;
	}
	//批量作废
	function delete(Req $req) {
		$id = $req->id;
		if (!$req->isMethod('post')) {
			return view('admin.game/unset_del', ['id' => $id]);
		} else {
			$password = $req->password;
			if (empty($password)) {
				return responseToJson(-7010000622);
			}
			$ids = explode(',', $id);
			//验证密码
			$admin_info = \App\Models\System_user::where('id', session('adminInfo.admin_id'))->first();
			if (!$admin_info) {
				return responseToJson(-7010100202);
			}
			$admin_info = $admin_info->toArray();
			if (!VerPassword($password, $admin_info['encryption_2'], $admin_info['password_2'])) {
				return responseToJson(-7010001622);
			}
			//撤销的订单
			DB::beginTransaction();
			$buy_table = new \App\Models\MoneyBuy;
			$res2 = $buy_table->CancelByids($ids);
			if ($res2 < 0) {
				DB::rollBack();
				return responseToJson($res2);
			}
			DB::commit();
			return responseToJson(1);
		}
	}
	//查看投注详情
	function view(Req $req) {
		$dt = \App\Lib\DataTable\DataTable::init();
		$id = $req->id;
		$game_no = $req->game_no;
        $game_name = $req->game_name;
		if (empty($id)) {
			abort(404);
		}
		$db = new \App\Models\MoneyBuy();
		$data = $db->getInfoByID($id);
		$dt->setDataSource('/admin/game/CodeDetails?id=' . $id."&game_no=".$game_no."&game_name=".$game_name);
		$dt->setlang($data['game_name']);
		$dt->addColsFields('gameType', array('sort' => false, 'width' => 100));
		$dt->addColsFields('codes', array('sort' => false));
		$dt->addColsFields('money', array('sort' => false, 'width' => 100));
		$dt->addColsFields('odds', array('sort' => false, 'width' => 100));
		$dt->addColsFields('back', array('sort' => false, 'width' => 100));
		$dt->addColsFields('maybe', array('sort' => false, 'width' => 100));
		$dt->addColsFields('get_money', array('sort' => false, 'templet' => '#getMoney', 'width' => 100));
		$dt->addColsFields('prize_status', array('sort' => false, 'templet' => '#status', 'width' => 110));
		$dt->addColsFields('remark',array('sort'=>false,'width'=> '200','title'=>'备注'));
		return view('admin.unset/betdatial', $dt->render());

	}
}