<?php

namespace Modules\AdministrativeUnits\Controllers;

use App\Controllers\Auth\AdminAuthController as Adminauth;
use Modules\AdministrativeUnits\Models\DistrictModel;
use Modules\AdministrativeUnits\Models\CountyModel;
use Modules\AdministrativeUnits\Models\SubcountyModel;

class County extends Adminauth
{
    protected $districtModel;
    protected $countyModel;
    protected $subcountyModel;

    public function __construct()
    {
        parent::init();
        $this->districtModel = new DistrictModel();
        $this->countyModel = new CountyModel();
        $this->subcountyModel = new SubcountyModel();
    }

    public function index()
    {
        //Page title & Path
        $this->data['title']   = 'Counties';
        $this->data['bcrumb'] = 'Counties';

        $this->data['counties'] = $this->countyModel->findAll();
        $this->data['districts'] = $this->districtModel->findAll();

        return View("Modules\AdministrativeUnits\Views\county\list", $this->data);
    }
    public function getCountyList()
    {
        $data = $this->countyModel->findAll();
        $response = [
                'success' => true,
                'data' => $data,
        ];
    
        return $this->response->setJSON($response);
    }
    public function subcounties($id = null)
    {
        $items = $this->subcountyModel->findByCountyId($id);
        return $this->response->setJSON($items);
    }

    public function add()
    {
        $data = [
            'county_name' => $this->request->getVar("county_name"),
            'district_id' => $this->request->getVar("district_id"),
        ];


        $result = $this->countyModel->save($data);
        if ($result) {
            $id = $this->countyModel->getInsertID();
            $response = [
                'success' => true,
                'msg' => "County has been saved",
                'county_id' => $id,
            ];
        } else {
            $errors = $this->countyModel->errors();
            $msg = '<ul class="alert alert-danger">';
            foreach ($errors as $error){
                $msg .='<li>'.$error.'</li>';
            }
            $msg .='</ul>';
            $response = [
                'success' => false,
                'msg' => $msg,
            ];
        }

        return $this->response->setJSON($response);
    }


    public function edit($id = null)
    {
        if($id == null)
        {
            $id = $this->request->getVar("county_id");
            $data = [
                'county_name' => $this->request->getVar("county_name"),
                'district_id' => $this->request->getVar("district_id"),
            ];


            $result = $this->countyModel->update($id,$data);
            if ($result) {

                $response = [
                    'success' => true,
                    'msg' => "County has been updated",
                    'county_id' => $id,
                ];
            } else {
                $errors = $this->countyModel->errors();
                $msg = '<ul class="alert alert-danger">';
                foreach ($errors as $error){
                    $msg .='<li>'.$error.'</li>';
                }
                $msg .='</ul>';
                $response = [
                    'success' => false,
                    'msg' => $msg,
                    'county_id' => $id,
                ];
            }

            return $this->response->setJSON($response);
        }
        else
        {
            $county = $this->countyModel->find($id);
            return $this->response->setJSON($county);
        }
    }

    public function delete($id)
    {
        $result = $this->countyModel->delete($id);
        if ($result) {

            $response = [
                'success' => true,
                'msg' => "County has been deleted",
            ];
        } else {
            $errors = $this->countyModel->errors();
            $msg = '<ul class="alert alert-danger">';
            foreach ($errors as $error){
                $msg .='<li>'.$error.'</li>';
            }
            $msg .='</ul>';
            $response = [
                'success' => false,
                'msg' => $msg,
            ];
        }

        return $this->response->setJSON($response);
    }
}
