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

        $region           = Region::select('id', 'name', 'province_id', 'ordering')->where('id', $id)->first(); 
        $region->name     = request('name');
        $region->ordering = request('ordering');
        $region->save();
        return redirect('admin/regions/'.$region->province_id)->with('success','Region 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');
        Region::where('id',$id)->delete();
        return response()->json([
            'success' => 1,
            'message' => 'Data successfully deleted.'
        ]);
    } 
}
