<?php

namespace  App\Http\Controllers;

use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class UploadController extends Controller{


    public function success_all(Request $request){
        if ($request->war_name=='请选择战区'){
            $data=DB::table('inte_review')->join('user','user.id','=','inte_review.uid')
                ->where('inte_review.season',$request->season)
                ->where('inte_review.time','>','1573228799')
                ->select('inte_review.id','user.avatar','user.phone','user.nickname','inte_review.*')->orderByDesc('inte_review.created_at')
                ->paginate(15);
        }else{
            $data=DB::table('inte_review')->join('user','user.id','=','inte_review.uid')
                ->join('service','user.up_phone','=','service.phone')
                ->join('war_zone','war_zone.uid','=','service.uid')
                ->where('war_zone.war_name',$request->war_name)
                ->where('inte_review.season',$request->season)
                ->where('inte_review.time','>','1573228799')
                ->select('inte_review.id','user.phone','user.avatar','user.nickname','inte_review.*')->orderByDesc('inte_review.created_at')
                ->paginate(15);
        }

        $all=[];
        $datas=$data->items();
        if ($data){
            foreach ($datas as $k=>$v){
                $all[$k]['id']=$v->id;
                $all[$k]['time']=strtotime($v->created_at)*1000;
                $all[$k]['img']=$v->imgurl;
                $all[$k]['hard']=$v->type_one;
                $all[$k]['simple']=$v->type_two;
                $all[$k]['old']=$v->type_three;
                $all[$k]['order_num']=$v->number;
                $all[$k]['score']=$v->integral;
                $all[$k]['phone']=$v->phone;
                $all[$k]['type']=$v->status;
                $all[$k]['avatar']=$v->avatar;
                $all[$k]['nickname']=$v->nickname;
                $all[$k]['money']=$v->figure;
            }
            $total=$data->total();
            return $this->success_list($all,'ok',$total);
        }
        return $this->success_list([]);

    }

    public function search(Request $request){
        $string=$request->phone;

        if (is_numeric($string)){
            $s='user.phone';
            if (mb_strlen($string,'utf-8')>20){
                $data=DB::table('inte_review')
                    ->join('user','user.id','=','inte_review.uid')
                    ->where('inte_review.number','=',$string)
//                    ->where('inte_review.time','>','1573228799')
                    ->where('inte_review.season','=',$request->season)
                    ->select('inte_review.id','user.avatar','user.phone','user.nickname','inte_review.created_at','inte_review.imgurl','inte_review.type_one','inte_review.type_two','inte_review.type_three','inte_review.number','inte_review.integral','inte_review.status','inte_review.figure')
                    ->orderByDesc('created_at')->paginate(15);
                $all=[];
                if ($data){
                    foreach ($data as $k=>$v){
                        $all[$k]['id']=$v->id;
                        $all[$k]['time']=strtotime($v->created_at)*1000;
                        $all[$k]['img']=$v->imgurl;
                        $all[$k]['hard']=$v->type_one;
                        $all[$k]['simple']=$v->type_two;
                        $all[$k]['old']=$v->type_three;
                        $all[$k]['order_num']=$v->number;
                        $all[$k]['score']=$v->integral;
                        $all[$k]['type']=$v->status;
                        $all[$k]['phone']=$v->phone;
                        $all[$k]['avatar']=$v->avatar;
                        $all[$k]['nickname']=$v->nickname;
                        $all[$k]['money']=$v->figure;
                    }
                    $total=$data->total();
                }else{
                    $total=0;
                }

                return $this->success_list($all,'ok',$total);
            }
        }else{
            $s='user.nickname';
        }
        if ($request->status==1){
            $data=DB::table('fail')
                ->join('user','user.id','=','fail.uid')
                ->where($s,'like','%'.$request->phone.'%')
                ->where('fail.status',0)
                ->where('fail.season',$request->season)
                ->where('fail.time','>','1573228799')
                ->select('fail.id','user.phone','user.avatar','user.nickname','fail.created_at','fail.imgurl','fail.type_one','fail.type_two','fail.type_three','fail.number','fail.remark','fail.figure')
                ->orderByDesc('created_at')->paginate(15);
            $all=[];
            if ($data){
                foreach ($data as $k=>$v){
                    $all[$k]['id']=$v->id;
                    $all[$k]['time']=strtotime($v->created_at)*1000;
                    $all[$k]['img']=$v->imgurl;
                    $all[$k]['hard']=$v->type_one;
                    $all[$k]['simple']=$v->type_two;
                    $all[$k]['old']=$v->type_three;
                    $all[$k]['order_num']=$v->number;
                    $all[$k]['phone']=$v->phone;
                    $all[$k]['nickname']=$v->nickname;
                    $all[$k]['money']=$v->figure;
                    $all[$k]['avatar']=$v->avatar;
                    $all[$k]['remark']=$v->remark;
                }
                $total=$data->total();
            }else{
                $total=0;
            }

            return $this->success_list($all,'ok',$total);
        }
        if ($request->status==0){
            $data=DB::table('inte_review')
                ->join('user','user.id','=','inte_review.uid')
                ->where($s,'like','%'.$request->phone.'%')
                ->where('inte_review.time','>','1573228799')
                ->where('inte_review.season','=',$request->season)
                ->select('inte_review.id','user.avatar','user.phone','user.nickname','inte_review.created_at','inte_review.imgurl','inte_review.type_one','inte_review.type_two','inte_review.type_three','inte_review.number','inte_review.integral','inte_review.status','inte_review.figure')
                ->orderByDesc('created_at')->paginate(15);
            $all=[];
            if ($data){
                foreach ($data as $k=>$v){
                    $all[$k]['id']=$v->id;
                    $all[$k]['time']=strtotime($v->created_at)*1000;
                    $all[$k]['img']=$v->imgurl;
                    $all[$k]['hard']=$v->type_one;
                    $all[$k]['simple']=$v->type_two;
                    $all[$k]['old']=$v->type_three;
                    $all[$k]['order_num']=$v->number;
                    $all[$k]['score']=$v->integral;
                    $all[$k]['type']=$v->status;
                    $all[$k]['phone']=$v->phone;
                    $all[$k]['avatar']=$v->avatar;
                    $all[$k]['nickname']=$v->nickname;
                    $all[$k]['money']=$v->figure;
                }
                $total=$data->total();
            }else{
                $total=0;
            }

            return $this->success_list($all,'ok',$total);
        }
        else{
            $datas=DB::table('fail')
                ->join('user','user.id','=','fail.uid')
                ->where($s,'like','%'.$request->phone.'%')
                ->where('fail.type','<>',0)
                ->where('fail.season','=',$request->season)
                ->where('fail.time','>','1573228799')
                ->select('user.nickname','user.phone','user.avatar','fail.note','fail.type','fail.type_one as hard','fail.imgurl as img','fail.type_two as simple','fail.type_three as old','fail.remark','fail.figure as money','fail.number as order_num','fail.created_at as time')
                ->paginate(15);
            }
            return $this->success_list($datas->items(),'0',$datas->total());



    }

    public function change_inte(Request $request){
        $uid=DB::table('inte_review')->where('id',$request->id)->value('uid');
        $num=DB::table('inte_review')->where('id',$request->id)->first();
        $count=$num->type_one+$num->type_two+$num->type_three;
        $count_change=$request->hard+$request->simple+$request->old;
        DB::table('inte_review')->where('id',$request->id)->update([
            'type_one'=>$request->hard,
            'type_two'=>$request->simple,
            'type_three'=>$request->old,
            'integral'=>$count_change*10,
        ]);
        DB::table('inte_log')->where('imgurl',$num->imgurl)->update([
//            'type_one'=>$request->hard,
//            'type_two'=>$request->simple,
//            'type_three'=>$request->old,
            'integral'=>$count_change*10,
            'remarks'=>Auth::user()->name
        ]);
        $count_now=$count_change-$count;
        DB::table('user')->where('id',$uid)->increment('integral',10*$count_now);
        return $this->success([]);
    }

    public function fail_all(Request $request){
        if ($request->war_name=='请选择战区'){
            $datas=DB::table('fail')->join('user','user.id','=','fail.uid')
                ->where('fail.status',0)
                ->where('fail.season',$request->season)
                ->select('fail.id','user.nickname as nickname','user.avatar','user.phone','fail.figure as money','fail.imgurl as img','fail.type_one as hard','fail.type_two as simple','fail.type_three as old','fail.season','fail.number as order_num','fail.remark','fail.time as time')->orderByDesc('fail.created_at')
                ->paginate(15);

        }else{
            $datas=DB::table('fail')->join('user','user.id','=','fail.uid')
                ->join('service','user.up_phone','=','service.phone')
                ->join('war_zone','war_zone.uid','=','service.uid')
                ->where('war_zone.war_name',$request->war_name)
                ->where('fail.time','>','1573228799')
                ->where('fail.status',0)
                ->where('fail.season',$request->season)
                ->select('fail.id','user.nickname as nickname','user.avatar','user.phone','fail.figure as money','fail.imgurl as img','fail.type_one as hard','fail.type_two as simple','fail.type_three as old','fail.season','fail.number as order_num','fail.remark','fail.time as time')->orderByDesc('fail.created_at')
                ->paginate(15);
        }
        if ($datas->items()){
            $data=[];
            foreach ($datas as $k=>$v){
                $data[$k]['nickname']=$v->nickname;
                $data[$k]['id']=$v->id;
                $data[$k]['phone']=$v->phone;
                $data[$k]['avatar']=$v->avatar;
//                $data[$k]['note']=$v->note;
                $data[$k]['img']=$v->img;
                $data[$k]['hard']=$v->hard;
                $data[$k]['season']=$v->season;
                $data[$k]['simple']=$v->simple;
                $data[$k]['old']=$v->old;
                $data[$k]['avatar']=$v->avatar;
                $data[$k]['remark']=$v->remark;
                $data[$k]['money']=$v->money;
                $data[$k]['order_num']=$v->order_num;
                $data[$k]['time']=date('Y-m-d H:i:s',$v->time);
            }
            return $this->success_list($data,'ok',$datas->total());
        }


        return $this->success_list([]);
    }

    public function judge(Request $request){
        $input=$request->all();
        Log::error('{--------审核记录开始--------}'.Auth::user()->name.'{--------参数--------}'.'{-----------审核记录----------}'.Carbon::now()->toDateTimeString());
        Log::error(json_encode($input));
        if ($request->type==0){
//            $data=DB::table('fail')->where('id',$request->id)->first();
            $s=DB::table('inte_review')->where('number',$request->order_num)->first();
            if ($s){
//                DB::table('fail')->where('id',$request->id)->update([
//                    'status'=>1,
//                    'note'=>Auth::user()->name,
//                    'type'=>1,
//                    'time'=>$request->time
//                ]);
                Log::error('{--------审核记录--------}'.Auth::user()->name.'{-------无效二次判断----审核记录----------}'.Carbon::now()->toDateTimeString());
                return $this->error(300);
            }
            DB::table('fail')->where('id',$request->id)->update([
                'status'=>1,
                'note'=>Auth::user()->name,
                'type'=>2,
                'time'=>$request->time
            ]);
        }
        if ($request->type==1){
            $data=DB::table('fail')->where('id',$request->id)->first();
            $s=DB::table('inte_review')->where('number',$request->order_num)->first();
            if ($s){

                Log::error('{--------审核记录--------}'.Auth::user()->name.'{-------有效二次判断----审核记录----------}'.Carbon::now()->toDateTimeString());
//                DB::table('fail')->where('id',$request->id)->update([
//                    'status'=>1,
//                    'note'=>Auth::user()->name,
//                    'type'=>1,
//                    'time'=>$request->time
//                ]);
                return $this->error(300);
            }
            $re=DB::table('fail')->where('id',$request->id)->update([
                'status'=>1,
                'note'=>Auth::user()->name,
                'type'=>1,
                'time'=>$request->time
            ]);
            Log::error('{--------审核记录--------}'.Auth::user()->name.'{-----------审核记录----------}'.Carbon::now()->toDateTimeString());
//            $data=DB::table('fail')->where('id',$request->id)->first();
            $inte=($request->hard+$request->simple+$request->old)*10;
            if ($re){
                DB::table('inte_review')->insert([
                    'uid'=>$data->uid,
                    'integral'=>$inte,
                    'imgurl'=>$data->imgurl,
                    'status'=>Auth::user()->name,
                    'season'=>$request->season,
                    'type_one'=>$request->hard,
                    'type_two'=>$request->simple,
                    'type_three'=>$request->old,
                    'number'=>$request->order_num,
                    'figure'=>$request->money,
                    'created_at'=>Carbon::now()->toDateTimeString(),
                    'time'=>$request->time
                ]);
                Log::error('{------插入记录返回结果------}'.$re.'{--------返回结果--------}');

                DB::table('inte_log')->insert([
                    'uid'=>$data->uid,
                    'season'=>$data->season,
                    'type'=>1,
                    'imgurl'=>$data->imgurl,
                    'integral'=>$inte,
                    'created_at'=>Carbon::now()->toDateTimeString(),
                    'updated_at'=>Carbon::now()->toDateTimeString(),
                    'time'=>$request->time
                ]);
                Log::error('{------插入学分返回结果------}'.'{--------返回结果--------}');
                DB::table('user')->where('id',$data->uid)->increment('integral',$inte);
                DB::table('user')->where('id',$data->uid)->increment('money',$request->money);
                DB::table('user')->where('id',$data->uid)->increment('total',$inte/10);
                Log::error('{--------审核记录结束--------}'.Auth::user()->name.'{-----------审核记录----------}'.Carbon::now()->toDateTimeString());
            }

        }
        return $this->success([]);
    }


    /*----判定记录---*/

    public function record(Request $request){
        if ($request->war_name=='请选择战区'){
            $data=DB::table('fail')
                ->join('user','user.id','=','fail.uid')
                ->where('fail.type','<>',0)
                ->where('fail.season',$request->season)
                ->select('fail.id','user.nickname','user.phone','user.avatar','fail.note','fail.type','fail.type_one as hard','fail.type_two as simple','fail.type_three as old','fail.imgurl as img','fail.remark','fail.figure as money','fail.number as order_num','fail.created_at as time')
                ->paginate(15);
            return $this->success_list($data->items(),'0',$data->total());
        }else{
            $data=DB::table('fail')
                ->join('user','user.id','=','fail.uid')
                ->join('service','user.up_phone','=','service.phone')
                ->join('war_zone','war_zone.uid','=','service.uid')
                ->where('war_zone.war_name',$request->war_name)
                ->where('fail.type','<>',0)
                ->where('fail.season',$request->season)
                ->select('fail.id','user.nickname','user.phone','user.avatar','fail.note','fail.type','fail.type_one as hard','fail.type_two as simple','fail.type_three as old','fail.imgurl as img','fail.remark','fail.figure as money','fail.number as order_num','fail.created_at as time')
                ->paginate(15);
            return $this->success_list($data->items(),'0',$data->total());
        }
    }
}