<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
//use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Facades\Excel;


class OrderController extends Controller
{

    public function upload_excel(Request $request){

        $data=$request->data;
//        dd($data);
        foreach ($data as $k=>$v){
            $id=DB::table('user')->where('phone',$v['phone'])->value('id');
            DB::table('gift_orders_copy1')->where('uid',$id)->update(['tracking_number'=>$v['tracking_number']]);
        }
        return $this->success([]);
//        dd($request->all());
    }


    public function send(Request $request){
        $data=$request->orders;
        $num=$request->express_order;
        DB::table('gift_orders')->whereIn('order_number',$data)->update([
            'shipping_status'=>1,
            'tracking_number'=>$num,
            'operator'=>Auth::user()->name
        ]);
        return $this->success([]);
//        dd($request->all());
    }

    public function sended(Request $request){
        $data=DB::table('gift_orders')
            ->join('user','user.id','=','gift_orders.uid')
            ->join('gift','gift.id','=','gift_orders.gift_id')
            ->where('gift_orders.shipping_status','=',1)
            ->where('gift_orders.season',$request->season)
            ->select('gift.name as gift_name','gift_orders.*','user.nickname','user.phone','user.up_name','user.up_phone','user.level_name')
            ->paginate(15);
//        $count=DB::table('gift_orders')->where('shipping_status',1)->count();
        $da=[];
        $total=0;
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $da[$k]['order_num']=$v->order_number;
                $da[$k]['time']=strtotime($v->created_at)*1000;
                $da[$k]['gift_name']=$v->gift_name;
                $da[$k]['gift_num']=$v->num;
                $da[$k]['username']=$v->nickname;
                $da[$k]['phone']=$v->phone;
//                $da[$k]['up_name']=$v->up_name;
                $da[$k]['bonus']=$v->bonus*$v->num;
                $da[$k]['operator']=$v->operator;
//                $da[$k]['up_phone']=$v->up_phone;
                $da[$k]['status']=$v->shipping_status==0?false:true;
                $da[$k]['express_order']=$v->tracking_number;
                if ($v->level_name=='代理公司'){
                    $da[$k]['up_phone']=$v->phone;
                    $da[$k]['up_name']=$v->nickname;
                }else{
                    $da[$k]['up_phone']=$v->up_phone;
                    $da[$k]['up_name']=$v->up_name;
                }
            }
            $total=$data->total();
        }
        return $this->success_list($da,'ok',$total);
    }

    public function nosend(Request $request){
        $data=DB::table('gift_orders')
//            ->join('address','address.id','=','gift_orders.address_id')
                ->join('user','user.id','=','gift_orders.uid')
            ->join('gift','gift.id','=','gift_orders.gift_id')
            ->where('gift_orders.shipping_status','=',0)
            ->where('gift_orders.season',$request->season)
            ->select('gift.name as gift_name','gift_orders.*','user.nickname','user.phone','user.up_name','user.up_phone','user.level_name','user.avatar')
            ->paginate(15);
//        $count=DB::table('gift_orders')->where('shipping_status',0)->count();
        $da=[];
        $total=0;
        if ($data->items()){
            foreach ($data->items() as $k=>$v){
                $da[$k]['order_num']=$v->order_number;
                $da[$k]['time']=strtotime($v->created_at)*1000;
                $da[$k]['gift_name']=$v->gift_name;
                $da[$k]['gift_num']=$v->num;
                $da[$k]['username']=$v->nickname;
                $da[$k]['phone']=$v->phone;
                $da[$k]['avatar']=$v->avatar;
//                if ($v->level=='')
                $da[$k]['bonus']=$v->bonus*$v->num;
                $da[$k]['status']=$v->shipping_status==0?false:true;
                if ($v->level_name=='代理公司'){
                    $da[$k]['up_phone']=$v->phone;
                    $da[$k]['up_name']=$v->nickname;
                }else{
                    $da[$k]['up_phone']=$v->up_phone;
                    $da[$k]['up_name']=$v->up_name;
                }
            }
            $total=$data->total();
        }
        return $this->success_list($da,'ok',$total);
    }

    public function search(Request $request){
        $user=DB::table('user')->where('phone',$request->phone)->first();
        $totals=0;
        $das=[];
        if ($user){
            $crown=DB::table('gift_orders')->join('gift','gift.id','=','gift_orders.gift_id')
                ->where('uid',$user->id)
                ->where('season',$request->season)
                ->where('shipping_status',$request->status)->select('gift_orders.*','gift.name as gift_name')
                ->paginate(200);

            if ($crown->items()){
                foreach ($crown->items() as $k=>$v){
                    $das[$k]['order_num']=$v->order_number;
                    $das[$k]['time']=strtotime($v->created_at)*1000;
                    $das[$k]['gift_name']=$v->gift_name;
                    $das[$k]['gift_num']=$v->num;
                    $das[$k]['username']=$user->nickname;
                    $das[$k]['phone']=$user->phone;
                    $das[$k]['up_name']=$user->nickname;
                    $das[$k]['bonus']=$v->bonus*$v->num;
                    $das[$k]['up_phone']=$user->phone;
                    $das[$k]['express_order']=$v->tracking_number;
                    $das[$k]['status']=$v->shipping_status==0?false:true;
                }
                $totals=$crown->total();
            }
//            return $this->success_list([]);
        }

        $data=DB::table('gift_orders')
            ->join('user','user.id','=','gift_orders.uid')
            ->join('gift','gift.id','=','gift_orders.gift_id')
            ->where('gift_orders.shipping_status','=',$request->status)
            ->where('gift_orders.season',$request->season)
            ->where('user.up_phone','=',$request->phone)
            ->select('gift.name as gift_name','gift_orders.*','user.nickname','user.phone','user.up_name','user.up_phone')
            ->paginate(15000);
        $total=0;
        $da=[];
        if ($data->items()){

            foreach ($data->items() as $k=>$v){
                $da[$k]['order_num']=$v->order_number;
                $da[$k]['time']=strtotime($v->created_at)*1000;
                $da[$k]['gift_name']=$v->gift_name;
                $da[$k]['gift_num']=$v->num;
                $da[$k]['username']=$v->nickname;
                $da[$k]['phone']=$v->phone;
                $da[$k]['up_name']=$v->up_name;
                $da[$k]['bonus']=$v->bonus*$v->num;
                $da[$k]['express_order']=$v->tracking_number;
                $da[$k]['up_phone']=$v->up_phone;
                $da[$k]['status']=$v->shipping_status==0?false:true;
            }
            $total=$data->total();
        }
        return $this->success_list(array_merge($das,$da),'ok',$total+$totals);
    }

    public function update_address(Request $request){
        $data['name']=$request->username;
        $data['phone']=$request->phone;
        $data['address']=$request->address;
        $re=DB::table('address')->where('id',$request->addressid)->update($data);
        if ($re){
            return $this->success([]);
        }
        return $this->error(300);
    }

    public function per_search(Request $request){
        $data=DB::table('gift_orders')
            ->join('user','user.id','=','gift_orders.uid')
            ->join('gift','gift.id','=','gift_orders.gift_id')
//            ->where('gift_orders.shipping_status','=',$request->status)
            ->where('gift_orders.season',$request->season)
            ->where('user.phone','=',$request->phone)
            ->select('gift.name as gift_name','gift_orders.*','user.nickname','user.phone','user.up_name','user.up_phone')
            ->paginate(15000);
        $total=0;
        $da=[];
        if ($data->items()){

            foreach ($data->items() as $k=>$v){
                $da[$k]['order_num']=$v->order_number;
                $da[$k]['time']=strtotime($v->created_at)*1000;
                $da[$k]['gift_name']=$v->gift_name;
                $da[$k]['gift_num']=$v->num;
                $da[$k]['username']=$v->nickname;
                $da[$k]['phone']=$v->phone;
                $da[$k]['up_name']=$v->up_name;
                $da[$k]['bonus']=$v->bonus;
                $da[$k]['up_phone']=$v->up_phone;
                $da[$k]['status']=$v->shipping_status==0?false:true;
            }
            $total=$data->total();
        }

        return $this->success_list($da,'',$total);
    }

    public function download_order(Request $request){
        $datas=DB::table('gift_orders')
            ->join('user','user.id','=','gift_orders.uid')
            ->join('gift','gift.id','=','gift_orders.gift_id')
            ->where('gift_orders.season',$request->season)
            ->select('user.nickname','user.phone','user.level_name','gift_orders.*','gift.name as gift_name')
            ->orderBy('gift_orders.con_phone')
            ->get();
        $dd=[];
        foreach ($datas as $k=>$v){
            $dd[$k]['代理昵称']=$v->nickname;
            $dd[$k]['代理手机号']=$v->phone;
            $dd[$k]['销售等级']=$v->level_name;
            $dd[$k]['礼品名称']=$v->name;
            $dd[$k]['兑换礼品数量']=$v->num;
            $dd[$k]['收货人']=$v->con_name;
            $dd[$k]['收货人手机号']=$v->con_phone;
            $dd[$k]['订单状态']=$v->shipping_status==0?'未发货':'已发货';
            $dd[$k]['物流单号']=$v->tracking_number;
        }

        return $this->success_list($dd);
    }

    public function download_per(Request $request){
        $data=DB::table('gift_orders')->where('season',$request->season)->groupBy('uid')->get();
//        dd($data);
//        $data=DB::table('gift_orders')->where('uid',3274)->get();
        $ss=[];
        $ssss=[];
        foreach ($data as $k=>$v){

            $s=DB::table('gift_orders')->where('season',$request->season)->where('gift_orders.uid',$v->uid)->get();
            $datas=[];
            //            $ds=['护心卡*99'=>0,'袜子*199'=>0,'鞋垫*199'=>0,'项链*399'=>0,'丝巾*399'=>0,'护腰*799'=>0,'秋衣秋裤 女M*1699'=>0,'秋衣秋裤 女L号*1699'=>0,'秋衣秋裤 女XL号*1699'=>0,'秋衣秋裤 女2XL号*1699'=>0,'秋衣秋裤 男XL号*1699'=>0,'秋衣秋裤 男2XL号*1699'=>0,'秋衣秋裤 男3XL号*1699'=>0,'超能量T恤 M号*799'=>0,'超能量T恤 L号*799'=>0,'超能量T恤 XL号*799'=>0,'超能量T恤 3XL号*799'=>0,'超能量T恤 4XL号*799'=>0,'口罩*199'=>0];
            $ds=['护心卡*99'=>0,'袜子*199'=>0,'鞋垫*199'=>0,'项链*399'=>0,'丝巾*399'=>0,'手环*199'=>0,'T恤  M号*799'=>0,'T恤 L号*799'=>0,'T恤 XL号*799'=>0,'T恤 XXXL号*798'=>0,'T恤XXL*799'=>0,'T恤 4XL*799'=>0];
            $bonus=0;
            foreach ($s as $key=>$value){
                $datas[]="$value->name*$value->num";
                if ($value->gift_id==1){
                    $ds['护心卡*99']=$value->num;
                    $bonus=$bonus+99*$value->num;
                }
                elseif ($value->gift_id==2){
                    $ds['袜子*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
                elseif ($value->gift_id==3){
                    $ds['鞋垫*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
                elseif ($value->gift_id==6){
                    $ds['项链*399']=$value->num;
                    $bonus=$bonus+399*$value->num;
                }
                elseif ($value->gift_id==13){
                    $ds['丝巾*399']=$value->num;
                    $bonus=$bonus+399*$value->num;
                }
//                elseif ($value->gift_id==14){
//                    $ds['护腰*799']=$value->num;
//                    $bonus=$bonus+799*$value->num;
//                }
//                elseif ($value->gift_id==15){
//                    $ds['秋衣秋裤 女M*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==16){
//                    $ds['秋衣秋裤 女L号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==17){
//                    $ds['秋衣秋裤 女XL号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==18){
//                    $ds['秋衣秋裤 女2XL号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==19){
//                    $ds['秋衣秋裤 男XL号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==20){
//                    $ds['秋衣秋裤 男2XL号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
//                elseif ($value->gift_id==21){
//                    $ds['秋衣秋裤 男3XL号*1699']=$value->num;
//                    $bonus=$bonus+1699*$value->num;
//                }
                elseif ($value->gift_id==22){
                    $ds['手环*199']=$value->num;
                    $bonus=$bonus+199*$value->num;
                }
                elseif ($value->gift_id==23){
                    $ds['T恤  M号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==24){
                    $ds['T恤 L号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==26){
                    $ds['T恤 XL号*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==28){
                    $ds['T恤 XXXL号*798']=$value->num;
                    $bonus=$bonus+798*$value->num;
                }
                elseif ($value->gift_id==29){
                    $ds['T恤XXL*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
                elseif ($value->gift_id==30){
                    $ds['T恤 4XL*799']=$value->num;
                    $bonus=$bonus+799*$value->num;
                }
            }
            $user=DB::table('user')->where('id',$v->uid)->first();
            $glory=DB::table('glory')->where('season',$request->season)->where('uid',$v->uid)->first();


            if ($user){
                $ss['昵称']=$user->nickname;
                $ss['手机号']=$user->phone;
                $ss['销售等级']=$user->level_name;
                if ($user->level_name=='代理公司'){
                    $ss['收货人']=$user->nickname;
                    $ss['收货人手机号']=$user->phone;
                    $ss['战区']=DB::table('service')->where('phone',$user->phone)->value('war_names').'战区';
                }else{
                    $ss['收货人']=$user->up_name;
                    $ss['收货人手机号']=$user->up_phone;
                    $ss['战区']=DB::table('service')->where('phone',$user->up_phone)->value('war_names').'战区';
                }
            }else{
                $ss['昵称']='出错了';
                $ss['手机号']='出错了';
                $ss['销售等级']='';

                $ss['收货人']='';
                $ss['收货人手机号']='';

            }

            if ($glory){
                $ss['奖学金']=$glory->bonus;
            }else{
                $ss['奖学金']=$bonus+$user->bonus;
            }

            $ss['兑换详情']=implode(',',$datas);
            $sss=$ds;
            $ssss[$k]=array_merge($ss,$sss);
//            $ss[$k]['pho']=implode(',',$datas);
        }
//        dd($ss);
        return $this->success_list($ssss);
    }

    public function exchange(Request $request){
        $phone=$request->phone;
        $season=DB::table('basic')->where('keys','=','season')->value('value');
        $user=DB::table('user')->where('phone',$phone)->first();
        $data=DB::table('gift_orders')->where('season','=',$season)->where('uid',$user->id)->get();
        foreach ($data as $k=>$v){
            $s=DB::table('gift_orders')->where('id',$v->id)->first();
            DB::table('gift_orders')->where('id',$s->id)->delete();
            DB::table('gift')->where('id',$s->gift_id)->increment('exchange_num','-'.$s->num);
            DB::table('user')->where('id',$user->id)->increment('bonus',$s->num*$s->bonus);
        }
        return $this->success([]);
    }


}
