<?php

namespace App\Models\Panel\Approvals\Presence;

use CodeIgniter\Model;

class Siswa extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'approvals_presence_siswa';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'siswa_id',
        'status',
        'approval_description',
        'presence_status',
        'approver_description',
        'tanggal',
        'edited_by',
    ];

    // Dates
    protected $useTimestamps = true;
    protected $dateFormat    = 'int';
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_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 getAll($opt = [])
    {
        $select = "$this->table.*,IFNULL(u.fullname,'') editor_name,ms.nomor_induk,ms.nama user_name";
        $builder = $this->db->table($this->table);
        $builder->join('m_siswa ms', "ms.id=$this->table.siswa_id");
        $builder->join('users u', "u.id=$this->table.edited_by", 'left');
        $builder->select($select);
        if (isset($opt['status'])) {
            $builder->where("$this->table.status", $opt['status']);
        }
        if (isset($opt['presence_status'])) {
            $builder->where("$this->table.presence_status", $opt['presence_status']);
        }
        if (isset($opt['kelas_id'])) {
            $builder->where("$this->table.kelas_id", $opt['kelas_id']);
        }
        if (isset($opt['created_at'])) {
            if (is_array($opt['created_at'])) {
                $builder->where("($this->table.created_at BETWEEN {$opt['created_at']['startDate']} AND {$opt['created_at']['endDate']})", null, false);
            }
        }
        if (isset($opt['startDate'])) {
            $builder->where("$this->table.created_at >=", "$opt[startDate]",  false);
        }
        if (isset($opt['endDate'])) {
            $builder->where("$this->table.created_at <=", "$opt[endDate]",  false);
        }
        $builder->orderBy("created_at", "DESC");
        return $builder->get()->getResultArray();
    }
    public function findId($id, $opt = [])
    {
        $select = "$this->table.*,IFNULL(u.fullname,'') editor_name,ms.nomor_induk,ms.nama user_name";
        $builder = $this->db->table($this->table);
        $builder->join('m_siswa ms', "ms.id=$this->table.siswa_id");
        $builder->join('users u', "u.id=$this->table.edited_by", 'left');
        $builder->select($select);
        if (isset($opt['siswa_id'])) {
            $builder->where("$this->table.siswa_id", $opt['siswa_id']);
        }
        $builder->where("$this->table.id", $id);
        return $builder->get()->getRowArray();
    }
}
