<?php

namespace App\Models;

use CodeIgniter\Model;

class AreaModel extends Model
{
    protected $table            = 'tbl_area';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useSoftDeletes   = false;
    protected $protectFields    = false;
    protected $allowedFields    = [];

    protected bool $allowEmptyInserts = false;
    protected bool $updateOnlyChanged = true;

    protected array $casts = [];
    protected array $castHandlers = [];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];



    public function _set_query_subdistrict()
    {

        $this->where('UPPER(type)',"SUBDISTRICT");
        if(!empty($this->search)){
            $this->groupStart();
            $this->like('UPPER(name)',$this->search);
            $this->groupEnd();
        }
    }

    public function getAllDataSubdistrict()
    {
        $data = [];

        if ($this->columns[$this->order[0]['column']]['data'] == 'no') {
            $this->columns[$this->order[0]['column']]['data'] = $this->table . '.id';
        }

        //all data
        $this->select($this->table . '.id');
        $this->countData = $this->countAllResults();

        //filtered
        $this->select($this->table . '.id');
        $this->_set_query_subdistrict();
        $this->filterData = $this->countAllResults();

        //view data
        $this->_set_query_subdistrict();
        $this->orderBy($this->columns[$this->order[0]['column']]['data'], $this->order[0]['dir']);
        $this->limit( intval($this->limit), intval($this->start));
        $getData = $this->findAll();
        $no      = $this->start;
        
        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {
                $row->no = ++$no;
                
                $row->option = '<p class="m-0 text-nowrap">';

                if(getAccessMenu(2,"is_delete", session()->get('role_id'))){

                    $row->option = $row->option . '<a href="javascript:void(0)" title="Ubah data" data-link="' . base_url() . 'area/subdistrict/edit/' . $row->id . '" class=" datatable-edit me-2"><i class="bx bx-edit text-warning" style="font-size: 20px;"></i></a>';
                }


                if(getAccessMenu(2,"is_update", session()->get('role_id'))){

                    $row->option = $row->option . ' <a href="javascript:void(0)" data-id="' . $row->id . '" title="Hapus data" data-link="' . base_url() . 'area/subdistrict/delete" class=" datatable-delete"><i class="bx bx-trash text-danger" style="font-size: 20px;"></i></a>';
                }

                $row->option    = $row->option . '</p>';


                $data[]      = $row;
            }
        }

        $result = array(
            "draw"            => $this->draw,
            "recordsTotal"    => $this->countData,
            "recordsFiltered" => $this->filterData,
            "data"            => $data,
        );

        return json_encode($result);
    }



    public function _set_query_village()
    {

        $this->where('UPPER(type)',"VILLAGE");
        if(!empty($this->search)){
            $this->groupStart();
            $this->like('UPPER(name)',$this->search);
            $this->groupEnd();
        }
    }

    public function getAllDataVillage()
    {
        $data = [];

        if ($this->columns[$this->order[0]['column']]['data'] == 'no') {
            $this->columns[$this->order[0]['column']]['data'] = $this->table . '.id';
        }

        //all data
        $this->select($this->table . '.id');
        $this->countData = $this->countAllResults();

        //filtered
        $this->select($this->table . '.id');
        $this->_set_query_village();
        $this->filterData = $this->countAllResults();

        //view data
        $this->_set_query_village();
        $this->orderBy($this->columns[$this->order[0]['column']]['data'], $this->order[0]['dir']);
        $this->limit( intval($this->limit), intval($this->start));
        $getData = $this->findAll();
        $no      = $this->start;
        
        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {

                $row->parent_name = $this->find($row->parent_id)->name;

                $row->no = ++$no;
                
                $row->option = '<p class="m-0 text-nowrap">';

                if(getAccessMenu(2,"is_delete", session()->get('role_id'))){

                    $row->option = $row->option . '<a href="javascript:void(0)" title="Ubah data" data-link="' . base_url() . 'area/village/edit/' . $row->id . '" class=" datatable-edit me-2"><i class="bx bx-edit text-warning" style="font-size: 20px;"></i></a>';
                }


                if(getAccessMenu(2,"is_update", session()->get('role_id'))){

                    $row->option = $row->option . ' <a href="javascript:void(0)" data-id="' . $row->id . '" title="Hapus data" data-link="' . base_url() . 'area/village/delete" class=" datatable-delete"><i class="bx bx-trash text-danger" style="font-size: 20px;"></i></a>';
                }

                $row->option    = $row->option . '</p>';


                $data[]      = $row;
            }
        }

        $result = array(
            "draw"            => $this->draw,
            "recordsTotal"    => $this->countData,
            "recordsFiltered" => $this->filterData,
            "data"            => $data,
        );

        return json_encode($result);
    }

    

    public function getAllSubdistirctName()
    {

        $this->select('id');
        $this->where('UPPER(type)',"SUBDISTRICT");
        $this->countData    = $this->findAll();

        $this->select('id');
        $this->where('UPPER(type)',"SUBDISTRICT");
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->filterData = $this->countAllResults();

        $this->where('UPPER(type)',"SUBDISTRICT");
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->limit(intval($this->limit), intval($this->start));
        $getData        = $this->findAll();

        $data           = [];
        $no             = $this->start;

        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {
                $row->no    = $no + 1;
                $data[]     = $row;
            }
        }

        return json_encode(array(
            'recorsTotal'     => $this->countData,
            'recordsFiltered' => $this->filterData,
            'data'            => $data
        ));
    }


    public function getAllVillageName()
    {



        $this->select('id');
        $this->where('UPPER(type)',"VILLAGE");
        if(!empty($this->parentId)){
            $this->where('UPPER(parent_id)',$this->parentId);
        }
        $this->countData    = $this->findAll();

        $this->select('id');
        $this->where('UPPER(type)',"VILLAGE");
        if(!empty($this->parentId)){
            $this->where('UPPER(parent_id)',$this->parentId);
        }
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->filterData = $this->countAllResults();


        $this->where('UPPER(type)',"VILLAGE");
        if(!empty($this->parentId)){
            $this->where('UPPER(parent_id)',$this->parentId);
        }
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->limit(intval($this->limit), intval($this->start));
        $getData        = $this->findAll();

        $data           = [];
        $no             = $this->start;

        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {
                $row->no    = $no + 1;
                $data[]     = $row;
            }
        }

        return json_encode(array(
            'recorsTotal'     => $this->countData,
            'recordsFiltered' => $this->filterData,
            'data'            => $data
        ));
    }




}
