<?php

namespace Modules\AdministrativeUnits\Controllers;

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

class Subcounty extends Adminauth
{
    protected $subcountyModel;
    protected $countyModel;
    protected $districtModel;
    protected $parishModel;
  
    public function __construct()
    {
        parent::init();
        $this->subcountyModel = new SubcountyModel();
        $this->countyModel = new CountyModel();
        $this->districtModel = new DistrictModel();
        $this->parishModel = new ParishModel();
    }

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

        $this->data['breadcrumb_title'] ="Sub Counties";

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

        return View("Modules\AdministrativeUnits\Views\subcounty\list", $this->data);
    }

    public function getSubcountyList()
    {
        $data = $this->subcountyModel->findAll();
        $response = [
            'success' => true,
            'data' => $data,
    ];
    return $this->response->setJSON($response);
    }

    public function parishes($id = null)
    {
        $items = $this->parishModel->findBySubCountyId($id);
        return $this->response->setJSON($items);
    }

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


        $result = $this->subcountyModel->save($data);
        if ($result) {
            $id = $this->subcountyModel->getInsertID();
            $response = [
                'success' => true,
                'msg' => "Subcounty has been saved",
                'subcounty_id' => $id,
            ];
        } else {
            $errors = $this->subcountyModel->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("subcounty_id");
            $data = [
                'subcounty_name' => $this->request->getVar("subcounty_name"),
                'county_id' => $this->request->getVar("county_id"),
            ];


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

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

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

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

            $response = [
                'success' => true,
                'msg' => "Subcounty has been deleted",
            ];
        } else {
            $errors = $this->subcountyModel->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);
    }
}
