<?php

namespace Modules\AdministrativeUnits\Controllers;

use App\Core\AuthenticatedController;
use Modules\AdministrativeUnits\Models\ConstituencyModel;

class Constituency extends AuthenticatedController
{
    protected $constituencyModel;
  
    public function __construct()
    {
        parent::__construct();
        $this->constituencyModel = new ConstituencyModel();
        
    }

    public function index()
    {
        $this->data['constituencies'] = $this->constituencyModel->findAll();

        return $this->render("Manage/Constituencies/List","Constituencies");
    }

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


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


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

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

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

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

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