<?php

namespace App\Models;

use CodeIgniter\Model;

class RoleModel extends Model
{
    protected $table            = 'tbl_role';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useSoftDeletes   = false;
    protected $protectFields    = false;
    protected $allowedFields    = [];

    protected bool $allowEmptyInserts = false;
    protected bool $updateOnlyChanged = true;

    protected array $casts = [];
    protected array $castHandlers = [];

    // 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 _set_query()
    {
        if(!empty($this->search)){
            $this->groupStart();
            $this->like('DATE(name)',$this->search);
            $this->groupEnd();
        }
    }

    public function getAllData()
    {
        $data = [];

        if ($this->columns[$this->order[0]['column']]['data'] == 'no') {
            $this->columns[$this->order[0]['column']]['data'] = $this->table . '.id';
        }

        //all data
        $this->select($this->table . '.id');
        $this->countData = $this->countAllResults();

        //filtered
        $this->select($this->table . '.id');
        $this->_set_query();
        $this->filterData = $this->countAllResults();

        //view data
        $this->_set_query();
        $this->orderBy($this->columns[$this->order[0]['column']]['data'], $this->order[0]['dir']);
        $this->limit( intval($this->limit), intval($this->start));
        $getData = $this->findAll();
        $no      = $this->start;
        
        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {
                $row->no = ++$no;
                
                
                $row->option = '<p class="m-0 text-nowrap">';

                if(intval($row->id)!=1){

                    if(intval(session()->get('role_id'))==1){

                        $row->option = $row->option . '<a href="javascript:void(0)" title="Ubah data" data-link="' . base_url() . 'role/edit/' . $row->id . '" class=" datatable-edit me-2"><i class="bx bx-edit text-warning" style="font-size: 20px;"></i></a>';
                    }


                    if(intval(session()->get('role_id'))==1){

                        $row->option = $row->option . ' <a href="javascript:void(0)" data-id="' . $row->id . '" title="Hapus data" data-link="' . base_url() . 'role/delete" class=" datatable-delete"><i class="bx bx-trash text-danger" style="font-size: 20px;"></i></a>';
                    }

                }
                $row->option    = $row->option . '</p>';



                $data[]      = $row;
            }
        }

        $result = array(
            "draw"            => $this->draw,
            "recordsTotal"    => $this->countData,
            "recordsFiltered" => $this->filterData,
            "data"            => $data,
        );

        return json_encode($result);
    }


    public function getAllRoleName()
    {

        $this->select('id');
        $this->where('id<>', 1);
        $this->countData    = $this->findAll();

        $this->select('id');
        $this->where('id<>', 1);
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->filterData = $this->countAllResults();

        $this->where('id<>', 1);
        $this->like('UPPER(name)', strtoupper($this->name));
        $this->limit(intval($this->limit), intval($this->start));
        $getData        = $this->findAll();

        $data           = [];
        $no             = $this->start;

        if ( count((array) $getData) > 0) {
            foreach ($getData as $row) {
                $row->no    = $no + 1;
                $data[]     = $row;
            }
        }

        return json_encode(array(
            'recorsTotal'     => $this->countData,
            'recordsFiltered' => $this->filterData,
            'data'            => $data
        ));
    }

}
