<?php

namespace Modules\AdministrativeUnits\Controllers;

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

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

    public function index()
    {

        //Page title & Path
        $this->data['title']   = 'Parishes';
        $this->data['bcrumb'] = 'Parishes';

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

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

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

    public function villages($id = null)
    {
        $items = $this->villageModel->findByParishId($id);
        return $this->response->setJSON($items);
    }

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


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


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

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

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

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

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