<?php

namespace App\Models\Panel\Elearning\JadwalMengajar;

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

class AgendaKegiatan extends Model
{
    protected $DBGroup          = 'default';
    protected $table            = 'agenda_kegiatan';
    protected $primaryKey       = 'tanggal';
    protected $useAutoIncrement = false;
    protected $insertID         = 0;
    protected $returnType       = 'array';
    protected $useSoftDeletes   = false;
    protected $protectFields    = true;
    protected $allowedFields    = [
        'jadwal_id',
        'rpp_id',
        'rangkuman',
        'tanggal',
    ];

    // 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);
        $builder = $this->db->table("jadwal_pelajaran jp")->join("$this->table ak", 'ak.jadwal_id=jp.id');
        $builder->join('rpp', 'rpp.id=ak.rpp_id', 'left');
        $builder->join('kompetensi_dasar kd', 'kd.id=rpp.kompetensi_id', 'left');
        $builder->select("ak.*,kd.nomor nomor_kompetensi,kd.nama nama_kompetensi,kd.kode kode_kompetensi");
        $builder->where(['jp.tahun_id' => $tahun_id, 'jp.kelas_id' => $kelas_id, 'jp.mapel_id' => $mapel_id['mapel_id']])->orderBy('ak.created_at', 'ASC');
        return $builder->get()->getResultArray();
    }

    public function getAllByJadwalID($jadwal_id)
    {
        $builder = $this->db->table("$this->table ak")->join('rpp', 'rpp.id=ak.rpp_id', 'left');
        $builder->join('kompetensi_dasar kd', 'kd.id=rpp.kompetensi_id', 'left');
        $builder->select("ak.*,kd.nomor nomor_kompetensi,kd.nama nama_kompetensi,kd.kode kode_kompetensi");
        $builder->where('ak.jadwal_id', $jadwal_id)->orderBy('ak.created_at', 'ASC');
        return $builder->get()->getResultArray();
    }

    public function simpan($jadwal_id, $data, $tanggal = null)
    {
        $builder = $this->db->table($this->table);
        if (isset($data['rangkuman'])) $builder->set('rangkuman', $data['rangkuman']);
        if (isset($data['rpp_id'])) $builder->set('rpp_id', $data['rpp_id']);
        $builder->set('updated_at', time());
        $builder->where('jadwal_id', $jadwal_id);
        $builder->where("created_at >=", $tanggal ? strtotime("$tanggal 00:00:00") : strtotime(date('Y-m-d 00:00:00')));
        $builder->where("created_at <=", $tanggal ? strtotime("$tanggal 23:59:59") : strtotime(date('Y-m-d 23:59:59')));
        $builder->update();
        return $this->db->affectedRows() > 0;
    }
}
