<?php

namespace App\Models\Cusen;

use App\Models\BaseModel;

class PesanChildModel extends BaseModel
{
    protected $table            = 'pesanbc_res';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['id_pesan', 'id_penerima', 'agen', 'wkt_kirim'];

    // Validation
    protected $validationRules = [
		'id_pesan' => [
            'label' => 'id_pesan',
            'rules' => 'required',
        ],
		'id_penerima' => [
            'label' => 'id_penerima',
            'rules' => 'required',
        ],
    ];

    function get_rows2($id_pesan, $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, u.nama_calon');
        $builder->join('nasabah_calon as u', 'm.id_penerima = u.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) == 'agen') {
                                $field = 'm.agen';
                            }
                            if (trim($filter->field) == 'wkt_kirim') {
                                $field = 'm.wkt_kirim';
                            }
                            if (trim($filter->field) == 'nama_calon') {
                                $field = 'u.nama_calon';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'agen') {
                        $field = 'm.agen';
                    }
                    if (trim($filter->field) == 'wkt_kirim') {
                        $field = 'm.wkt_kirim';
                    }
                    if (trim($filter->field) == 'nama_calon') {
                        $field = 'u.nama_calon';
                    }
                    $builder->like($field, trim(urldecode($value)));
                }
            }
        }

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

        $builder->limit($limit, $start);
        $builder->where('m.id_pesan', $id_pesan);
        
        return $builder->get()->getResult();
    }

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

        return $builder->countAllResults();
    }
}