<?php
/*比赛管理*/
namespace App\Http\Controllers\Admin;

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

class MatchController extends Controller
{
    //核销用户列表
    public function checkerList(Request $request)
    {
        $list = Db::table('agent.dbo.agent')
                ->select('created_at','name','wx_account','password')
                ->where('hexiao',1)
                ->paginate(10);
    }

    //创建核销用户
    public function createChecker(Request $request)
    {
        if(Agent::where('wx_account',$params['wx_account'])->first()){
            return ['status'=>false,'message'=>'登录账号不能重复'];
        }
        $password = bcrypt($params['password']);
        $CreatedParentID = Auth::user()->id;
        $data = [
            'role_id'   =>  0,
            'parent_id' =>  0,
            'name'      =>  $params['name'],
            'wx_account'=>  $params['wx_account'],
            'mobile'    =>  0,
            'id_card'   =>  0,
            'status'    =>  1,
            'password'  =>  $password,
            'num_account' => $this->get_num_account(),
            'CreatedParentID'   =>  $CreatedParentID,
            'hexiao' => 1,
            'channel'   =>  Auth::user()->channel
        ];
        if($agent = Agent::create($data)){
            AgentApp::create([
                'agent_id'=>$agent->id,
                'app_id'=>1
            ]);
            return ['status'=>true,'message'=>'创建成功'];
        }else{
            return ['status'=>false,'message'=>'创建失败'];
        }
    }

    public function getlist(Request $request)
    {   
        $params = $request->all();
        $match = isset($params['match']) ? $params['match'] : 1;//0 推广员

        $status = isset($params['status']) ? $params['status'] : 0;
        $searchKey = isset($params['searchKey']) ? $params['searchKey'] : 0;
        $YN_Team = new Servers();
        $YN_Team->setTable('YN_Team');
        //$count = $YN_Team->where('Status',$status)->where('match',$match)->count();
        //$data = $YN_Team->orderBy('created','desc')->get();
        
        // $limit = isset($params['limit']) ? intval($params['limit']) : 50;
        // $offset = isset($params['offset']) ? intval($params['offset']) : 0;

       
        if($match ==2){
            $query = $YN_Team->select('YN_Team.*')->join('YN_TeamCode','YN_Team.TeamID','=','YN_TeamCode.TeamID')->where('YN_TeamCode.Status',1)->where('YN_TeamCode.Match',$match);
            if($status != 'all'){
                $query = $query->where('YN_Team.Status',$status);
            }
        }else{
            $query = $YN_Team->where('Match',$match);
            if($status != 'all'){
                $query = $query->where('Status',$status);
            }
        }
        if($searchKey){
            $query->where(function ($query) use ($searchKey) {
                                   $query->where('YN_Team.TeamName', 'like', "%{$searchKey}%")->orWhere('YN_Team.TeamID', 'like', "%{$searchKey}%");
                                   });
        }
        //->skip($offset)->take($limit)
        $count = $query->count();
        $data = $query->orderBy('YN_Team.created','desc')->get();

        return Response::json(['data'=>$data,'count'=>$count,'status'=>true,'searchKey'=>$searchKey],200);
    }

    
}
