<?php

namespace App\Http\Controllers;

use App\Depository;
use App\Http\Controllers\Api\thirdPart\flux\StoreController as FStoreController;
use App\Owner;
use App\Store;
use App\StoreItems;
use App\Warehouse;
use App\WMSReflectReceive;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class StoreController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(!Gate::allows('入库管理-快速入库-查询')){ return redirect(url('/'));  }
        $stores=Store::orderBy('id','DESC')->paginate(50);
        return view('store.index',['stores'=>$stores]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(!Gate::allows('入库管理-快速入库-录入')){ return redirect(url('/'));  }
        return view('store.fast.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        if(!Gate::allows('入库管理-快速入库-录入')){ return redirect(url('/'));  }
        $this->validator($request);
        $WMSReflectReceive=WMSReflectReceive::with('skus')->where('ASNNO',$request->input('asn_code'))->first();
        if (!$WMSReflectReceive)return redirect('store/fast/create')->with('successError','ASN编号不存在！');
        $warehouse=Warehouse::where('code',$WMSReflectReceive->WAREHOUSEID)->first();
        if (!$warehouse&&$WMSReflectReceive->WAREHOUSEID){
            $warehouse=new Warehouse([
                'name'=>$WMSReflectReceive->WAREHOUSEID,
                'code'=>$WMSReflectReceive->WAREHOUSEID
            ]);
            $warehouse->save();
        }
        $owner=Owner::where('code',$WMSReflectReceive->CUSTOMERID)->first();
        if (!$owner&&$WMSReflectReceive->CUSTOMERID){
            $owner=new Warehouse([
                'name'=>$WMSReflectReceive->CUSTOMERID,
                'code'=>$WMSReflectReceive->CUSTOMERID
            ]);
            $owner->save();
        }
        $store=Store::where('asn_code',$WMSReflectReceive->ASNNO)->first();
        if(!$store){
            $store=new Store([
                'asn_code'=>$WMSReflectReceive->ASNNO,
                'warehouse_id'=>$warehouse->id,
                'owner_id'=>$owner->id,
                'stored_method'=>'快速入库',
                'status'=>'未入库',
                'remark'=>$WMSReflectReceive->NOTES,
            ]);
            $store->save();

            $customDepository=(function()use($request){
                $customDepository=Depository::where('code',$request->input('depository_code'))->first();
                if($request->input('depository_code')){
                    if (!$customDepository){
                        $depository=new Depository([
                            'name'=>$request->input('depository_code'),
                            'code'=>$request->input('depository_code')
                        ]);
                        $depository->save();
                    }
                }
                return $customDepository;
            })();

            if ($WMSReflectReceive->skus){
                foreach ($WMSReflectReceive->skus as $sku){
                    $depository=(function()use($sku,$customDepository){
                        if($customDepository)return $customDepository;
                        $depository=Depository::where('code',$sku->LOTATT05)->first();
                        if (!$depository){
                            if (!$sku->LOTATT05)return $depository;
                            $depository=new Depository([
                                'name'=>$sku->LOTATT05,
                                'code'=>$sku->LOTATT05
                            ]);
                            $depository->save();
                        }
                        return $depository;
                    })();
                    $storeItem=new StoreItems([
                        'store_id'=>$store->id,
                        'asn_line_code'=>$sku->ASNLINENO,
                        'name'=>$sku->SKUDESCRC,
                        'sku'=>$sku->SKU,
                        'barcode'=>$sku->ALTERNATE_SKU1,
                        'quality'=>$request->input('quality'),
                        'status'=>'未入库',
                    ]);
                    if ($depository)$storeItem->depository_id=$depository->id;
                    $storeItem->save();
                }
            }
        }
        $store=Store::with('storeItems')->where('asn_code',$request->input('asn_code'))->first();
        $storeApi=new FStoreController();
        $result=$storeApi->accomplishToWMS($store,[
            'quality'=>$request->input('quality'),
            'depository_code'=>$request->input('depository_code'),
            'follow_code'=>$store['id'],
        ]);
        if ($result){
            $store->status='已入库';
            $store->save();
            if ($store->storeItems){
                $quality=$request->input('quality');
                $store->storeItems->each(function ($storeItem)use($quality){
                    $storeItem->status='已入库';
                    $storeItem->quality=$quality;
                    $storeItem->save();
                });
            }
            return redirect('store/fast/create')->with('successTip','成功！');
        }
        return redirect('store/fast/create')->with('successError','失败！请检查错误日志');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function show(Store $store)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function edit(Store $store)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Store $store)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Store  $store
     * @return \Illuminate\Http\Response
     */
    public function destroy(Store $store)
    {
        //
    }

    public function validator(Request $request){
        $validator=Validator::make($request->input(),[
            'asn_code'=>['required'],
            'quality'=>['required'],
            'depository_code'=>['nullable','string'],
        ],[
            'required'=>':attribute 为必填项',
            'unique'=>':attribute 已存在',
        ],[
            'asn_code'=>'ASN编号',
            'quality'=>'货物类型'
        ])->validate();
        return $validator;
    }
}
