<?php

namespace App\Controllers\Panel\Elearning\JadwalMengajar;

use App\Controllers\Panel\BC;
use App\Models\Panel\Elearning\JadwalMengajar\AgendaKegiatan;
use App\Models\Panel\Elearning\JadwalMengajar\Jadwal;
use CodeIgniter\API\ResponseTrait;

class Agenda extends BC
{
    use ResponseTrait;
    private $JModel;
    public function __construct()
    {
        parent::__construct();
        $this->JModel = new Jadwal();
    }
    public function all($jadwalID)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $isExist = $this->JModel->getJadwalMengajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        if (!$isExist['agenda_exist']) return $this->failNotFound();
        if ($isExist['is_libur'] == 1) return $this->fail(['message' => $isExist['title_libur']]);
        $agenda = (new AgendaKegiatan())->getAllByTahunKelas($isExist['id'], $isExist['tahun_id'], $isExist['kelas_id']);
        return $this->respond(['agenda' => $agenda, 'mapel_id' => $isExist['mapel_id'], 'tahun_id' => $isExist['tahun_id']]);
    }
    public function save($jadwalID)
    {
        if (!$this->request->isAJAX() && ENVIRONMENT != 'development') {
            exit('No direct script access allowed');
        }
        $isExist = $this->JModel->getJadwalMengajar(base64_decode($jadwalID));
        if (!$isExist) return $this->failNotFound();
        if (!$isExist['agenda_exist']) return $this->failNotFound();
        if ($isExist['is_libur'] == 1) return $this->fail(['message' => $isExist['title_libur']]);
        $state = (new AgendaKegiatan())->simpan($isExist['id'], [
            'rangkuman' => $this->request->getVar('rangkuman'),
            'rpp_id' => $this->request->getVar('rpp_id'),
        ]);
        if (!$state) return $this->fail(['message' => "Gagal Menyimpan Agenda"]);
        return $this->respond(['message' => "Berhasil"]);
    }
}
