<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

use CodeIgniter\Model;

class AbsensiSiswa extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_absensi_siswa';
    protected $primaryKey       = 'siswa_id';
    protected $useAutoIncrement = false;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'jadwal_id',
        'siswa_id',
        'keterangan',
    ];

    // 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($jadwal_id, $opt = [])
    {
        $builder = $this->db->table("$this->table jas");
        $builder->select('ms.nomor_induk,ms.nama,jas.siswa_id,jas.keterangan');
        $builder->join('m_siswa ms', 'ms.id=jas.siswa_id');
        $builder->where('jas.jadwal_id', $jadwal_id);
        $tanggal = $opt['tanggal'] ?? null;
        $builder->where("created_at >=", $tanggal ? strtotime("$tanggal 00:00:00") : strtotime(date('Y-m-d 00:00:00')));
        $builder->where("created_at <=", $tanggal ? strtotime("$tanggal 23:59:59") : strtotime(date('Y-m-d 23:59:59')));
        return  $builder->orderBy('nama', 'ASC')->get()->getResultArray();
    }
    public function simpan($jadwal_id, $siswa_id, $keterangan, $tanggal = null)
    {
        $builder = $this->db->table("$this->table");
        $builder->where(['siswa_id' => $siswa_id, 'jadwal_id' => $jadwal_id]);
        $builder->where("created_at >=", $tanggal ? strtotime("$tanggal 00:00:00") : strtotime(date('Y-m-d 00:00:00')));
        $builder->where("created_at <=", $tanggal ? strtotime("$tanggal 23:59:59") : strtotime(date('Y-m-d 23:59:59')));
        $builder->set(['keterangan' => $keterangan, 'updated_at' => time()])->update();
        return $this->db->affectedRows() > 0;
    }
    public function allIn($jadwal_id, $tanggal = null)
    {
        $builder = $this->db->table("$this->table");
        $builder->where(['jadwal_id' => $jadwal_id]);
        $builder->where("created_at >=", $tanggal ? strtotime("$tanggal 00:00:00") : strtotime(date('Y-m-d 00:00:00')));
        $builder->where("created_at <=", $tanggal ? strtotime("$tanggal 23:59:59") : strtotime(date('Y-m-d 23:59:59')));
        $builder->set(['keterangan' => "1", 'updated_at' => time()])->update();
        return $this->db->affectedRows() > 0;
    }
}
