<?php

namespace App\Models\Panel\LainLain;

use CodeIgniter\Model;

class BeritaAcara extends Model
{
    protected $DBGroup              = 'default';
    protected $table                = 'berita_acara';
    protected $primaryKey           = 'id';
    protected $useAutoIncrement     = true;
    protected $insertID             = 0;
    protected $returnType           = 'array';
    protected $useSoftDeletes       = false;
    protected $protectFields        = true;
    protected $allowedFields        = [
        'jadwal_id',
        'nomor_induk_proktor',
        'nama_proktor',
        'catatan',
    ];

    // 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          = [];
    protected $select               = [
        "berita_acara.*,du.nama nama_draft,mru.kode kode_ruang,mru.nama nama_ruang",
        "ju.startDateTime,ju.endDateTime",
        "(SELECT COUNT(*) FROM ujian_siswa sus WHERE sus.jadwal_id=ju.id AND sus.status != 0) siswa_hadir",
        "(SELECT COUNT(*) FROM ujian_siswa sus WHERE sus.jadwal_id=ju.id AND sus.status = 0) tidak_hadir",
        "mm.kode kode_mapel,mm.nama nama_mapel,users.nomor_induk nomor_induk_pengawas",
        "mju.kode kode_jenis,mju.nama nama_jenis,users.fullname nama_pengawas",
        "(SELECT GROUP_CONCAT(DISTINCT (CASE WHEN smk.kode IS NULL THEN 'Tidak Ada Kelas' ELSE CONCAT(smk.kode) END)) FROM m_siswa sms JOIN m_kelas smk ON smk.id=sms.kelas_id JOIN ujian_siswa sus ON sus.siswa_id=sms.id WHERE sus.jadwal_id=ju.id GROUP BY sus.jadwal_id) kelas"
    ];

    public function getAll()
    {
        $builder = $this->db->table($this->table);
        $builder->select(implode(',', $this->select), false);
        $builder->join("jadwal_ujian ju", "ju.id=$this->table.jadwal_id");
        $builder->join("m_ruangujian mru", "mru.id=ju.ruang_id", "left");
        $builder->join("m_jenisujian mju", "mju.id=ju.jenis_id", "left");
        $builder->join("draft_ujian du", "du.id=ju.draft_id", "left");
        $builder->join("users", "users.id=ju.pengawas_id", "left");
        $builder->join("m_mapel mm", "mm.id=du.mapel_id", "left");
        return $builder->get()->getResultArray();
    }
    public function findId($id)
    {
        $this->select[] = "(SELECT GROUP_CONCAT(sms.nomor_induk) FROM ujian_siswa sus JOIN m_siswa sms ON sms.id=sus.siswa_id WHERE sus.jadwal_id=ju.id AND sus.status = 0) siswa_tidak_hadir";
        $builder = $this->db->table($this->table);
        $builder->select(implode(',', $this->select), false);
        $builder->join("jadwal_ujian ju", "ju.id=$this->table.jadwal_id");
        $builder->join("m_ruangujian mru", "mru.id=ju.ruang_id", "left");
        $builder->join("m_jenisujian mju", "mju.id=ju.jenis_id", "left");
        $builder->join("draft_ujian du", "du.id=ju.draft_id", "left");
        $builder->join("users", "users.id=ju.pengawas_id", "left");
        $builder->join("m_mapel mm", "mm.id=du.mapel_id", "left");
        $builder->where(["$this->table.id" => $id]);
        return $builder->get()->getRowArray();
    }
}
