<?php

namespace App\Models\Referensi;

use App\Models\BaseModel;

class PdkkakoModel extends BaseModel
{
    protected $table            = 'pdk_kako';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['id_prov', 'nama_kako', 'nama_alt', 'kode'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_kako as c');
        $builder->select('c.id, c.id_prov, c.nama_kako, c.nama_alt, c.kode, m.nama_prov');
        $builder->join('pdk_prov as m', 'm.id = c.id_prov', 'left');
        $builder->where('c.id', $id);

        return $builder->get()->getRow();
    }

    function get_rows2($id_prov, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_kako as c');
        $builder->select('c.id, c.id_prov, c.nama_kako, c.nama_alt, c.kode, m.nama_prov');
        $builder->join('pdk_prov as m', 'm.id = c.id_prov', 'left');
        
        if ($filterRules) {
            $filterRules = json_decode($filterRules);

            foreach ($filterRules as $filter) {
                $value = $filter->value;
                $pecah = explode(';', $value);
                if (count($pecah) > 1) {
                    $builder->groupStart();
                    foreach ($pecah as $p) {
                        if (trim($p) <> '') {
                            $field = $filter->field;
                            if (trim($filter->field) == 'nama_kako') {
                                $field = 'c.nama_kako';
                            }
                            if (trim($filter->field) == 'nama_alt') {
                                $field = 'c.nama_alt';
                            }
                            if (trim($filter->field) == 'kode') {
                                $field = 'c.kode';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_kako') {
                        $field = 'c.nama_kako';
                    }
                    if (trim($filter->field) == 'nama_alt') {
                        $field = 'c.nama_alt';
                    }
                    if (trim($filter->field) == 'kode') {
                        $field = 'c.kode';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }

        if ($sortOrder) {
            $builder->orderBy($sortOrder);
        } else {
            if ($this->orderBy <> '') {
                $builder->orderBy($this->orderBy);
            } else {
                $builder->orderBy($this->primaryKey, $this->sort);
            }
        }

        $builder->limit($limit, $start);
        $builder->where('c.id_prov', $id_prov);
        
        return $builder->get()->getResult();
    }

    public function get_total2($id_prov, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_kako as c');
        $builder->select('c.id, c.id_prov, c.nama_kako, c.nama_alt, c.kode, m.nama_prov');
        $builder->join('pdk_prov as m', 'm.id = c.id_prov', 'left');

        if ($filterRules) {
            $filterRules = json_decode($filterRules);
            foreach ($filterRules as $filter) {
                $value = $filter->value;
                $pecah = explode(';', $value);
                if (count($pecah) > 1) {
                    $builder->groupStart();
                    foreach ($pecah as $p) {
                        if (trim($p) <> '') {
                            $field = $filter->field;
                            if (trim($filter->field) == 'nama_kako') {
                                $field = 'c.nama_kako';
                            }
                            if (trim($filter->field) == 'nama_alt') {
                                $field = 'c.nama_alt';
                            }
                            if (trim($filter->field) == 'kode') {
                                $field = 'c.kode';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_kako') {
                        $field = 'c.nama_kako';
                    }
                    if (trim($filter->field) == 'nama_alt') {
                        $field = 'c.nama_alt';
                    }
                    if (trim($filter->field) == 'kode') {
                        $field = 'c.kode';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        $builder->where('c.id_prov', $id_prov);

        return $builder->countAllResults();
    }


    function dd_kako1($id_prov)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('id, nama_kako');
        $builder->where('id_prov', $id_prov);
        $result = $builder->get()->getResult();

        $option[''] = 'Pilih Kab./Kota...';
        foreach ($result as $res) {
            $option[$res->id] = $res->nama_kako;
        }
        return $option;
    } 

    function dd_kako2($id_prov)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('id, nama_kako');
        $builder->where('id_prov', $id_prov);
        $result = $builder->get()->getResult();

        return $result;
    }
}