<?php

namespace App\Http\Controllers;

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

class CarriersController extends Controller
{

    public function index()
    {
        if(!Gate::allows('承运商-查询')){ return redirect(url('/'));  }
            $carriers=Carrier::query()->orderBy('id', 'DESC')->paginate(50);
            return view('maintenance.carrier.index',['carriers'=>$carriers]);
    }

    public function create()
    {
        if(!Gate::allows('承运商-录入')){ return redirect(url('/'));  }
        return view('maintenance.carrier.create');
    }


    public function store(Request $request)
    {
        if(!Gate::allows('承运商-录入')){ return redirect(url('/'));  }
        $id=false;
        $this->validatorCarrier($request,$id)->validate();
        $carriers=$request->input('Carrier');
        if(Carrier::query()->create($carriers)){
            app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return redirect('maintenance/carrier')->with('successTip','新承运商“'.$request->input('Carrier.name').'”添加成功');
        };
    }

    public function edit($id)
    {
        if(!Gate::allows('承运商-编辑')){ return redirect(url('/'));  }
        $carrier=Carrier::query()->find($id);
        return view('maintenance.carrier.edit',['carrier'=>$carrier]);
    }

    public function update(Request $request, $id)
    {
        $this->validatorCarrier($request,$id)->validate();
        if(!Gate::allows('承运商-编辑')){ return redirect(url('/'));  }
        $data=$request->input('Carrier');
        $carrier=Carrier::query()->find($id);
        $carrier->fill($data);
        if ($carrier->save()){
            app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return redirect('maintenance/carrier')->with('successTip','承运商“'.$request->input('Carrier.name').'”修改成功');
        }
    }

    public function destroy($id)
    {
        if(!Gate::allows('承运商-删除')){ return redirect(url('/'));  }
        $carrier=Carrier::query()->find($id);
        app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($carrier),Auth::user()['id']);
        $result=$carrier->delete();
        return ['success'=>$result];
    }

    protected function validatorCarrier(Request $request,$id){
        if ($id){$name=$id;};
        $validator=Validator::make($request->input(),[
            'Carrier.name'=>['max:50','required',isset($name)?"unique:carriers,name,$name":'unique:carriers,name'],
            'Carrier.mobile'=>['required','regex:/^(\d{7,11})|(1[3|4|5|7|8][0-9]\d{4,8})$/',isset($name)?"unique:carriers,mobile,$name":'unique:carriers,mobile'],
            'Carrier.delivery_fee'=>'nullable|min:0|numeric|max:999999',
        ],[
            'digits_between'=>':attribute 最小一位，最大五十位',
            'required'=>':attribute 不应为空',
            'regex'=>':attribute 输入有误',
            'integer'=>':attribute 应为数值',
            'min'=>':attribute 不得为0或为负',
            'numeric'=>':attribute 必须为数字',
            'unique'=>':attribute 已存在',
            'max'=>':attribute 输入值过大',
        ],[
            'Carrier.name'=>'承运商名称',
            'Carrier.mobile'=>'承运商电话',
            'Carrier.delivery_fee'=>'送货费',
        ]);
        return $validator;
    }

}
