<?php

namespace App\Http\Controllers;


use App\Warehouse;
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 WarehouseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('仓库-查询')){ return redirect(url('/'));  }
        $warehouses=Warehouse::orderBy('id','desc')->paginate(35);
        return view('maintenance.warehouse.index',['warehouses'=>$warehouses]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!Gate::allows('仓库-录入')){ return redirect(url('/'));  }
        return view('maintenance.warehouse.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();
        $warehouse=new Warehouse($request->all());
        $warehouse->save();

        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return redirect('maintenance/warehouse/create')->with('successTip',"成功录入仓库“{$request->input('name')}”");
    }
    protected function validatorCreate(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:50', 'unique:warehouses'],
            'code' => ['nullable', 'string', 'max:50', 'unique:warehouses,code'],
        ]);
    }
    protected function validatorUpdate(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:50'],
            'code' => ['nullable', 'string', 'max:50'],
        ]);
    }
    /**
     * Display the specified resource.
     *
     * @param Warehouse $warehouse
     * @return Response
     */
    public function show(Warehouse $warehouse)
    {
        //
    }

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

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request
     * @param Warehouse $warehouse
     * @return Response
     */
    public function update(Request $request, Warehouse $warehouse)
    {
        if(!Gate::allows('仓库-编辑')){ return redirect(url('/'));  }
        $this->validatorUpdate($request->all())->validate();
        $warehouse->fill($request->all());
        $warehouse->update();
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
        return redirect('maintenance/warehouse/')->with('successTip',"成功修改仓库“{$warehouse['name']}”！");
    }

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