<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class ProcessMethodController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('作业类型-查询')){ return redirect('denied');  }
        $methods = app('ProcessMethodService')->paginate();
        return response()->view("maintenance.processMethod.index",compact("methods"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!Gate::allows('作业类型-录入')){ return redirect('denied');  }
        $units = app('UnitService')->getSelection();
        return response()->view("maintenance.processMethod.create",compact("units"));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('作业类型-录入')){ return redirect('denied');  }
        $this->validator($request->input())->validate();
        app('ProcessMethodService')->create([
            "name"=>$request->input("name"),
            "unit_id"=>$request->input("unit_id"),
            "unit_price"=>$request->input("unit_price"),
        ]);
        return response()->redirectTo("maintenance/processMethod")->with("successTip","成功创建项目组“".$request->input("name")."”");
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!Gate::allows('作业类型-编辑')){ return redirect('denied');  }
        $method = app('ProcessMethodService')->find($id);
        $units = app('UnitService')->getSelection();
        return response()->view('maintenance.processMethod.create',compact("method","units"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('作业类型-编辑')){ return redirect('denied');  }
        $this->validator($request->input(),$id)->validate();
        $result = app('ProcessMethodService')->update(["id"=>$id],[
            "name"=>$request->input("name"),
            "unit_id"=>$request->input("unit_id"),
            "unit_price"=>$request->input("unit_price"),
        ]);
        if ($result == 1){
            return response()->redirectTo("maintenance/processMethod")->with("successTip","成功修改项目组“".$request->input("name")."”的信息");
        }
        return response()->view("exception.default",["code"=>"509"]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return array
     */
    public function destroy($id)
    {
        if(!Gate::allows('作业类型-删除')){ return ["success"=>false,"data"=>"无权操作！"];  }
        $result = app('ProcessMethodService')->destroy($id);
        if ($result == 1)return ["success"=>true];
        return ["success"=>false,"data"=>"删除了“".$result."”行"];
    }

    private function validator(array $params, $id = null)
    {
        return Validator::make($params,[
            'name'=>['required',$id?"unique:process_methods,name,$id":'unique:process_methods,name','max:20'],
            "unit_id" => ["nullable",'numeric'],
            "unit_price" => ["nullable",'numeric','min:0'],
        ],[
            'required'=>':attribute 为必填项',
            'max'=>':attribute 字符过多或输入值过大',
            'unique'=>':attribute 已存在',
            'numeric'=>':attribute 必须为数字',
            'min'=>':attribute 数值过小',
        ],[
            'name'=>'作业类型名称',
            'unit_id'=>'作业类型单位',
            'unit_price'=>'作业类型单价',
        ]);
    }
}
