<?php

namespace App\Models\Admin;

use App\Models\BaseModel;

class RolegroupModel extends BaseModel
{
    protected $table            = 'rolegroups';
    protected $primaryKey       = 'id_group';
    protected $allowedFields    = ['nama_group', 'id_modul', 'ket'];

    // Validation
    protected $validationRules = [
		'nama_group' => [
            'label' => 'nama group',
            'rules' => 'required',
        ],
		'id_modul' => [
            'label' => 'id modul',
            'rules' => 'required',
        ],
		'ket' => [
            'label' => 'ket',
            'rules' => 'required',
        ],
    ];

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

        $builder->join('moduls', 'moduls.id_modul = rolegroups.id_modul');
        $builder->where('id_group', $id);

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

    public function get_total($filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('rolegroups as c');
        $builder->select('c.id_group, c.nama_group, c.ket, m.nama_modul');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', 'left');

        $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) <> '') {
                            $field = $filter->field;
                            if (trim($filter->field) == 'nama_group') {
                                $field = 'c.nama_group';
                            }
                            if (trim($filter->field) == 'nama_modul') {
                                $field = 'm.nama_modul';
                            }
                            if (trim($filter->field) == 'ket') {
                                $field = 'c.ket';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_group') {
                        $field = 'c.nama_group';
                    }
                    if (trim($filter->field) == 'nama_modul') {
                        $field = 'm.nama_modul';
                    }
                    if (trim($filter->field) == 'ket') {
                        $field = 'c.ket';
                    }
                    $builder->like($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('rolegroups as c');
        $builder->select('c.id_group, c.nama_group, c.ket, m.nama_modul');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', '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 = 'c.nama_group';
                            }
                            if (trim($filter->field) == 'nama_modul') {
                                $field = 'm.nama_modul';
                            }
                            if (trim($filter->field) == 'ket') {
                                $field = 'c.ket';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_group') {
                        $field = 'c.nama_group';
                    }
                    if (trim($filter->field) == 'nama_modul') {
                        $field = 'm.nama_modul';
                    }
                    if (trim($filter->field) == 'ket') {
                        $field = 'c.ket';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }

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

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

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

    function dd_rolegroups()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('rolegroups as c');
        $builder->select('c.id_group, c.nama_group, c.id_modul, m.nama_modul');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', 'left');
        $result = $builder->get()->getResult();

        return $result;
    }

    function get_menu_bymodul($id_modul)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('menus');
        $builder->select('id_menu, id_parent, nama_menu, method');
        $builder->where('id_modul', $id_modul);

        $result = $builder->get()->getResultArray();

        return $result;
    }

    function get_menu_bymodul2($id_group, $id_modul)
    {
        $db = \Config\Database::connect();
        // Buat subquery untuk mendapatkan menu_id dari groups_menu berdasarkan group_id
        $subquery = $db->table('rolemenus')
               ->select('menu_id')
               ->where('group_id', $id_group);

        // Query utama untuk mengambil menu yang tidak ada di subquery dan dengan kondisi id_modul
        $query = $db->table('menus')
            ->select('id_menu, id_parent, nama_menu, method')
            ->whereNotIn('id_menu', $subquery) // Mengecualikan hasil subquery
            ->where('id_modul', $id_modul) // Filter berdasarkan id_modul
            ->get();

        $result = $query->getResult();
        return $result;
    }
}