<?php
/**
 * 系统设置
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\ArticleType;
use Illuminate\Http\Request as Req;
use Request;
use App\Http\Model\St_match_rule as matchRuleModel;
use App\Http\Model\St_match_rule_group as matchRuleGroupModel;


/**
 *
 */
class MatchRuleController extends Controller
{

    /**
     * 赛事规则
     */
    function Index(Req $req)
    {
        $request['title'] = isset($req->title) ? trim($req->title) : null;
        $request['type'] = isset($req->type) ? trim($req->type) : null;

        $dt = \App\Lib\DataTable\DataTable::init();
        $dt->setDataSource('/admin/MatchRule/getMatchRule');
        $dt->setLang('matchRule');
        $dt->addColsFields('id', array('width' => 60));
        $dt->addColsFields('modular_name',array('sort' => false, 'width' => 80));
        $dt->addColsFields('group_name', array('templet' => '#group_name', 'sort' => false, 'width' => 120));
        $dt->addColsFields('atime',array('width' => 120));
        $dt->addColsFields('author',array('width' => 120));
        $dt->addColsFields('status', array('templet' => '#closeTool', 'sort' => false, 'width' => 80));
        $dt->enableCheckBox();
        $arr[] = 'view';
        if (checkRriv('/admin/MatchRule/Edit')) {
            $arr[] = 'edit';
        }
        $dt->setToolBar($arr, array('width' => 150));

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

    /**
     * 获取赛事规则数据
     */
    function getMatchRule(){
        $limit = Request::has('limit') ? Request::get('limit') : 10;

		$modular_name = Request::has('title') ? Request::get('title') : '';
        $group_id = Request::has('type') ? Request::get('type') : '';

		$where = array();
		if (!empty($modular_name)) {
			$where[] = array('modular_name', 'like', '%' . $modular_name . '%');
		}
		if (!empty($group_id)) {
			$where[] = array('group_id', '=', $group_id);
		}
	
		$db = new matchRuleModel();
        $data = $db->getList($limit, $where);	

		return \App\Lib\DataTable\DataTable::init()->toJson($data);
    }

    /**
     * 发布赛事规则
     */
    function addMatchRule(Req $req)
    {
        if (!$req->isMethod('post')) {
            return view('admin.matchRule/editMatchRule', ['select' => 0, "child_id" => 0]);
        } else {
            if(matchRuleModel::where('group_id', $req->type)->first()){
                return responseToJson(-20001);
            }
            $data['modular_name'] = $req->input('title');
            $data['group_id'] = $req->type;
            $data['group_name'] = matchRuleGroupModel::find($data['group_id'])->group_name;
            $data['content'] = $req->desc;
            $log = array(
                session('adminInfo.admin_name'),
                $data['modular_name']
            );

            $data['atime'] = date('Y-m-d H:i:s');
            $data['utime'] = date('Y-m-d H:i:s');
            $data['author'] = session('adminInfo.admin_name');
            $data['status'] = 1;
            $db = new matchRuleModel();
            $res = $db->addMatchRule($data);
            OperationLog(session('adminInfo.admin_id'), 'add_match_rule', $log);
            return responseToJson(1);
        }
    }

    /**
     * 编辑
     */
    function Edit(Req $req)
    {
        $id = $req->id;
        if (intval($id) < 1) {
            abort(404);
        }

        $db = new matchRuleModel;
        if (!$req->isMethod('post')) {
            $data = $db->getDmsg($id);

            if (!$data) {
                return responseToJson($data);
            }
            $select = $data["group_id"];
            // $child_id = $data["child_id"];

            return view('admin.matchRule/editMatchRule', ['data' => $data]);

        } else {
            // $data['child_id'] = $req->child_id;
            $data['title'] = $req->input('title');
            // $data['author'] = $req->input('author');
            $data['sort'] = $req->input('sort');
            $data['type'] = $req->input('type');
            $data['img'] = $req->input('img');
            $data['typebind'] = $req->input('typebind');
            $data['mobilecontent'] = $req->mobilecontent;
            $data['mobileimg'] = $req->input('mobileimg');
            $data['content'] = $req->desc;
            $res = $db->updateMsg($data, $id);
            $log = array(
                session('adminInfo.admin_name'),
                $data['title']
            );
            OperationLog(session('adminInfo.admin_id'), 'edit_game', $log);
            return responseToJson($res);
        }
    }

    /**
     * 禁用
     */
    function activeClose(Req $req)
    {
        $status = $req->has('status') ? $req->get('status') : '';
        $id = $req->has('id') ? $req->get('id') : '';


        if (empty($id)) {
            return responseToJson(-5030002031);
        }
        if ($status < 0) {
            return responseToJson(-5030002032);
        }

        $db = new matchRuleModel();

        $data = array(
            'status' => $status,
        );
        if ($status) {
            $data['status'] = 1;
        } else {
            $data['status'] = 0;
        }
        $res = $db->closeGame(["id" => intval($id)], $data);
        return responseToJson($res);
    }


    /**
     * 查看
     */
    function view(Req $req)
    {
        return $this->Edit($req);
    }

   
   

    /**
     * 批量删除
     * [delete description]
     * @return [type] [description]
     */
    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]);
                }
            }
        }
        $rows = matchRuleModel::whereIn('id', $ids)->delete();
        if (!$rows) {
            return responseToJson(-2003);
        }
        return responseToJson(1);
    }

   

    
}

?>