<?php

namespace Modules\AdministrativeUnits\Controllers;

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

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

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

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

        return View("Modules\AdministrativeUnits\Views\\village\list", $this->data);
    }
    public function getVillageList()
    {
        $data = $this->villageModel->findAll();
        $response = [
            'success' => true,
            'data' => $data,
        ];
        return $this->response->setJSON($response);
    }

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


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


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

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

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

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

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