<?php

namespace App\Http\Controllers;

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

class MeasuringMachineController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if(!Gate::allows('测量设备-查询')){ return redirect(url('/'));  }
        $measuringMachines=MeasuringMachine::orderBy('id','DESC')->paginate(50);
        return view('maintenance.measuringMachine.index',['measuringMachines'=>$measuringMachines]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(!Gate::allows('测量设备-录入')){ return redirect(url('/'));  }
        return view('maintenance.measuringMachine.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('测量设备-录入')){ return redirect(url('/'));  }
        $id=false;
        $this->validator($request,$id)->validate();
        MeasuringMachine::create($request->input());
        return redirect('maintenance/measuringMachine')->with('successTip','新设备“'.$request->input('name').'”添加成功');
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(!Gate::allows('测量设备-编辑')){ return redirect(url('/'));  }
        $measuringMachine=MeasuringMachine::find($id);
        return view('maintenance.measuringMachine.edit',['measuringMachine'=>$measuringMachine]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('测量设备-编辑')){ return redirect(url('/'));  }
        $this->validator($request,$id)->validate();
        $measuringMachine=MeasuringMachine::find($id);
        $measuringMachine->fill($request->input());
        $measuringMachine->save();
        return redirect('maintenance/measuringMachine')->with('successTip','新设备“'.$request->input('name').'”更新成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     */
    public function destroy($id)
    {
        if(!Gate::allows('测量设备-删除')){ return redirect(url('/'));  }
        $measuringMachine=MeasuringMachine::find($id)->delete();
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($measuringMachine),Auth::user()['id']);
        if ($measuringMachine)return ['success'=>true];
    }

    public function validator(Request $request,$id){
        if ($id){$code=$id;}
        $validator=Validator::make($request->input(),[
            'name'=>['required','max:50'],
            'code'=>['required','max:50',isset($code)?"unique:measuring_machines,code,$code":'unique:measuring_machines,code'],
        ],[
            'required'=>':attribute 为必填项',
            'unique'=>':attribute 已存在',
        ],[
            'name'=>'设备名',
            'code'=>'设备代码',
        ]);
        return $validator;
    }
}
