<?php

namespace App\Models\Mobile\Kelas;

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

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

    // 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, $kelas_id)
    {
        $mapel_id = (new JadwalPelajaran())->getMapelIdFromJadwal($jadwal_id);
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,jm.judul,jm.isi,jm.created_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('tahun_pelajaran tp', 'tp.id=jp.tahun_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where(['jm.is_active' => '1', 'tp.is_active' => 1, 'jp.kelas_id' => $kelas_id, 'jp.mapel_id' => $mapel_id['mapel_id']]);
        $builder->orderBy('jm.created_at', 'ASC');
        return $builder->get()->getResultArray();
    }
    public function getAllByJadwalID($jadwal_id)
    {
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul,jm.isi,jm.created_at,jm.kompetensi_id,jm.is_active,jm.updated_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('jadwal_id', $jadwal_id);
        return $builder->get()->getResultArray();
    }
    public function getMateri($id, $jadwal_id = null)
    {
        $filePath = base_url("assets/other/materi");
        $select = ["jm.id,mm.kode kode_mapel,mk.nama nama_kelas,mk.kode kode_kelas,jm.judul,jm.isi,jm.created_at,jm.kompetensi_id,jm.is_active,jm.updated_at,CASE WHEN jm.file IS NOT NULL THEN CONCAT('$filePath/',jm.file) ELSE NULL END file"];
        $select[] = "kd.nama nama_kompetensi,kd.kode kode_kompetensi,kd.nomor nomor_kompetensi,kd.mapel_id";
        $builder = $this->db->table("$this->table jm");
        $builder->select(implode(',', $select));
        $builder->join('jadwal_pelajaran jp', 'jp.id=jm.jadwal_id');
        $builder->join('m_kelas mk', 'mk.id=jp.kelas_id');
        $builder->join('kompetensi_dasar kd', 'kd.id=jm.kompetensi_id');
        $builder->join('m_mapel mm', 'mm.id=kd.mapel_id');
        $builder->where('jm.id', $id);
        if ($jadwal_id) $builder->where('jm.jadwal_id', $jadwal_id);
        return $builder->get()->getRowArray();
    }
}
