<?php

namespace App\Http\Controllers;

use App\Exports\Export;
use App\Package;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Maatwebsite\Excel\Facades\Excel;

class WeighExceptedController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function indexCreate(Request $request)
    {
        if(!Gate::allows('称重管理-查看异常')){ return redirect(url('/'));  }
        $paginate=$request->input('paginate');
        $weightExcepteds=Package::select('id','status','logistic_number','logistic_id','measuring_machine_id','weighed_at','weight','length','width','height','bulk','paper_box_id')->
        where('status','上传异常')->orWhere('status','测量异常')->orderBy('created_at','DESC');
        if ($paginate){
            $weightExcepteds=$weightExcepteds->paginate($paginate);
        }else{
            $weightExcepteds=$weightExcepteds->paginate(50);
        };
        return view('weight.weightExcepted.index',['weightExcepteds'=>$weightExcepteds,'view'=>'indexCreate']);
    }

    public function indexIssued(Request $request)
    {
        if(!Gate::allows('称重管理-查看异常')){ return redirect(url('/'));  }
        $paginate=$request->input('paginate');
        $weightExcepteds=Package::select('id','owner_id','logistic_number','created_at','delivery_number','batch_number','batch_rule','recipient','recipient_mobile','logistic_id')->
        where('status','下发异常')->orWhere('status','记录异常')->orWhere('status','已上传异常')->orderBy('created_at','DESC');
        if ($paginate){
            $weightExcepteds=$weightExcepteds->paginate($paginate);
        }else{
            $weightExcepteds=$weightExcepteds->paginate(50);
        };
        return view('weight.weightExcepted.index',['weightExcepteds'=>$weightExcepteds,'view'=>'indexIssued']);
    }

   public function export($id){
       if(!Gate::allows('称重信息-查看异常')){ return redirect(url('/'));  }
       if ($id==-1){
           $id=[];
           ini_set('max_execution_time',2500);
           ini_set('memory_limit','1526M');
           $packages=Package::select('id')->get();
           foreach ($packages as $package){
               array_push($id,$package->id);
           }
       }else $id = explode( ',',$id);
       if (!$id)return ;
       $weighExcepted=Package::find($id[0]);
       if (!$weighExcepted->status=="下发异常"){
           $row=[[
               'id'=>'ID',
               'logistic_number'=>'快递单号',
               'logistic_name'=>'承运商',
               'measuringMachine_name'=>'设备',
               'weigh_time'=>'称重时间',
               'weight'=>'重(KG)',
               'length'=>'长(CM)',
               'width'=>'宽(CM)',
               'height'=>'高(CM)',
               'bulk'=>'体积(CM³)',
               'paperBox_name'=>'纸箱',
               'genre'=>'异常类型',
               'created_at'=>'称重时间',
               'status'=>'异常类型',
           ]];
           $list=[];
           for ($i=0; $i<count($id);$i++){
               $weighExcepted=Package::find($id[$i]);
               $w=[
                   'id'=>isset($weighExcepted->id)?$weighExcepted->id:'',
                   'logistic_number'=>isset($weighExcepted->logistic_number)?$weighExcepted->logistic_number:'',
                   'logistic_name'=>isset($weighExcepted->logistic_name)?$weighExcepted->logistic_name:'',
                   'measuringMachine_name'=>isset($weighExcepted->measuringMachine_name)?$weighExcepted->measuringMachine_name:'',
                   'weigh_time'=>isset($weighExcepted->weigh_time)?$weighExcepted->weigh_time:'',
                   'weight'=>isset($weighExcepted->weight)?$weighExcepted->weight:'',
                   'length'=>isset($weighExcepted->length)?$weighExcepted->length:'',
                   'width'=>isset($weighExcepted->width)?$weighExcepted->width:'',
                   'bulk'=>isset($weighExcepted->bulk)?$weighExcepted->bulk:'',
                   'height'=>isset($weighExcepted->height)?$weighExcepted->height:'',
                   'paperBox_name'=>isset($weighExcepted->paperBox_name)?$weighExcepted->paperBox_name:'',
                   'genre'=>isset($weighExcepted->genre)?$weighExcepted->genre:'',
                   'created_at'=>isset($weighExcepted->created_at)?$weighExcepted->created_at:'',
                   'status'=>isset($weighExcepted->status)?$weighExcepted->status:''
               ];
               $list[$i]=$w;
           }
       }else{
           $row=[[
               'id'=>'ID',
               'logistic_number'=>'快递单号',
               'created_at'=>'下发时间',
               'delivery_number'=>'发货单号',
               'batch_number'=>'波次号',
               'batch_rule'=>'波次规则',
               'recipient'=>'收件人',
               'recipient_mobile'=>'收件人电话',
               'logistic_name'=>'承运商',
               'status'=>'异常类型',
           ]];
           $list=[];
           for ($i=0; $i<count($id);$i++){
               $weighExcepted=Package::find($id[$i]);
               $w=[
                   'id'=>isset($weighExcepted->id)?$weighExcepted->id:'',
                   'logistic_number'=>isset($weighExcepted->logistic_number)?$weighExcepted->logistic_number:'',
                   'logistic_name'=>isset($weighExcepted->logistic_name)?$weighExcepted->logistic_name:'',
                   'created_at'=>isset($weighExcepted->created_at)?$weighExcepted->created_at:'',
                   'delivery_number'=>isset($weighExcepted->delivery_number)?$weighExcepted->delivery_number:'',
                   'batch_number'=>isset($weighExcepted->batch_number)?$weighExcepted->batch_number:'',
                   'batch_rule'=>isset($weighExcepted->batch_rule)?$weighExcepted->batch_rule:'',
                   'recipient'=>isset($weighExcepted->recipient)?$weighExcepted->recipient:'',
                   'recipient_mobile'=>isset($weighExcepted->recipient_mobile)?$weighExcepted->recipient_mobile:'',
                   'status'=>isset($weighExcepted->status)?$weighExcepted->status:'',
               ];
               $list[$i]=$w;
           }
       }
       return Excel::download(new Export($row,$list), date('YmdHis', time()). '称重异常单.xlsx');
   }
}
