<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\City;
use App\Models\Country;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class CityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data['lists'] = City::with(['state','country'])->latest()->paginate(10);
        // dd( $data['lists']);
        $data['title'] ="City Master";
        return view('admin.city.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data['title'] ="Add New City";
        $data['countries'] = Country::whereStatus('1')->pluck('country_name','id');
        return view('admin.city.create',$data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'name'         => ['required','unique:cities,name'],
            'country_id'   => ['required',Rule::exists('countries', 'id')],
            'state_id'     => ['required',Rule::exists('states', 'id')],
            'status'       => ['required'],
        ]);
        $input = $request->only(['name','country_id','state_id','status']);        
        City::create($input);
        session()->flash('success', 'City Created Successfully');
        return redirect()->route('admin.city.create');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Admin\CityController  $cityController
     * @return \Illuminate\Http\Response
     */
    public function show(CityController $cityController)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Admin\CityController  $cityController
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data['editValue'] = City::findOrFail($id);
        $data['title'] ="Edit City";
        $data['countries'] = Country::whereStatus('1')->pluck('country_name','id');
        return view('admin.city.create', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Admin\CityController  $cityController
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $request->validate([
            'name'         => ['required','unique:cities,name,'.$id.''],
            'country_id'   => ['required',Rule::exists('countries', 'id')],
            'state_id'     => ['required',Rule::exists('states', 'id')],
            'status'       => ['required'],
        ]);

        $spc =  City::findOrFail($id);
        $spc->name        = $request->name;
        $spc->country_id  = $request->country_id;
        $spc->state_id    = $request->state_id;
        $spc->status    = $request->status;
        $spc->save();

        session()->flash('success', 'Updated Successfully');
        return redirect()->route('admin.city.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Admin\CityController  $cityController
     * @return \Illuminate\Http\Response
     */
    public function destroy(City $city)
    {
        $city->delete();
        return response()->json(['status' => 200, 'message' => 'deleted successfully!']);
    }
}
