<?php

namespace App\Models\Mobile\Approvals;

use CodeIgniter\Model;

class Presence 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.*,'' approval_description,'' approver_description,u.fullname editor_name";
        $builder = $this->db->table($this->table);
        $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']);
        }
        if (isset($opt['status'])) {
            $builder->where("$this->table.status", $opt['status']);
        }
        $builder->orderBy("created_at", "DESC");
        return $builder->get()->getResultArray();
    }
    public function findId($id, $opt = [])
    {
        $select = "$this->table.*,u.fullname editor_name";
        $builder = $this->db->table($this->table);
        $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();
    }
}
