<?php

namespace App\Http\Controllers;

use App\Logistic;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class LogisticController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('物流公司-查询')){ return redirect(url('/'));  }
        $logistics=Logistic::orderBy('id','desc')->paginate(35);
        return view('maintenance.logistic.index',['logistics'=>$logistics]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!Gate::allows('物流公司-录入')){ return redirect(url('/'));  }
        return view('maintenance.logistic.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('物流公司-录入')){ return redirect(url('/'));  }
        $this->validatorCreate($request->all())->validate();
        $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
        $logistic=new Logistic($request->all());
        $logistic->save();

        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return redirect('maintenance/logistic/create')->with('successTip',"成功录入物流公司“{$request->input('name')}”");
    }
    protected function validatorCreate(array $data)
    {
        return Validator::make($data, [
            'type' => ["required"],
            'name' => ['required', 'string', 'max:50', 'unique:logistics'],
            'code' => ['nullable', 'string', 'max:50', 'unique:logistics,code'],
            'delivery_fee' => ['nullable', 'numeric', 'min:0'],
        ]);
    }
    protected function validatorUpdate(array $data,$id)
    {
        return Validator::make($data, [
            'type' => ["required"],
            'name' => ['required', 'string', 'max:50',"unique:logistics,name,$id"],
            'code' => ['nullable', 'string', 'max:50',"unique:logistics,code,$id"],
            'delivery_fee' => ['nullable', 'numeric', 'min:0'],
        ]);
    }
    /**
     * Display the specified resource.
     *
     * @param Logistic $logistic
     * @return Response
     */
    public function show(Logistic $logistic)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param Logistic $logistic
     * @return Response
     */
    public function edit(Logistic $logistic)
    {
        if(!Gate::allows('物流公司-编辑')){ return redirect(url('/'));  }
        return view('maintenance.logistic.edit',['logistic'=>$logistic]);
    }

    public function update(Request $request, Logistic $logistic)
    {
        if(!Gate::allows('物流公司-编辑')){ return redirect(url('/'));  }
        $this->validatorUpdate($request->all(),$logistic->id)->validate();
        $request->offsetSet("is_bunched",$request->input("is_bunched") ? 'Y' : 'N');
        $logistic->fill($request->all());
        $logistic->update();
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return redirect('maintenance/logistic/')->with('successTip',"成功修改物流公司“{$logistic['name']}”！");
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Logistic $logistic
     * @return array|Response
     * @throws Exception
     */
    public function destroy(Logistic $logistic)
    {
        if(!Gate::allows('物流公司-删除')){ return redirect(url('/'));  }
        app('LogService')->log(__METHOD__,__FUNCTION__,$logistic->toJson(),Auth::user()['id']);
        $re=$logistic->delete();
        return ['success'=>$re];
    }

    public function get()
    {
        $type = \request("type");
        $column = ['id','name'];
        if (!$type)$column[] = "type";
        $logistics = app("LogisticService")->getSelection($column,$type);
        return ["success"=>true,"data"=>$logistics];
    }
}
