<?php

namespace Modules\AdministrativeUnits\Controllers;

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

class District extends Adminauth
{
    public $districtModel;
    public $countyModel;

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

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

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

        return view("Modules\AdministrativeUnits\Views\district\list",$this->data);
    }
    public function getDistrictList()
    {
        $data = $this->districtModel->findAll();
        $response = [
                'success' => true,
                'data' => $data,
        ];
    
        return $this->response->setJSON($response);
    }

    public function counties($id = null)
    {
        $counties = $this->countyModel->findByDistrictId($id);
        return $this->response->setJSON($counties);
    }

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


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


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

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

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

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

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