<?php

namespace App\Models\Cusen;

use App\Models\BaseModel;

class PesanModel extends BaseModel
{
    protected $table            = 'pesanbc';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['judul', 'isi', 'id_kantor', 'id_user', 'wkt_rekam'];

    // Validation
    protected $validationRules = [
		'judul' => [
            'label' => 'judul',
            'rules' => 'required',
        ],
		'isi' => [
            'label' => 'pesan',
            'rules' => 'required',
        ],
    ];

    public function kantor_user($id_user)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('users');
        $builder->select('id_kantor');
        $builder->where('id', $id_user);
        $res = $builder->get()->getRow();
        
        return $res ? $res->id_kantor : null;
    }

    function get_rowsx($limit, $start = 0, $filterRules = NULL, $sortOrder = NULL, $id_kantor) {
        $db = \Config\Database::connect();
        $builder = $db->table('pesanbc as m');
        $builder->select('m.*, u.username');
        $builder->join('users as u', 'm.id_user = u.id', 'left');
        $builder->where('m.id_kantor', $id_kantor);
        
        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) == 'judul') {
                                $field = 'm.judul';
                            }
                            if (trim($filter->field) == 'isi') {
                                $field = 'm.isi';
                            }
                            if (trim($filter->field) == 'wkt_rekam') {
                                $field = 'm.wkt_rekam';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'judul') {
                        $field = 'm.judul';
                    }
                    if (trim($filter->field) == 'isi') {
                        $field = 'm.isi';
                    }
                    if (trim($filter->field) == 'wkt_rekam') {
                        $field = 'm.wkt_rekam';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    $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);
        if ($this->where <> '') {
            $builder->where($this->where);
        }
        
        return $builder->get()->getResult();
    }

    public function get_totalx($filterRules = null, $id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pesanbc as m');
        $builder->select('m.*, u.username');
        $builder->join('users as u', 'm.id_user = u.id', 'left');
        $builder->where('m.id_kantor', $id_kantor);
        // Debug log
        log_message('debug', 'get_total2 called with id_kantor: ' . ($id_kantor ?? 'NULL'));
        
        // Only add WHERE condition if id_kantor is not null
        if ($id_kantor !== null) {
            $builder->where('u.id_kantor', $id_kantor);
        }

        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) == 'judul') {
                                $field = 'm.judul';
                            }
                            if (trim($filter->field) == 'isi') {
                                $field = 'm.isi';
                            }
                            if (trim($filter->field) == 'wkt_rekam') {
                                $field = 'm.wkt_rekam';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'judul') {
                        $field = 'm.judul';
                    }
                    if (trim($filter->field) == 'isi') {
                        $field = 'm.isi';
                    }
                    if (trim($filter->field) == 'wkt_rekam') {
                        $field = 'm.wkt_rekam';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        if ($this->where <> '') {
            $builder->where($this->where);
        }

        return $builder->countAllResults();
    }
}