<?php

namespace App\Http\Controllers;

use App\Models\City;
use App\Models\State;
use Illuminate\Http\Request;

class CityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $sort_city = $request->sort_city;
        $sort_state = $request->sort_state;
        $cities_queries = City::query();
        if ($request->sort_city) {
            $cities_queries->where('name', 'like', "%$sort_city%");
        }
        if ($request->sort_state) {
            $cities_queries->where('state_id', $request->sort_state);
        }
        $cities = $cities_queries->orderBy('status', 'desc')->paginate(15);
        $states = State::where('status', 1)->get();

        return view('backend.settings.cities.index', compact('cities', 'states', 'sort_city', 'sort_state'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $city = new City;

        $city->name = $request->name;
        $city->state_id = $request->state_id;

        $city->save();

        flash(translate('City has been inserted successfully'))->success();

        return back();
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $city  = City::findOrFail($id);
        $states = State::where('status', 1)->get();
        return view('backend.settings.cities.edit', compact('city', 'states'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $city = City::findOrFail($id);
        $city->name = $request->name;
        $city->state_id = $request->state_id;

        $city->save();

        flash(translate('City has been updated successfully'))->success();
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        City::destroy($id);

        flash(translate('City has been deleted successfully'))->success();
        return back();
    }

    public function updateStatus(Request $request)
    {
        $city = City::findOrFail($request->id);
        $city->status = $request->status;
        if ($city->save()) {
            return 1;
        }
        return 0;
    }

    public function getCitites(Request $request)
    {
        $states = State::with('cities')->whereIn('id', $request->selectedStatesIds)->get();
        // $html = '<option value="">' . translate("Select State") . '</option>';
        $html = null;
        foreach ($states as $state) {
            foreach($state->cities as $city){
                $html .= '<option value="' . $city->id . '">' . $city->name . '</option>';
            }
        }

        return json_encode($html);
    }
}
