<?php

namespace App\Http\Controllers\Admin;

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

/**
 *
 */
class MatchcodeController extends Controller {

	function index(Req $req) {
		$request=array();
		$request['odds_code'] = isset($req->odds_code) ? trim($req->odds_code) : null;
		$request['sureblurs'] = isset($req->sureblurs) ? $req->sureblurs : 'on';
		$request['game_code'] = isset($req->game_code) ? trim($req->game_code) : -1;

		$dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/matchcode/info');
        $dt->setLang('matchcode');
        $dt->addColsFields('id', array('templet' => '#userdetail', 'sort' => false, 'width' => 100));
        $dt->addColsFields('game_code', array('templet' => '#userdetail', 'sort' => false, 'width' => 100));
        $dt->addColsFields('odds_code', array('templet' => '#userdetail', 'sort' => false, 'width' => 200));
        $dt->addColsFields('odds_name', array('templet' => '#userdetail', 'sort' => false, 'width' => 200));
        $dt->addColsFields('p_id', array('templet' => '#userdetail', 'sort' => false, 'width' => 80));
        $dt->addColsFields('type', array('templet' => '#userdetail', 'sort' => false, 'width' => 110));

        if (checkRriv('/admin/matchcode/edit')) {
			$arr[] = 'edit';
		}
		$dt->setToolBar($arr, array('width' => 200));
        $dt->enableCheckBox();

		return view('admin.matchcode/index', $dt->render($request));
	}

	function info(){
		$page = Request::has('page') ? Request::get('page') : '';
		$list = Request::has('limit') ? Request::get('limit') : 10;
		$odds_code = Request::has('odds_code') ? Request::get('odds_code') : '';
		$sureblur = Request::has('sureblurs') ? Request::get('sureblurs') : 'off';
		$game_code = Request::has('game_code') ? Request::get('game_code') : '';

		$where = array();
		if (!empty($odds_code)) {
			if (empty($sureblur) || $sureblur == 'off') {
				$where[] = array('odds_code', 'like', '%' . $odds_code . '%');
			} else {
				$where[] = array('odds_code', '=', $odds_code);
			}
		}
		if (!empty($game_code)) {
			$where[] = array('game_code', '=', $game_code);
		}
		$newapp = new \App\Models\Matchcode();
        $data = $newapp->matchcodelist($list, $page, $where);
        
        return \App\Lib\DataTable\DataTable::init()->toJson($data['data'], $data['total']);
	}

	function addmatchcode(Req $req) {
		if (!$req->isMethod('post')) {
            $lange = trans('menu');
			$newapp = new \App\Models\Matchcode();
	        $djid = $newapp->djlist();

			return view('admin.matchcode/addmatchcode',['data'=>$djid]);
		} else {
			$model = new \App\Models\Matchcode();
			$model->odds_code = trim($req->input('odds_code'));
			$model->odds_name = trim($req->input('odds_name'));
			$model->game_code = trim($req->input('game_code'));
			$model->p_id = trim($req->input('p_id'));
			$model->type = 1;

			$model->save();
			return responseToJson(1);
		}

	}

	function edit(Req $req) {
		$id = $req->id;
		if (intval($id) < 1) {
			return -1;
		}
		if (!$req->isMethod('post')) {
			$data = \App\Models\Matchcode::where('id', $id)->first();
			if (!$data) {
				return -2;
			}
			$data = $data->toArray();

			$newapp = new \App\Models\Matchcode();
	        $djid = $newapp->djlist();

			return view('admin.matchcode/edit', ['data' => $data, 'djid' => $djid]);
		} else {
			$model = \App\Models\Matchcode::where('id', $id)->first();
			$model->odds_code = $req->input('odds_code');
			$model->odds_name = $req->input('odds_name');
			$model->game_code = $req->input('game_code');
			$model->p_id = $req->input('p_id');
			
			$model->save();
			return responseToJson(1);
		}
	}

	//删除
	function delete(Req $req) {
		$id = $req->input('id');
		if (empty($id)) {
			return responseToJson(-2001); //
		}
		$ids = explode(',', $id);
		if (!is_array($ids) && intval($ids) < 0) {
			return responseToJson(-2002); //
		}
		if (is_array($ids) && count($ids) > 0) {
			foreach ($ids as $k => $v) {
				if (intval($v) < 1) {
					unset($ids[$k]);
				}
			}
		}
		// echo '敬请期待';die;
		$rows = \App\Models\Matchcode::whereIn('id', $ids)->delete();
		if (!$rows) {
			return responseToJson(-2003); 
		}
		return responseToJson(1); 
	}

}