<?php

namespace App\Http\Controllers;

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

class WaveController extends Controller
{

    public function __construct()
    {
        app()->bind("WaveService",WaveService::class);
    }

    // 主页
    public function index(Request $request){
        if(!Gate::allows('订单管理-波次-查询')){ return redirect(url('/'));  }
        $waveService = app("WaveService");
        $waves = $waveService->queryWave($request);
        //$waveStatus = $waveService->getWaveStatus();
        $param =  $waveService->getPageParameter($request);
        $search = $waveService->getSearchCondition($request);
        foreach ($waves as $index=>$wave){
            $wave->pickerPrint = '';
            $wave->pickerPrintTime = '';
            $wave->expressPrinting = '';
            $wave->expressPrintTime = '';
            if(!is_null($wave->userdefine1)){
                $str = $wave->userdefine1;
                $index = strpos($str,'-PK');
                $wave->pickerPrint = substr($str,0,$index);
                $wave->pickerPrintTime = substr($str,$index+3);
            }
            if(!is_null($wave->userdefine2)){
                $str = $wave->userdefine2;
                if(strpos($str,'-EX')){
                    $index = strpos($str,'-EX');
                    $wave->expressPrinting = substr($str,0,$index);
                    $wave->expressPrintTime = substr($str,$index+3);
                }else if(strpos($str,'-Auto')){
                    $index = strpos($str,'-Auto');
                    $wave->expressPrinting = substr($str,0,$index);
                    $wave->expressPrintTime = substr($str,$index+5);
                }
            }
        }
        return view("order/wave/search",compact('waves','param','search'));
    }

    public function create(){

    }
    // todo
    public function store(Request $request){

    }

    public function edit($id){

    }

    public function update(Request $request, $id){

    }

    public function destroy($id){

    }

    public function cancelPrinting(Request $request){
        if(!Gate::allows('订单管理-波次-重置打印')){ return ['success'=>false,'fail_info'=>'没有权限，请联系管理员'];  }
        $waveService = app("WaveService");
        $meg = ['success'=>false];
        $ids = $request->input("ids");
        $meg = $waveService->cancelPrint($ids);
        if($meg['fail_info']){
            return $meg;
        }
        $this->log(__METHOD__,'重置打印标记'.__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return $meg;
    }

    //
    public function exportExcelOnParams(Request $request){
        $waveService = app("WaveService");
        ini_set('max_execution_time',2500);
        ini_set('memory_limit','1526M');

        $waves  = $waveService->downloadExec($request);
        $row = $waveService->getExcelFromHead();

        $data = [];
        foreach ($waves as $index=>$wave){
            $arr= [
                'waveNo' => $wave->waveno,
                'codename_c' => $wave->codename_c,
                'waveRule' => $wave->waverule,
                'descr' => $wave->descr,
                'descr_c' => $wave->descr_c,
                'addWho' => $wave->addwho,
                'addTime' => $wave->addtime,
                'pickerPrint'=>'',
                'pickerPrintTime'=>'',
                'expressPrinting'=>'',
                'expressPrintTime'=> ''
            ];
            if(!is_null($wave->userdefine1)){
                $str = $wave->userdefine1;
                $index = strpos($str,'-PK');
                $arr['pickerPrint'] = substr($str,0,$index);
                $arr['pickerPrintTime']= substr($str,$index+3);
            }
            if(!is_null($wave->userdefine2)){
                $str = $wave->userdefine2;
                if(strpos($str,'-EX')){
                    $index = strpos($str,'-EX');
                    $arr['expressPrinting'] =substr($str,0,$index);
                    $arr['expressPrintTime'] = substr($str,$index+3);
                }else if(strpos($str,'-Auto')){
                    $index = strpos($str,'-Auto');
                    $arr['expressPrinting'] =substr($str,0,$index);
                    $arr['expressPrintTime'] = substr($str,$index+5);
                }

            }
            $data[] = $arr;
        }
        return Excel::download(new Export($row,$data),date('YmdHis', time()).'-波次表.xlsx');
    }

}
