<?php

namespace App\Models\Admin;

use App\Models\BaseModel;

class MenuModel extends BaseModel
{
    protected $table            = 'menus';
    protected $primaryKey       = 'id_menu';
    protected $allowedFields    = ['id_modul', 'id_parent', 'nama_menu', 'urut', 'method', 'created_at', 'created_by', 'updated_at', 'updated_by'];

    // Validation
    protected $validationRules = [
		'id_modul' => [
            'label' => 'id modul',
            'rules' => 'required',
        ],
		'nama_menu' => [
            'label' => 'nama menu',
            'rules' => 'required',
        ],
		'urut' => [
            'label' => 'urut',
            'rules' => 'required',
        ],
		'method' => [
            'label' => 'method',
            'rules' => 'required',
        ],
    ];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('menus as c');
        $builder->select('c.id_menu, c.id_parent, c.nama_menu, c.urut, c.method, m.id_modul, m.nama_modul, x.nama_menu as nama_parent');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', 'left');
        $builder->join('menus as x', 'x.id_menu = c.id_parent', 'left');
        $builder->where('c.id_menu', $id);

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

    function get_rows2($id_modul, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('menus as c');
        $builder->select('c.id_menu, c.id_parent, c.nama_menu, c.urut, c.method, m.id_modul, m.nama_modul, x.nama_menu as nama_parent');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', 'left');
        $builder->join('menus as x', 'x.id_menu = c.id_parent', '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_menu') {
                                $field = 'c.nama_menu';
                            }
                            if (trim($filter->field) == 'method') {
                                $field = 'c.method';
                            }
                            if (trim($filter->field) == 'urut') {
                                $field = 'c.urut';
                            }
                            if (trim($filter->field) == 'nama_parent') {
                                $field = 'x.nama_menu';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_menu') {
                        $field = 'c.nama_menu';
                    }
                    if (trim($filter->field) == 'method') {
                        $field = 'c.method';
                    }
                    if (trim($filter->field) == 'urut') {
                        $field = 'c.urut';
                    }
                    if (trim($filter->field) == 'nama_parent') {
                        $field = 'x.nama_menu';
                    }
                    $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_modul', $id_modul);
        
        return $builder->get()->getResult();
    }

    public function get_total2($id_modul, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('menus as c');
        $builder->select('c.id_menu, c.id_parent, c.nama_menu, c.urut, c.method, m.id_modul, m.nama_modul, x.nama_menu as nama_parent');
        $builder->join('moduls as m', 'm.id_modul = c.id_modul', 'left');
        $builder->join('menus as x', 'x.id_menu = c.id_parent', 'left');

        if ($filterRules) {
            $filterRules = json_decode($filterRules);
            //var_dump(('Id MOdUL = '. $id_modul)); die();
            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_menu') {
                                $field = 'c.nama_menu';
                            }
                            if (trim($filter->field) == 'method') {
                                $field = 'c.method';
                            }
                            if (trim($filter->field) == 'urut') {
                                $field = 'c.urut';
                            }
                            if (trim($filter->field) == 'nama_parent') {
                                $field = 'x.nama_menu';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_menu') {
                        $field = 'c.nama_menu';
                    }
                    if (trim($filter->field) == 'method') {
                        $field = 'c.method';
                    }
                    if (trim($filter->field) == 'urut') {
                        $field = 'c.urut';
                    }
                    if (trim($filter->field) == 'nama_parent') {
                        $field = 'x.nama_menu';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        $builder->where('c.id_modul', $id_modul);

        return $builder->countAllResults();
    }

    //data menu parent saat new dan edit
    function dd_menus($id_modul)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('id_menu, nama_menu');
        $builder->where('id_modul', $id_modul);
        $builder->where('id_parent', 0);
        $result = $builder->get()->getResult();

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

    //data menu parent onchange
    function get_parent($id_modul)
    {
        $db = \Config\Database::connect();
        $builder = $db->table($this->table);
        $builder->select('id_menu, nama_menu');
        $builder->where('id_modul', $id_modul);
        $builder->where('id_parent', 0);
        $result = $builder->get()->getResult();

        return $result;
    }
        
}