<?php

namespace App\Models\Pesaing;

use App\Models\BaseModel;

class DatapesaingModel extends BaseModel
{
    protected $table            = 'lokasi_pesaing';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['nama_pesaing', 'alamat', 'lati', 'longi', 'phone', 'website', 'rating', 'id_rek', 'wkt_rek', 'id_kantor'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing as lp');
        $builder->select('lp.*, u.username, pk.lati as kantor_lati, pk.longi as kantor_longi');
        $builder->join('users as u', 'lp.id_rek = u.id', 'left');
        $builder->join('pdk_satker as pk', 'lp.id_kantor = pk.id', 'left');
        $builder->where('lp.id', $id);

        return $builder->get()->getRow();
    }
    
    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;
    }

    public function get_swot_pesaing($id_pesaing)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('swot_pesaing as sp');
        $builder->select('sp.*, u.username');
        $builder->join('users as u', 'sp.id_rek = u.id', 'left');
        $builder->where('sp.id_pesaing', $id_pesaing);
        $builder->orderBy('sp.wkt_rek', 'DESC');

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

    function get_rows2($limit, $start = 0, $filterRules = NULL, $sortOrder = NULL, $id_kantor) {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing as lp');
        $builder->select('lp.id, lp.nama_pesaing, lp.alamat, lp.phone, lp.website, lp.rating, lp.wkt_rek, u.username as nama_user');
        $builder->join('users as u', 'lp.id_rek = u.id', 'left');
        $builder->where('lp.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) == 'nama_pesaing') {
                                $field = 'lp.nama_pesaing';
                            }
                            if (trim($filter->field) == 'alamat') {
                                $field = 'lp.alamat';
                            }
                            if (trim($filter->field) == 'phone') {
                                $field = 'lp.phone';
                            }
                            if (trim($filter->field) == 'website') {
                                $field = 'lp.website';
                            }
                            if (trim($filter->field) == 'rating') {
                                $field = 'lp.rating';
                            }
                            if (trim($filter->field) == 'nama_user') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'wkt_rek') {
                                $field = 'lp.wkt_rek';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_pesaing') {
                        $field = 'lp.nama_pesaing';
                    }
                    if (trim($filter->field) == 'alamat') {
                        $field = 'lp.alamat';
                    }
                    if (trim($filter->field) == 'phone') {
                        $field = 'lp.phone';
                    }
                    if (trim($filter->field) == 'website') {
                        $field = 'lp.website';
                    }
                    if (trim($filter->field) == 'rating') {
                        $field = 'lp.rating';
                    }
                    if (trim($filter->field) == 'nama_user') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'wkt_rek') {
                        $field = 'lp.wkt_rek';
                    }
                    $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('lokasi_pesaing as lp');
        $builder->select('lp.id, lp.nama_pesaing, lp.alamat, lp.phone, lp.website, lp.rating, lp.wkt_rek, u.username as nama_user');
        $builder->join('users as u', 'lp.id_rek = u.id', 'left');
        $builder->where('lp.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) == 'nama_pesaing') {
                                $field = 'lp.nama_pesaing';
                            }
                            if (trim($filter->field) == 'alamat') {
                                $field = 'lp.alamat';
                            }
                            if (trim($filter->field) == 'phone') {
                                $field = 'lp.phone';
                            }
                            if (trim($filter->field) == 'website') {
                                $field = 'lp.website';
                            }
                            if (trim($filter->field) == 'rating') {
                                $field = 'lp.rating';
                            }
                            if (trim($filter->field) == 'nama_user') {
                                $field = 'u.username';
                            }
                            if (trim($filter->field) == 'wkt_rek') {
                                $field = 'lp.wkt_rek';
                            }
                            $builder->orLike($field, trim(urldecode($p)));
                        }
                    }
                    $builder->groupEnd();
                } else {
                    $field = $filter->field;
                    if (trim($filter->field) == 'nama_pesaing') {
                        $field = 'lp.nama_pesaing';
                    }
                    if (trim($filter->field) == 'alamat') {
                        $field = 'lp.alamat';
                    }
                    if (trim($filter->field) == 'phone') {
                        $field = 'lp.phone';
                    }
                    if (trim($filter->field) == 'website') {
                        $field = 'lp.website';
                    }
                    if (trim($filter->field) == 'rating') {
                        $field = 'lp.rating';
                    }
                    if (trim($filter->field) == 'nama_user') {
                        $field = 'u.username';
                    }
                    if (trim($filter->field) == 'wkt_rek') {
                        $field = 'lp.wkt_rek';
                    }
                    $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;
    }
}
