<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

use App\Models\Panel\Elearning\JadwalPelajaran;
use CodeIgniter\Model;

class Tugas extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'jadwal_tugas';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'jadwal_id',
        'kompetensi_id',
        'judul',
        'file',
        'deskripsi',
        'is_active',
        'dueDate',
    ];

    // 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 getAllByTahunKelas($jadwal_id, $tahun_id, $kelas_id)
    {
        $mapel_id = (new JadwalPelajaran())->getMapelIdFromJadwal($jadwal_id);
        $filePath = base_url(config('app')->pdfPath . "/elearning/tugas");
        $select = ["jt.id,jt.judul,jt.dueDate,jt.kompetensi_id,jt.is_active,jt.created_at,jt.updated_at"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $select[] = "CASE WHEN jt.file IS NOT NULL THEN CONCAT('$filePath/',jt.file) ELSE NULL END file";
        $builder = $this->db->table("$this->table jt");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jt.jadwal_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jt.kompetensi_id');
        $builder->where(['jp.tahun_id' => $tahun_id, 'jp.kelas_id' => $kelas_id, 'jp.mapel_id' => $mapel_id['mapel_id']]);
        return $builder->get()->getResultArray();
    }
    public function getAllByJadwalID($jadwal_id)
    {
        $filePath = base_url(config('app')->pdfPath . "/elearning/tugas");
        $select = ["jt.id,jt.judul,jt.dueDate,jt.kompetensi_id,jt.is_active,jt.created_at,jt.updated_at"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $select[] = "CASE WHEN jt.file IS NOT NULL THEN CONCAT('$filePath/',jt.file) ELSE NULL END file";
        $builder = $this->db->table("$this->table jt");
        $builder->select(implode(',', $select));
        $builder->join('kompetensi_dasar kd', 'kd.id=jt.kompetensi_id');
        $builder->where('jt.jadwal_id', $jadwal_id);
        return $builder->get()->getResultArray();
    }
    public function related($jadwal_id)
    {
        $jadwal = (new JadwalPelajaran())->findId($jadwal_id);
        $select = ["jt.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jt.judul"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jt");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jt.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jt.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('kd.mapel_id', $jadwal['mapel_id']);
        $builder->where('kd.tingkat_id', $jadwal['tingkat_id']);
        $builder->where('jadwal_id !=', $jadwal_id);
        return $builder->get()->getResultArray();
    }
    public function getTugas($id, $jadwal_id = null)
    {
        $filePath = base_url(config('app')->pdfPath . "/elearning/tugas");
        $select = ["jt.id,jt.deskripsi,jt.judul,jt.dueDate,jt.kompetensi_id,jt.is_active,jt.created_at,jt.updated_at"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $select[] = "CASE WHEN jt.file IS NOT NULL THEN CONCAT('$filePath/',jt.file) ELSE NULL END file";
        $builder = $this->db->table("$this->table jt");
        $builder->select(implode(',', $select));
        $builder->join('kompetensi_dasar kd', 'kd.id=jt.kompetensi_id');
        $builder->where('jt.id', $id);
        if ($jadwal_id) $builder->where('jt.jadwal_id', $jadwal_id);
        return $builder->get()->getRowArray();
    }
}
