<?php
namespace App\Http\Controllers\Admin;
use App\Models\Province;
use App\Models\City;
use App\Models\Region;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request,$regionId)
    {
        if(auth()->user()->user_type != 1){
           return redirect('/');
        }
        $region = Region::select('id', 'name', 'province_id')->where('id',$regionId)->first();
        if($request->ajax()){
            return datatables()->of(City::select('id', 'name', 'region_id', 'ordering')->where('region_id',$regionId))
            ->addColumn('actions', function($row){
                return '<span class="action-links"><a class="uk-text-primary uk-edit" href="'.url('admin/city/'.$row->id.'/edit').'" uk-tooltip="Edit City"><i class="fa-solid fa-pen-to-square"></i></a>  <a data-id="'.$row->id.'" class="uk-text-danger delete-city" href="javascript:void(0)" uk-tooltip="Delete City"><i class="fa-solid fa-trash"></i></a></span>';
            })
            ->rawColumns(['actions'])->toJson();
        }
        else{
            $this->bladeContent['title']      = '('.$region->name.') - City'; 
            $this->bladeContent['active']     = 'provinces';
            $this->bladeContent['regionId']   = $regionId;
            $this->bladeContent['provinceId']   = $region->province_id;
            return view('admin.city.index',$this->bladeContent);
        }
    }
    
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request,$regionId)
    {
        if(auth()->user()->user_type != 1){
           return redirect('/');
        }
        $this->bladeContent['region']     = Region::select('id', 'province_id')->where('id', $regionId)->first();
        $this->bladeContent['title']      = 'Add City';
        $this->bladeContent['active']     = 'provinces';
        $this->bladeContent['regionId']   = $regionId;
        return view ('admin.city.create', $this->bladeContent);
    }
    
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        
        $city              = new City();
        $city->province_id = request('province_id');
        $city->region_id   = request('region_id');
        $city->name        = request('name');
        $city->ordering    = request('ordering');
        $city->save();
        return redirect('admin/city/'.$city->region_id)->with('success','City successfully created.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function show(Request $request, $slug)
    {
       
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $this->bladeContent['title']   = 'Edit City';
        $this->bladeContent['active']  = 'provinces';
        $this->bladeContent['data']    =  City::select('id', 'name', 'region_id', 'ordering')->where('id', $id)->first(); 
        return view('admin.city.edit', $this->bladeContent);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request,$id)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }

        $city           = City::select('id', 'name', 'region_id', 'ordering')->where('id', $id)->first(); 
        $city->name     = request('name');
        $city->ordering = request('ordering');
        $city->save();
        return redirect('admin/city/'.$city->region_id)->with('success','City successfully updated.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Users  $users
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if(auth()->user()->user_type != 1){
            return redirect('/');
        }
        $id = request('id');
        City::where('id',$id)->delete();
        return response()->json([
            'success' => 1,
            'message' => 'Data successfully deleted.'
        ]);
    } 
}
