<?php

namespace App\Models\Referensi;

use App\Models\BaseModel;

class PdkclayModel extends BaseModel
{
    protected $table            = 'pdk_layc';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['id_jalay', 'uraian', 'berkas'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_layc as c');
        $builder->select('c.*, m.uraian as nama_layanan');
        $builder->join('pdk_laym as m', 'm.id = c.id_jalay', 'left');
        $builder->where('c.id', $id);

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

    function get_rows2($id_mlay, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_layc as c');
        $builder->select('c.id, c.id_jalay, c.uraian as slayanan, c.berkas, m.uraian as nama_layanan');
        $builder->join('pdk_laym as m', 'm.id = c.id_jalay', '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) == 'slayanan') {
                                $field = 'c.uraian';
                            }
                            if (trim($filter->field) == 'berkas') {
                                $field = 'c.berkas';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'slayanan') {
                        $field = 'c.uraian';
                    }
                    if (trim($filter->field) == 'berkas') {
                        $field = 'c.berkas';
                    }
                    $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_jalay', $id_mlay);
        
        return $builder->get()->getResult();
    }

    public function get_total2($id_mlay, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_layc as c');
        $builder->select('c.id, c.id_jalay, c.uraian as slayanan, c.berkas, m.uraian as nama_layanan');
        $builder->join('pdk_laym as m', 'm.id = c.id_jalay', '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) == 'slayanan') {
                                $field = 'c.uraian';
                            }
                            if (trim($filter->field) == 'berkas') {
                                $field = 'c.berkas';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'slayanan') {
                        $field = 'c.uraian';
                    }
                    if (trim($filter->field) == 'berkas') {
                        $field = 'c.berkas';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        $builder->where('c.id_jalay', $id_mlay);

        return $builder->countAllResults();
    }
}