<?php

namespace App\Models\Admin;

use App\Models\BaseModel;

class ModulModel extends BaseModel
{
    protected $table            = 'moduls';
    protected $primaryKey       = 'id_modul';
    protected $allowedFields    = ['fname', 'nama_modul', 'urut', 'ikon', 'ket', 'created_at', 'created_by', 'updated_at', 'updated_by'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);

        $builder->where('id_modul', $id);

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

    public function get_total($filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);

        $builder->select($this->primaryKey);
        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) <> '') {
                            $builder->orLike($filter->field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $builder->like($filter->field, trim(urldecode($value)));
                }
            }
        }
        if ($this->where <> '') {
            $builder->where($this->where);
        }
        return $builder->countAllResults();
    }

    function get_rows($limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);

        if ($this->select <> '') {
            $builder->select($this->select);
        }
        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) <> '') {
                            $builder->orLike($filter->field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $builder->like($filter->field, trim(urldecode($value)));
                }
            }
        }

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

        $builder->limit($limit, $start);
        if ($this->where <> '') {
            $builder->where($this->where);
        }
        return $builder->get()->getResult();
    }

    function dd_moduls()
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $result = $builder->get()->getResult();

        $option[''] = 'Please Select';
        foreach ($result as $res) {
                $option[$res->id_modul] = $res->nama_modul;
        }
        return $option;
    }
}