<?php

namespace App\Models;

use CodeIgniter\Model;
use App\Libraries\GeneralLib;

class PangkalanperubahanApproval extends Model
{
    protected $table      = 'pangkalan_approval_perubahan';
    protected $primaryKey = 'id_pangkalan_approval';

    protected $useAutoIncrement = true;

    protected $returnType     = 'array';
    protected $useSoftDeletes = true;

    protected $allowedFields = ['id_perubahan','id_pangkalan','id_user','status_data_pokok','tgl_status_data_pokok','status_data_pemilik','tgl_status_data_pemilik','status_data_lokasi','tgl_status_data_lokasi','status_data_dokumen','tgl_status_data_dokumen','status_all','tgl_status_all','komentar','tgl_komentar'];

    // Dates
    protected $useTimestamps = false;
    protected $dateFormat    = 'datetime';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';
    protected $deletedField  = 'deleted_at';

    // Validation
    protected $validationRules      = [];
    protected $validationMessages   = [];
    protected $skipValidation       = false;
    protected $cleanValidationRules = true;

    // Callbacks
    protected $allowCallbacks = true;
    protected $beforeInsert   = [];
    protected $afterInsert    = [];
    protected $beforeUpdate   = [];
    protected $afterUpdate    = [];
    protected $beforeFind     = [];
    protected $afterFind      = [];
    protected $beforeDelete   = [];
    protected $afterDelete    = [];

    public function GetDT($request)
    {
        $genlib = new GeneralLib();
        $db = db_connect();
        $builder = $db->table('vpangkalan');
        
        $return_data = array();
        $return_data['draw'] = $request['draw'];

        $where = array();
        foreach($request['columns'] as $item){
            if (($item['searchable']=='true') && (!empty($item['search']['value'])))
            {
                $where[] = $item['data']." like '%".$item['search']['value']."%'";
            }
        }

        if (!empty($request['search']['value']))
        {
            $where = array();
            foreach($request['columns'] as $item){
                if (($item['searchable']=='true'))
                {
                    $where[] = $item['data']." like '%".$request['search']['value']."%'";
                }
            }
        }

        if(isset($request['searchBuilder']))
        {
            $filter = $genlib->dt_traverse_search_builder($request['searchBuilder']);
        }else{
            $filter = implode(" or ",$where);
        }
        //die(var_dump($filter));

        $order_by = $request['columns'][$request['order'][0]['column']]['data'];
        $order = $request['order'][0]['dir'];
        
        if (!empty($filter))
        {
            $data = $builder->where($filter)->orderBy($order_by, $order)->limit($request['start'],$request['length'])->get()->getResult();
        }else{
            $data = $builder->orderBy($order_by, $order)->limit($request['start'],$request['length'])->get()->getResult();
        }
        //die(var_dump($registrasi));
        $return_data['recordsTotal'] = $this->countAll();
        $return_data['recordsFiltered'] = count($data);
        $return_data['data'] = $data;

        return json_encode($return_data);
    }
}
?>