<?php

namespace App\Models\Mobile\Kelas;

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',
        'countJam',
        '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($day = null)
    {
        $url = base_url("kelas");
        $select = ['u.fullname nama_guru,jp.countJam,mm.nama nama_mapel,mm.kode kode_mapel,jp.startDay,jp.startTime,jp.endTime'];
        $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[] = "jl.title title_libur,(CASE WHEN jl.id IS NULL THEN 0 ELSE 1 END) is_libur";
        $builder = $this->db->table('jadwal_pelajaran jp');
        $builder->select(implode(',', $select));
        $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->join('m_mapel mm', 'mm.id=jp.mapel_id');
        $builder->join('users u', 'u.id=jp.user_id');
        $builder->where('jp.kelas_id', user()->id_kelas);
        $builder->where('tp.is_active', 1);
        if (is_integer($day)) $builder->where('jp.startDay', $day);
        if (is_array($day)) $builder->whereIn('jp.startDay', $day);
        return $builder->get()->getResultArray();
    }
    public function getJadwalBelajar($jadwalID)
    {
        $url = base_url("kelas");
        $select = ['jp.id,u.fullname nama_guru,jp.countJam,mm.nama nama_mapel,mm.kode kode_mapel,jp.startDay,jp.startTime,jp.endTime'];
        $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[] = "jl.title title_libur,(CASE WHEN jl.id IS NULL THEN 0 ELSE 1 END) is_libur";
        $builder = $this->db->table('jadwal_pelajaran jp');
        $builder->select(implode(',', $select));
        $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->join('m_mapel mm', 'mm.id=jp.mapel_id');
        $builder->join('users u', 'u.id=jp.user_id');
        $builder->where('jp.kelas_id', user()->id_kelas);
        $builder->where('tp.is_active', 1);
        $builder->where('jp.id', $jadwalID);
        return $builder->get()->getRowArray();
    }
}
