<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

use CodeIgniter\Model;

class Jadwal extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_pelajaran';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'mapel_id',
        'kelas_id',
        'tahun_id',
        'user_id',
        'startDay',
        'startTime',
        'endTime',
    ];

    // 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($startDate = null, $endDate = null)
    {
        $url = base_url("panel/e-learning/jadwal-mengajar");
        $select = ['mm.nama nama_mapel,mm.kode kode_mapel,jp.startDay,jp.startTime,jp.endTime'];
        $select[] = "CONCAT('$url/praktek/',TO_BASE64(jp.id)) masuk";
        $select[] = "CONCAT('$url/materi/',TO_BASE64(jp.id)) materi";
        $select[] = "CONCAT('$url/tugas/',TO_BASE64(jp.id)) tugas";
        $select[] = "CONCAT('$url/diskusi/',TO_BASE64(jp.id)) diskusi";
        $select[] = "mk.kode kode_kelas,mk.nama nama_kelas,u.nomor_induk";
        $select[] = "jl.title title_libur,CASE WHEN jl.id IS NULL THEN 0 ELSE 1 END is_libur";
        $builder =  $this->db->table('jadwal_pelajaran jp')->select(implode(',', $select), false);
        $builder->join('m_mapel mm', 'mm.id=jp.mapel_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('users u', 'u.id=jp.user_id');
        $tgl = date('Y-m-d');
        $builder->join('jadwal_libur jl', "jl.tanggal='$tgl' AND jl.is_nasional=1 AND DAYOFWEEK(jl.tanggal)=jp.startDay", 'left', false);
        $builder->join('tahun_pelajaran tp', 'tp.id=jp.tahun_id');
        $builder->where(['user_id' => user()->id, 'tp.is_active' => 1]);
        if ($startDate) $builder->where('jp.startDay >=', $startDate);
        if ($endDate) $builder->where('jp.startDay <=', $endDate);
        return $builder->get()->getResultArray();
    }

    public function getJadwalMengajar($jadwalID)
    {
        $tgl = date('Y-m-d');
        $QAgendaCheck = $this->db->table('agenda_kegiatan ak')->select("'$tgl' as tanggal");
        $QAgendaCheck->where([
            'ak.created_at >=' => strtotime(date("Y-m-d 00:00:00")),
            'ak.created_at <=' => strtotime(date("Y-m-d 23:59:59")),
            'ak.jadwal_id' => 'jp.id'
        ], null, false);
        $QAgendaCheck = $QAgendaCheck->getCompiledSelect();
        $select = ['jp.*,mk.nama nama_kelas,mm.nama nama_mapel,mm.kode kode_mapel,jl.title title_libur'];
        $select[] = "tp.is_active tahun_active,($QAgendaCheck) agenda_exist,CASE WHEN jl.id IS NULL THEN 0 ELSE 1 END is_libur";
        $builder = $this->db->table('jadwal_pelajaran jp');
        $builder->select(implode(',', $select), false);
        $builder->join('tahun_pelajaran tp', 'tp.id=jp.tahun_id');
        $builder->join('m_mapel mm', 'mm.id=jp.mapel_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('jadwal_libur jl', "jl.tanggal='$tgl' AND jl.is_nasional=1", 'left', false);
        $builder->where(['tp.is_active' => 1, 'jp.id' => $jadwalID, 'jp.user_id' => user()->id], null, false);
        // dd($builder->getCompiledSelect());
        return $builder->get()->getRowArray();
    }
}
