<?php

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

class SignupController extends Controller
{
    public function user_all(Request $request){

        //获取赛季内的所有已报名用户

        $data=DB::table('user')->join('enroll','enroll.uid','=','user.id')
            ->where('enroll.season',$request->season)->where('enroll.status','<>',1)
            ->select('enroll.uid','user.avatar','user.nickname', 'user.up_name','user.up_phone','user.phone','user.level_name','enroll.created_at','enroll.status','enroll.is_refund','enroll.pay_number','enroll.refund_number')
            ->paginate(15);
        $total=0;
        $dt=[];
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $war_name=DB::table('war_zone')
                    ->join('service','service.uid','=','war_zone.uid')
                    ->where('service.phone',$v->up_phone)->select('war_zone.war_name')->first();
                if (!$war_name){
                    $war_names='没有战区';
                }else{
                    $war_names=$war_name->war_name;
                }
                $dt[$k]['id']=$v->uid;
                $dt[$k]['nickname']=$v->nickname;
                $dt[$k]['phone']=$v->phone;
                $dt[$k]['level_name']=$v->level_name;
                $dt[$k]['time']=strtotime($v->created_at)*1000;
                $dt[$k]['status']=$v->status;
                $dt[$k]['avatar']=$v->avatar;
                $dt[$k]['refunded']=$v->is_refund;
                $dt[$k]['pay_order_num']=$v->pay_number;
                $dt[$k]['refund_order_num']=$v->refund_number;
                $dt[$k]['up_name']=$v->up_name;
                $dt[$k]['up_phone']=$v->up_phone;
                $dt[$k]['war_name']=$war_names;
            }
            $total=$data->total();
        }
        return $this->success_list($dt,'',$total);
    }
    //取消用户比赛资格
    public function user_update(Request $request){
        $id = $request->id;
        DB::table('enroll')->where('id','=',$id)->update(['status'=>2]);
        return $this->response()->array([
            'code'=>200
        ]);
    }

    //搜索已报名用户
    public function user_search(Request $request){
        $string=$request->phone;
        if (is_numeric($string)){
            $s='user.phone';
        }else{
            $s='user.nickname';
        }
        $data=DB::table('user')->join('enroll','enroll.uid','=','user.id')->where($s,'like','%'.$request->phone.'%')
            ->where('enroll.season',$request->season)->where('enroll.status','<>',1)
            ->select('enroll.uid','user.nickname', 'user.avatar','user.up_name','user.up_phone','user.phone','user.level_name','enroll.created_at','enroll.status','enroll.is_refund','enroll.pay_number','enroll.refund_number')
            ->paginate(15);
        $total=0;
        $dt=[];
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $war_name=DB::table('war_zone')
                    ->join('service','service.uid','=','war_zone.uid')
                    ->where('service.phone',$v->up_phone)->select('war_zone.war_name')->first();
                if (!$war_name){
                    $war_names='没有战区';
                }else{
                    $war_names=$war_name->war_name;
                }
                $dt[$k]['id']=$v->uid;
                $dt[$k]['nickname']=$v->nickname;
                $dt[$k]['phone']=$v->phone;
                $dt[$k]['level_name']=$v->level_name;
                $dt[$k]['time']=strtotime($v->created_at)*1000;
                $dt[$k]['status']=$v->status;
                $dt[$k]['refunded']=$v->is_refund;
                $dt[$k]['avatar']=$v->avatar;
                $dt[$k]['pay_order_num']=$v->pay_number;
                $dt[$k]['refund_order_num']=$v->refund_number;
                $dt[$k]['up_name']=$v->up_name;
                $dt[$k]['war_name']=$war_names;

            }
            $total=$data->total();
        }
        return $this->success_list($dt,'',$total);
    }

    public function down_excel(Request $request){
        $data=DB::table('user')->join('enroll','enroll.uid','=','user.id')
            ->join('service','service.phone','=','user.up_phone')
            ->where('enroll.season',$request->season)->where('enroll.status',0)
            ->select('enroll.uid','user.nickname', 'user.up_name','user.up_phone','user.phone','user.level_name','enroll.created_at','enroll.status','enroll.is_refund','enroll.pay_number','enroll.refund_number','service.war_names as war_name')
            ->get();
        $dt=[];
        if ($data){
            foreach ($data as $k=>$v){
//                $war_name=DB::table('war_zone')
//                    ->join('service','service.uid','=','war_zone.uid')
//                    ->where('service.phone',$v->up_phone)->select('war_zone.war_name')->first();
//                if (!$war_name){
//                    $war_name='未有战区归属';
//                }else{
//                    $war_name=$war_name->war_name;
//                }
                $dt[$k]['id']=$v->uid;
                $dt[$k]['nickname']=$v->nickname;
                $dt[$k]['phone']=$v->phone;
                $dt[$k]['level_name']=$v->level_name;
                $dt[$k]['time']=strtotime($v->created_at)*1000;
                $dt[$k]['status']=$v->status;
                $dt[$k]['refunded']=$v->is_refund;
                $dt[$k]['pay_order_num']=$v->pay_number;
                $dt[$k]['refund_order_num']=$v->refund_number;
                $dt[$k]['up_name']=$v->up_name;
                $dt[$k]['war_name']=$v->war_name;
                $dt[$k]['up_phone']=$v->up_phone;

            }
        }
        return $this->success_list($dt,'',0);
    }
}
