<?php

namespace App\Models\Admin;

use App\Models\BaseModel;

class RolemenuModel extends BaseModel
{
    protected $table            = 'rolemenus';
    protected $primaryKey       = 'id_rm';
    protected $allowedFields    = ['group_id', 'menu_id', 'rekam', 'ubah', 'hapus', 'exp'];

    // Validation
    protected $validationRules = [
		'group_id' => [
            'label' => 'group id',
            'rules' => 'required',
        ],
		'menu_id' => [
            'label' => 'menu id',
            'rules' => 'required',
        ],
    ];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('rolemenus as c');
        $builder->select('c.*, g.nama_group, m.nama_menu');
        $builder->join('rolegroups as g', 'g.id_group = c.group_id', 'left');
        $builder->join('menus as m', 'm.id_menu = c.menu_id', 'left');
        $builder->where('c.id_rm', $id);

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

    public function get_total2($id_group, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('rolemenus as c');
        $builder->select('c.*, g.nama_group, m.nama_menu');
        $builder->join('rolegroups as g', 'g.id_group = c.group_id', 'left');
        $builder->join('menus as m', 'm.id_menu = c.menu_id', '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_group') {
                                $field = 'g.nama_group';
                            }
                            if (trim($filter->field) == 'nama_menu') {
                                $field = 'm.nama_menu';
                            }
                            if (trim($filter->field) == 'rekam') {
                                $field = 'c.rekam';
                            }
                            if (trim($filter->field) == 'ubah') {
                                $field = 'c.ubah';
                            }
                            if (trim($filter->field) == 'hapus') {
                                $field = 'c.hapus';
                            }
                            if (trim($filter->field) == 'exp') {
                                $field = 'c.exp';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    if (trim($filter->field) == 'nama_group') {
                        $field = 'g.nama_group';
                    }
                    if (trim($filter->field) == 'nama_menu') {
                        $field = 'm.nama_menu';
                    }
                    if (trim($filter->field) == 'rekam') {
                        $field = 'c.rekam';
                    }
                    if (trim($filter->field) == 'ubah') {
                        $field = 'c.ubah';
                    }
                    if (trim($filter->field) == 'hapus') {
                        $field = 'c.hapus';
                    }
                    if (trim($filter->field) == 'exp') {
                        $field = 'c.exp';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        $builder->where('c.group_id', $id_group);
        return $builder->countAllResults();
    }

    function get_rows2($id_group, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('rolemenus as c');
        $builder->select('c.*, g.nama_group, m.nama_menu, m.id_parent');
        $builder->join('rolegroups as g', 'g.id_group = c.group_id', 'left');
        $builder->join('menus as m', 'm.id_menu = c.menu_id', '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_group') {
                                $field = 'g.nama_group';
                            }
                            if (trim($filter->field) == 'nama_menu') {
                                $field = 'm.nama_menu';
                            }
                            if (trim($filter->field) == 'id_parent') {
                                $field = 'm.id_parent';
                            }
                            if (trim($filter->field) == 'rekam') {
                                $field = 'c.rekam';
                            }
                            if (trim($filter->field) == 'ubah') {
                                $field = 'c.ubah';
                            }
                            if (trim($filter->field) == 'hapus') {
                                $field = 'c.hapus';
                            }
                            if (trim($filter->field) == 'exp') {
                                $field = 'c.exp';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    if (trim($filter->field) == 'nama_group') {
                        $field = 'g.nama_group';
                    }
                    if (trim($filter->field) == 'nama_menu') {
                        $field = 'm.nama_menu';
                    }
                    if (trim($filter->field) == 'id_parent') {
                        $field = 'm.id_parent';
                    }
                    if (trim($filter->field) == 'rekam') {
                        $field = 'c.rekam';
                    }
                    if (trim($filter->field) == 'ubah') {
                        $field = 'c.ubah';
                    }
                    if (trim($filter->field) == 'hapus') {
                        $field = 'c.hapus';
                    }
                    if (trim($filter->field) == 'exp') {
                        $field = 'c.exp';
                    }
                    $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.group_id', $id_group);

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