<?php

namespace App\Models\Account;

use App\Models\BaseModel;

class ProfileModel extends BaseModel
{
    protected $table         = 'users';
    protected $primaryKey    = 'id';
    protected $allowedFields = ['username', 'whatsapp', 'tfa', 'stfa', 'status', 'status_message', 'avatar', 'active', 'last_active', 'deleted_at'];

    // Validation
    protected $validationRules = [
        'username'       => 'required',
        'status'         => 'required',
        'status_message' => 'required',
        'avatar'         => 'required',
        'active'         => 'required',
        'last_active'    => 'required',
        'deleted_at'     => 'required',
    ];

    public function get_by_id($id)
    {
        $db      = \Config\Database::connect();
        $builder = $db->table('users as u');

        $builder->select('u.*, b.nama_unit, b.nama_sunit, c.nama_alt');

        $builder->join('pdk_unit as b', 'b.id = u.unit', 'left');
        $builder->join('pdk_jabatan as c', 'c.id = u.jabatan', 'left');

        $builder->where('u.id', $id);

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

    function get_rows2($id_user, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('userlog');
        $builder->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 {
            $builder->orderBy('id_log', 'DESC');
        }

        $builder->limit($limit, $start);
        $builder->where('id_user', $id_user);
        
        //$builder->join('moduls', 'moduls.id_modul = menus.id_modul');
        return $builder->get()->getResult();
    }

    public function get_total2($id_user, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('userlog');

        $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)));
                }
            }
        }
        $builder->where('id_user', $id_user);

        return $builder->countAllResults();
    }

    function get_rows3($id_user, $limit, $start = 0, $filterRules = NULL, $sortOrder = NULL) {
        $db = \Config\Database::connect();
        $builder = $db->table('pesanbc_res as m');
        $builder->select('m.agen, m.wkt_kirim, c.judul, c.isi, u.username');
        $builder->join('pesanbc as c', 'm.id_pesan = c.id', 'left');
        $builder->join('users as u', 'u.id = c.id_user', '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) == 'judul') {
                                $field = 'c.judul';
                            }
                            if (trim($filter->field) == 'isi') {
                                $field = 'c.isi';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'agen') {
                                $field = 'c.agen';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'judul') {
                        $field = 'c.judul';
                    }
                    if (trim($filter->field) == 'isi') {
                        $field = 'c.isi';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'agen') {
                        $field = 'c.agen';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }

        if ($sortOrder) {
            $builder->orderBy($sortOrder);
        } else {
            $builder->orderBy('m.id', 'DESC');
        }

        $builder->limit($limit, $start);
        $builder->where('m.id_penerima', $id_user);
        
        //$builder->join('moduls', 'moduls.id_modul = menus.id_modul');
        return $builder->get()->getResult();
    }

    public function get_total3($id_user, $filterRules = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pesanbc_res as m');
        $builder->select('m.agen, m.wkt_kirim, c.judul, c.isi, u.username');
        $builder->join('pesanbc as c', 'm.id_pesan = c.id', 'left');
        $builder->join('users as u', 'u.id = c.id_user', '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) == 'judul') {
                                $field = 'c.judul';
                            }
                            if (trim($filter->field) == 'isi') {
                                $field = 'c.isi';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'agen') {
                                $field = 'c.agen';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'judul') {
                        $field = 'c.judul';
                    }
                    if (trim($filter->field) == 'isi') {
                        $field = 'c.isi';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'agen') {
                        $field = 'c.agen';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }
        $builder->where('m.id_penerima', $id_user);

        return $builder->countAllResults();
    }
}
