<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TeamController extends  Controller{


    public function team_list(Request $request){
        if ($request->type==0){
            $sid=DB::table('war_zone')->where('war_name',$request->value)->value('uid');
            $data1=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('enroll.status','<>',1)
                ->where('service.uid',$sid)
                ->select('enroll.uid','user.nickname', 'user.up_name','user.up_phone','user.phone','user.level_name','user.integral','user.avatar','enroll.created_at','enroll.status','enroll.is_refund','enroll.pay_number','enroll.refund_number')
                ->paginate(15);
            $total=0;
            $dt=[];
            if ($data1->items()){
                foreach ($data1->items() as $k=>$v){
                    $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']=date('Y-m-d',strtotime($v->created_at));
                    $dt[$k]['status']=$v->status;
                    $dt[$k]['refunded']=$v->is_refund;
                    $dt[$k]['integral']=$v->integral;
                    $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;
                }
                $total=$data1->total();
            }

            $data2=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('enroll.status','<>',1)
                ->where('user.level_name','销售经理')
                ->where('service.uid',$sid)
                ->count();
            $data3=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('enroll.status','<>',1)
                ->where('user.level_name','销售主管')
                ->where('service.uid',$sid)
                ->count();
            return $this->response()->array([
                'code'=>200,
                'data'=>[
                    'total'=>$total,
                    'top_num'=>$data2,
                    'super_num'=>$data3,
                    'list'=>$dt
                ]
            ]);

        }
        if ($request->type==1){
            $crown=DB::table('user')->where('id',$request->value)->first();
            $data1=DB::table('user')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('user.up_phone',$crown->phone)
                ->where('enroll.status','<>',1)
                ->select('enroll.uid','user.nickname', 'user.up_name','user.up_phone','user.phone','user.integral','user.avatar','user.level_name','enroll.created_at','enroll.status','enroll.is_refund','enroll.pay_number','enroll.refund_number')
                ->paginate(15);
            $total=0;
            $dt=[];
            if ($data1->items()){
                foreach ($data1->items() as $k=>$v){
                    $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']=date('Y-m-d',strtotime($v->created_at));
                    $dt[$k]['status']=$v->status;
                    $dt[$k]['refunded']=$v->is_refund;
                    $dt[$k]['integral']=$v->integral;
                    $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;
                }
                $total=$data1->total();
            }
            $data2=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('enroll.status','<>',1)
                ->where('user.level_name','销售经理')
                ->where('user.up_phone',$crown->phone)
                ->count();
            $data3=DB::table('user')
                ->join('service','service.phone','=','user.up_phone')
                ->join('enroll','enroll.uid','=','user.id')
                ->where('enroll.season',$request->season)
                ->where('enroll.status','<>',1)
                ->where('user.level_name','销售主管')
                ->where('user.up_phone',$crown->phone)
                ->count();
            return $this->response()->array([
                'code'=>200,
                'data'=>[
                    'total'=>$total,
                    'top_num'=>$data2,
                    'super_num'=>$data3,
                    'list'=>$dt
                ]
            ]);
        }


    }

    public function team_all(){
        $data=DB::table('war_zone')->join('user','war_zone.uid','=','user.id')
            ->select('user.nickname','war_zone.war_name','user.id')->get();
        foreach ($data as $k=>$v){
            $list[$k]['war_name']=$v->war_name;
            $list[$k]['service']=$v->nickname;
            $crown=DB::table('user')->join('service','service.phone','=','user.phone')
                ->where('service.uid',$v->id)->select('user.name','user.id','user.phone')->get();
            $list[$k]['crowns']=$crown;
        }
        return $this->success_list($list);
    }
}