<?php

namespace App\Controllers;

use App\Controllers\BaseController;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Modules\AdministrativeUnits\Models\DistrictModel;
use Modules\AdministrativeUnits\Models\VillageModel;
use Modules\AdministrativeUnits\Models\CountyModel;
use Modules\AdministrativeUnits\Models\SubcountyModel;
use Modules\AdministrativeUnits\Models\ParishModel;

class SelectDistrict extends BaseController
{

    public function __construct()
    {
        $this->districtModel 			= new DistrictModel();
        $this->villageModel 			= new VillageModel();
        $this->countyModel 				= new CountyModel();
        $this->subcountyModel 			= new SubcountyModel();
        $this->parishModel 				= new ParishModel();

        // restrict this controller to admins only
        //$this->adminauth->restrict('admin');
    }

    public function index() {
        helper(['form', 'url']);

        $data['districts'] = $this->districtModel->findAll();
        return view('dropdown-list', $data);
    }

    public function getDistrict()
    {
        $data['districts'] = $this->districtModel->findAll();
        $this->data['states']       = $this->stateModel->getAll();
        $this->data['cities']       = $this->cityModel->getAll();
    }

    public function counties($id = null)
    {
        $counties = $this->countyModel->findByDistrictId($id);
        return $this->response->setJSON([
            'csrfToken' => csrf_hash(),
            'data' => $counties
        ]);
    }

    public function subcounties($id = null)
    {
        $items = $this->subcountyModel->findByCountyId($id);
        return $this->response->setJSON([
            'csrfToken' => csrf_hash(),
            'data' => $items
        ]);
    }

    public function parishes($id = null)
    {
        $items = $this->parishModel->findBySubCountyId($id);
        return $this->response->setJSON([
            'csrfToken' => csrf_hash(),
            'data' => $items
        ]);
    }

    public function villages($id = null)
    {
        $items = $this->villageModel->findByParishId($id);
        return $this->response->setJSON([
            'csrfToken' => csrf_hash(),
            'data' => $items
        ]);
    }
}