<?php

namespace App\Http\Controllers;

use App\City;
use App\Http\Requests\ForeignHaiRobotic_taskUpdateRequest;
use App\Http\Requests\TestAaRequest;
use App\Province;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class CitiesController extends Controller
{
    public function index(Request $request)
    {
        if(!Gate::allows('城市-查询')){ return redirect(url('/'));  }
        $cities=City::orderBy('id', 'DESC')->paginate(50);
        return view('maintenance.city.index',['cities'=>$cities]);
    }

    public function create()
    {
        if(!Gate::allows('城市-录入')){ return redirect(url('/'));  }
        $provinces=Province::get();
        return view('maintenance.city.create',['provinces'=>$provinces]);
    }

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

    public function edit($id)
    {
        if(!Gate::allows('城市-编辑')){ return redirect(url('/'));  }
        $city=City::find($id);
        $provinces=Province::get();
        return view('maintenance.city.edit',['city'=>$city,'provinces'=>$provinces]);
    }

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

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

    public function validatorCities(Request $request,$id){
        if ($id){$name=$id;}
        $validator=Validator::make($request->input(),[
            'City.name'=>['required','max:50',isset($name)?"unique:cities,name,$name":'unique:cities,name'],
        ],[
            'required'=>':attribute 为必填项',
            'unique'=>':attribute 已存在',
        ],[
            'City.name'=>'城市名称',
        ]);
        return $validator;
    }

    public function get(){
        return ["success"=>true,"data"=>City::query()->select("id","name","province_id")->with("province")->get()];
    }
}
