<?php

namespace App\Models\Pesaing;

use App\Models\BaseModel;

class DataswotModel extends BaseModel
{
    protected $table            = 'swot_pesaing';
    protected $primaryKey       = 'id';
    protected $allowedFields    = ['id_pesaing', 'id_produk', 'kekuatan', 'kelemahan', 'kesempatan', 'ancaman', 'id_rek', 'wkt_rek'];

    public function find_by_id($id)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('swot_pesaing as s');
        $builder->select('s.*, lp.nama_pesaing, lp.lati, lp.longi, x.uraian, u.username');
        $builder->join('lokasi_pesaing as lp', 's.id_pesaing = lp.id', 'left');
        $builder->join('pdk_laym as x', 's.id_produk = x.id', 'left');
        $builder->join('users as u', 's.id_rek = u.id', 'left');
        $builder->where('s.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;
    }

    function dd_pesaing($id_kantor = null)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        $builder->select('id, nama_pesaing, lati, longi');
        
        if ($id_kantor) {
            $builder->where('id_kantor', $id_kantor);
        }
        
        $builder->orderBy('nama_pesaing', '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();
    }

    function get_pesaing_detail($id_pesaing)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('lokasi_pesaing');
        $builder->select('id, nama_pesaing, lati, longi');
        $builder->where('id', $id_pesaing);
        return $builder->get()->getRow();
    }

    function get_rows2($limit, $start = 0, $filterRules = NULL, $sortOrder = NULL, $id_kantor) {
        $db = \Config\Database::connect();
        $builder = $db->table('swot_pesaing as s');
        $builder->select('s.id, s.kekuatan, s.kelemahan, s.kesempatan, s.ancaman, s.wkt_rek, lp.nama_pesaing, x.uraian, u.username');
        $builder->join('lokasi_pesaing as lp', 's.id_pesaing = lp.id', 'left');
        $builder->join('pdk_laym as x', 's.id_produk = x.id', 'left');
        $builder->join('users as u', 's.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) == 'uraian') {
                                $field = 'x.uraian';
                            }
                            if (trim($filter->field) == 'kekuatan') {
                                $field = 's.kekuatan';
                            }
                            if (trim($filter->field) == 'kelemahan') {
                                $field = 's.kelemahan';
                            }
                            if (trim($filter->field) == 'kesempatan') {
                                $field = 's.kesempatan';
                            }
                            if (trim($filter->field) == 'ancaman') {
                                $field = 's.ancaman';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            $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) == 'uraian') {
                        $field = 'x.uraian';
                    }
                    if (trim($filter->field) == 'kekuatan') {
                        $field = 's.kekuatan';
                    }
                    if (trim($filter->field) == 'kelemahan') {
                        $field = 's.kelemahan';
                    }
                    if (trim($filter->field) == 'kesempatan') {
                        $field = 's.kesempatan';
                    }
                    if (trim($filter->field) == 'ancaman') {
                        $field = 's.ancaman';
                    }
                    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);
                
        return $builder->get()->getResult();
    }

    public function get_total2($filterRules = null, $id_kantor)
    {
        $db = \Config\Database::connect();
        $builder = $db->table('swot_pesaing as s');
        $builder->select('s.id, s.kekuatan, s.kelemahan, s.kesempatan, s.ancaman, s.wkt_rek, lp.nama_pesaing, x.uraian, u.username');
        $builder->join('lokasi_pesaing as lp', 's.id_pesaing = lp.id', 'left');
        $builder->join('users as u', 's.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) == 'uraian') {
                                $field = 'x.uraian';
                            }
                            if (trim($filter->field) == 'kekuatan') {
                                $field = 's.kekuatan';
                            }
                            if (trim($filter->field) == 'kelemahan') {
                                $field = 's.kelemahan';
                            }
                            if (trim($filter->field) == 'kesempatan') {
                                $field = 's.kesempatan';
                            }
                            if (trim($filter->field) == 'ancaman') {
                                $field = 's.ancaman';
                            }
                            if (trim($filter->field) == 'username') {
                                $field = 'u.username';
                            }
                            $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) == 'uraian') {
                        $field = 'x.uraian';
                    }
                    if (trim($filter->field) == 'kekuatan') {
                        $field = 's.kekuatan';
                    }
                    if (trim($filter->field) == 'kelemahan') {
                        $field = 's.kelemahan';
                    }
                    if (trim($filter->field) == 'kesempatan') {
                        $field = 's.kesempatan';
                    }
                    if (trim($filter->field) == 'ancaman') {
                        $field = 's.ancaman';
                    }
                    if (trim($filter->field) == 'username') {
                        $field = 'u.username';
                    }
                    $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;
    }
}
