<?php

namespace App\Http\Controllers;
use Egulias\EmailValidator\Validation\DNSCheckValidation;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BonusController extends Controller
{
    //获取奖励金明细列表
    public function all(Request $request){
//        return $request->war_name;
        if($request->has('war_name')){
            if ($request->war_name=='请选择战区'){
                $data=DB::table('user')
                    ->where('user.level_name','<>','政委')
                    ->select('user.avatar','user.id','user.nickname','user.phone','user.up_phone','user.bonus','user.level_name')
                    ->orderByDesc('bonus')->paginate(15);
                $season=DB::table('basic')->where('keys','=','season')->value('value');
                $dt=[];

                foreach ($data as $k=>$v){
                    $s=DB::table('gift_orders')->where('uid',$v->id)->where('season',$request->season)->get();
                    $all=0;
                    foreach ($s as $key=>$value){
                        $all=$all+$value->num*$value->bonus;
                    }
                    if ($season==$request->season){
                        $bonus_all = $all+$v->bonus;
                        $dt[$k]['bonus_sup'] = $v->bonus;
                    }else{
                        $bon=DB::table('glory')->where('season',$request->season)->where('uid','=',$v->id)->value('bonus');
                        if (!$bon){
                            $bon=0;
                        }
                        $bonus_all=$bon;
                        $dt[$k]['bonus_sup'] = 0;
                    }

                    $dt[$k]['nickname'] = $v->nickname;
                    $dt[$k]['phone'] = $v->phone;
                    $dt[$k]['level_name'] = $v->level_name;
                    $dt[$k]['avatar'] = $v->avatar;
                    if ($v->level_name=='代理公司'){
                        $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v->phone)->value('war_names')).'战区';
                    }else{
                        $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v->up_phone)->value('war_names')).'战区';
                    }

                    $dt[$k]['bonus_all'] = $bonus_all;
                }
                return $this->success_list($dt,'',$data->total());
            }else{
                $users = DB::table('user')
                    ->join('service','service.phone','=','user.up_phone')
                    ->where('service.war_names','=',$request->war_name)
                    ->where('user.level_name','<>','政委')
                    ->select('user.avatar','user.id','user.nickname','user.phone','user.up_phone','user.bonus','user.level_name')
                    ->orderByDesc('bonus')->get()->map(function ($value) {return (array)$value;})->toArray();
                $userss = DB::table('user')
                    ->join('service','service.phone','=','user.phone')
                    ->where('service.war_names','=',$request->war_name)
                    ->where('user.level_name','<>','政委')
                    ->select('user.avatar','user.id','user.nickname','user.phone','user.up_phone','user.bonus','user.level_name')
                    ->orderByDesc('bonus')->get()->map(function ($value) {return (array)$value;})->toArray();
                $data=array_merge($userss,$users);
            }
            $season=DB::table('basic')->where('keys','=','season')->value('value');
            $dt=[];

            foreach ($data as $k=>$v){
                $s=DB::table('gift_orders')->where('uid',$v['id'])->where('season',$request->season)->get();
                $all=0;
                foreach ($s as $key=>$value){
                    $all=$all+$value->num*$value->bonus;
                }
                if ($season==$request->season){
                    $bonus_all = $all+$v['bonus'];
                    $dt[$k]['bonus_sup'] = $v['bonus'];
                }else{
                    $bon=DB::table('glory')->where('season',$request->season)->where('uid','=',$v['id'])->value('bonus');
                    if (!$bon){
                        $bon=0;
                    }
                    $bonus_all=$bon;
                    $dt[$k]['bonus_sup'] = 0;
                }

                $dt[$k]['nickname'] = $v['nickname'];
                $dt[$k]['phone'] = $v['phone'];
                $dt[$k]['level_name'] = $v['level_name'];
                $dt[$k]['avatar'] = $v['avatar'];
                if ($v['level_name']=='代理公司'){
                    $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v['phone'])->value('war_names')).'战区';
                }else{
                    $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v['up_phone'])->value('war_names')).'战区';
                }

                $dt[$k]['bonus_all'] = $bonus_all;

            }
            $num=count($dt);
            if ($request->has('page')){
                $page=($request->page)-1;
            }else{
                $page=0;
            }
            $num1=$num-$page*15;
            if ($num1>=15){
                $j=15;
            }else{
                $j=$num1;
            }
            $dts=[];
            for ($i=0;$i<$j;$i++){
                $dts[$i]['nickname']=$dt[$page*15+$i]['nickname'];
                $dts[$i]['phone'] =$dt[$page*15+$i]['phone'];
                $dts[$i]['level_name']=$dt[$page*15+$i]['level_name'];
                $dts[$i]['avatar']=$dt[$page*15+$i]['avatar'];
                $dts[$i]['war_name']=$dt[$page*15+$i]['war_name'];
                $dts[$i]['bonus_all']=$dt[$page*15+$i]['bonus_all'];
                $dts[$i]['bonus_sup']=$dt[$page*15+$i]['bonus_sup'];
            }
            return $this->success_list($dts,'',$num);
        }else{
            $data=DB::table('user')
                ->where('user.level_name','<>','政委')
                ->select('user.avatar','user.id','user.nickname','user.phone','user.up_phone','user.bonus','user.level_name')
                ->orderByDesc('bonus')->paginate(15);
            $season=DB::table('basic')->where('keys','=','season')->value('value');
            $dt=[];

            foreach ($data as $k=>$v){
                $s=DB::table('gift_orders')->where('uid',$v->id)->where('season',$request->season)->get();
                $all=0;
                foreach ($s as $key=>$value){
                    $all=$all+$value->num*$value->bonus;
                }
                if ($season==$request->season){
                    $bonus_all = $all+$v->bonus;
                    $dt[$k]['bonus_sup'] = $v->bonus;
                }else{
                    $bon=DB::table('glory')->where('season',$request->season)->where('uid','=',$v->id)->value('bonus');
                    if (!$bon){
                        $bon=0;
                    }
                    $bonus_all=$bon;
                    $dt[$k]['bonus_sup'] =0;
//                    $dt[$k]['bonus_sup'] =$bon-$all;
                }

                $dt[$k]['nickname'] = $v->nickname;
                $dt[$k]['phone'] = $v->phone;
                $dt[$k]['level_name'] = $v->level_name;
                $dt[$k]['avatar'] = $v->avatar;
                if ($v->level_name=='代理公司'){
                    $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v->phone)->value('war_names')).'战区';
                }else{
                    $dt[$k]['war_name'] = (DB::table('service')->where('phone',$v->up_phone)->value('war_names')).'战区';
                }

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

    }
    //搜索奖励金明细列表
    public function search(Request $request){
//        if ($request->has('war_name')){
//
//        }
        $season=DB::table('basic')->where('keys','=','season')->value('value');

        $phone = $request->phone;
        $dt=[];
        $users = DB::table('user')->select('id','avatar','nickname','phone','up_phone','bonus','level_name')->where('phone','=',$phone)->first();
        if (!$users){
            return $this->error(300);
        }
        $re=DB::table('gift_orders')->where('season',$request->season)->where('uid',$users->id)->get();
        $bonus=0;
        foreach ($re as $k=>$v){
            $bonus=$bonus+$v->num*$v->bonus;
        }
        $bonus_all = (DB::select("select sum(b.bonus) as bonus from (select (a.num*a.bonus) as bonus from gift_orders a where uid=? and season=?) b ",[$users->id,$request->season]))[0]->bonus+$users->bonus;
        $dt['nickname'] = $users->nickname;
        $dt['phone'] = $users->phone;
        $dt['level_name'] = $users->level_name;
        $dt['avatar'] = $users->avatar;
        if ($users->level_name=='代理公司'){
            $dt['war_name'] = (DB::table('service')->where('phone',$users->phone)->value('war_names')).'战区';
        }else{
            $dt['war_name'] = (DB::table('service')->where('phone',$users->up_phone)->value('war_names')).'战区';
        }
        if ($request->season==$season){
            $dt['bonus_all'] = $bonus+$users->bonus;
            $dt['bonus_sup'] = $users->bonus;
        }else{
            $dt['bonus_all'] = DB::table('glory')->where('season',$request->season)->where('uid',$users->id)->value('bonus');
            $dt['bonus_sup'] = $dt['bonus_all']-$bonus;
        }


        return $this->success_list([$dt],'',1);
    }

    public function download_bonus(Request $request){
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        if ($season==$request->season){
            $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('user.nickname','user.phone','user.bonus','user.up_phone','user.up_name','service.war_names','user.level_name')
                ->get();
        }else{
            $data=DB::table('glory')
                ->join('user','user.id','=','glory.uid')
                ->join('enroll','enroll.uid','=','glory.uid')
                ->where('glory.season','=',$request->season)
                ->where('enroll.season','=',$request->season)
                ->where('enroll.status','=',0)
                ->select('user.nickname','user.phone','user.up_phone','user.up_name','glory.bonus','user.level_name')->get();
        }
        $j=0;
        $re=[];
        foreach ($data as $k=>$v){
            if ($v->level_name!='政委'&&$v->level_name!='代理公司'){
                $re[$j]['昵称']=$v->nickname;
                $re[$j]['手机号']=$v->phone;
                $re[$j]['奖学金']=$v->bonus;
                $re[$j]['代理级别']=$v->level_name;
                $re[$j]['上级手机号']=$v->up_phone;
                $re[$j]['上级昵称']=$v->up_name;
                $j++;
            }
        }
        return $this->success_list($re);
    }
}