<?php

namespace App\Models\Potensiwilayah;

use App\Models\BaseModel;

class NasabahModel extends BaseModel
{
    protected $table            = 'nasabah_calon';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['id_kegiatan', 'id_produk', 'tgl_kegiatan', 'nama_calon', 'tgl_lahir', 'alamat', 'no_wa', 'nik', 'foto_kegiatan', 'referal', 'closing', 'id_rek', 'wkt_rek', 'no_cif', 'tgl_closing', 'emailx', 'maiden', 'pekerjaan', 'ket', 'foto_ktp', 'foto_closing', 'id_clo', 'wkt_clo'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('nasabah_calon as n');
        $builder->select('n.*, p.nama_giat, pr.nama_kegiatan, l.nama_lokasi, lk.nama_kat, p.tgl_1, p.tgl_2, pd.uraian');
        $builder->join('promosi_main as p', 'n.id_kegiatan = p.id', 'left');
        $builder->join('pdk_promo as pr', 'p.id_jenis = pr.id', 'left');
        $builder->join('lokasi_main as l', 'p.id_lokasi = l.id', 'left');
        $builder->join('lokasi_kate as lk', 'p.id_katelok = lk.id', 'left');
        $builder->join('pdk_laym as pd', 'n.id_produk = pd.id', 'left');
        $builder->where('n.id', $id);

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

    function dd_kegiatan($id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('promosi_main');
        $builder->select('id, nama_giat');
        $builder->where('status', 0);
        $builder->where('id_kantor', $id_kantor);
        $builder->orderBy('nama_giat', 'ASC');
        return $builder->get()->getResult();
    }

    function dd_produk()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('pdk_laym');
        $builder->select('id, uraian');
        $builder->orderBy('uraian', 'ASC');
        return $builder->get()->getResult();
    }

    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->id_kantor;
    }

    function get_kegiatan_detail($id_kegiatan)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('promosi_main as p');
        $builder->select('p.nama_giat, p.tgl_1, p.tgl_2, pr.nama_kegiatan, l.nama_lokasi, l.alamat, lk.nama_kat');
        $builder->join('pdk_promo as pr', 'p.id_jenis = pr.id', 'left');
        $builder->join('lokasi_main as l', 'p.id_lokasi = l.id', 'left');
        $builder->join('lokasi_kate as lk', 'p.id_katelok = lk.id', 'left');
        $builder->where('p.id', $id_kegiatan);
        return $builder->get()->getRow();
    }

    function get_rows2($limit, $start = 0, $filterRules = NULL, $sortOrder = NULL, $id_kantor) {
        $db = \Config\Database::connect();
        $builder = $db->table('nasabah_calon as n');
        $builder->select('n.id, n.nama_calon, n.nik, n.tgl_lahir, n.tgl_kegiatan, n.no_wa, n.closing, p.nama_giat, l.nama_lokasi, u.username, n.wkt_rek');
        $builder->join('promosi_main as p', 'n.id_kegiatan = p.id', 'left');
        $builder->join('lokasi_main as l', 'p.id_lokasi = l.id', 'left');
        $builder->join('users as u', 'n.id_rek = u.id', 'left');
        $builder->where('p.id_kantor', $id_kantor);
        $builder->where('n.closing', 0);
        
        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_calon') {
                                $field = 'n.nama_calon';
                            }
                            if (trim($filter->field) == 'nik') {
                                $field = 'n.nik';
                            }
                            if (trim($filter->field) == 'tgl_lahir') {
                                $field = 'n.tgl_lahir';
                            }
                            if (trim($filter->field) == 'tgl_kegiatan') {
                                $field = 'n.tgl_kegiatan';
                            }
                            if (trim($filter->field) == 'no_wa') {
                                $field = 'n.no_wa';
                            }
                            if (trim($filter->field) == 'closing') {
                                $field = 'n.closing';
                            }
                            if (trim($filter->field) == 'nama_giat') {
                                $field = 'p.nama_giat';
                            }
                            if (trim($filter->field) == 'nama_lokasi') {
                                $field = 'l.nama_lokasi';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'wkt_rek') {
                                $field = 'n.wkt_rek';
                            }
                            
                            if (trim($filter->field) == 'closing') {
                                $builder->orWhere($field, trim(urldecode($p)));
                            } else {
                                $builder->orLike($field, trim(urldecode($p)));
                            }
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_calon') {
                        $field = 'n.nama_calon';
                    }
                    if (trim($filter->field) == 'nik') {
                        $field = 'n.nik';
                    }
                    if (trim($filter->field) == 'tgl_lahir') {
                        $field = 'n.tgl_lahir';
                    }
                    if (trim($filter->field) == 'no_wa') {
                        $field = 'n.no_wa';
                    }
                    if (trim($filter->field) == 'closing') {
                        $field = 'n.closing';
                    }
                    if (trim($filter->field) == 'nama_giat') {
                        $field = 'p.nama_giat';
                    }
                    if (trim($filter->field) == 'nama_lokasi') {
                        $field = 'l.nama_lokasi';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'wkt_rek') {
                        $field = 'n.wkt_rek';
                    }
                    
                    if (trim($filter->field) == 'closing') {
                        $builder->where($field, trim(urldecode($value)));
                    } else {
                        $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);
                
        return $builder->get()->getResult();
    }

    public function get_total2($filterRules = null, $id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('nasabah_calon as n');
        $builder->select('n.id, n.nama_calon, n.nik, n.tgl_lahir, n.tgl_kegiatan, n.no_wa, n.closing, p.nama_giat, l.nama_lokasi, u.username, n.wkt_rek');
        $builder->join('promosi_main as p', 'n.id_kegiatan = p.id', 'left');
        $builder->join('lokasi_main as l', 'p.id_lokasi = l.id', 'left');
        $builder->join('users as u', 'n.id_rek = u.id', 'left');
        $builder->where('p.id_kantor', $id_kantor);
        $builder->where('n.closing', 0);

        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_calon') {
                                $field = 'n.nama_calon';
                            }
                            if (trim($filter->field) == 'nik') {
                                $field = 'n.nik';
                            }
                            if (trim($filter->field) == 'tgl_lahir') {
                                $field = 'n.tgl_lahir';
                            }
                            if (trim($filter->field) == 'tgl_kegiatan') {
                                $field = 'n.tgl_kegiatan';
                            }
                            if (trim($filter->field) == 'no_wa') {
                                $field = 'n.no_wa';
                            }
                            if (trim($filter->field) == 'closing') {
                                $field = 'n.closing';
                            }
                            if (trim($filter->field) == 'nama_giat') {
                                $field = 'p.nama_giat';
                            }
                            if (trim($filter->field) == 'nama_lokasi') {
                                $field = 'l.nama_lokasi';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'wkt_rek') {
                                $field = 'n.wkt_rek';
                            }
                            
                            if (trim($filter->field) == 'closing') {
                                $builder->orWhere($field, trim(urldecode($p)));
                            } else {
                                $builder->orLike($field, trim(urldecode($p)));
                            }
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_calon') {
                        $field = 'n.nama_calon';
                    }
                    if (trim($filter->field) == 'nik') {
                        $field = 'n.nik';
                    }
                    if (trim($filter->field) == 'tgl_lahir') {
                        $field = 'n.tgl_lahir';
                    }
                    if (trim($filter->field) == 'no_wa') {
                        $field = 'n.no_wa';
                    }
                    if (trim($filter->field) == 'closing') {
                        $field = 'n.closing';
                    }
                    if (trim($filter->field) == 'nama_giat') {
                        $field = 'p.nama_giat';
                    }
                    if (trim($filter->field) == 'nama_lokasi') {
                        $field = 'l.nama_lokasi';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'wkt_rek') {
                        $field = 'n.wkt_rek';
                    }
                    
                    if (trim($filter->field) == 'closing') {
                        $builder->where($field, trim(urldecode($value)));
                    } else {
                        $builder->like($field, trim(urldecode($value)));
                    }
                }
            }
        }

        return $builder->countAllResults();
    }

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

    function generateNextCif()
    {
        $db = \Config\Database::connect();
        $builder = $db->table('nasabah_calon');
        $builder->select('no_cif');
        $builder->where('no_cif IS NOT NULL');
        $builder->orderBy('no_cif', 'DESC');
        $builder->limit(1);
        $result = $builder->get()->getRow();
        
        if ($result && $result->no_cif) {
            $lastCif = (int)$result->no_cif;
            $nextCif = $lastCif + 1;
        } else {
            $nextCif = 1;
        }
        
        return str_pad($nextCif, 6, '0', STR_PAD_LEFT);
    }
}
