<?php

namespace App\Http\Controllers;

use App\LaborCompany;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class LaborCompanyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('劳务所-查询')){ return redirect(url('/'));  }
        $laborCompanys=LaborCompany::orderBy('id','desc')->paginate(35);
        return view('maintenance.laborCompany.index',['laborCompanys'=>$laborCompanys]);
    }

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

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

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

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

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