<?php

namespace App\Controllers\Panel\Elearning\JadwalMengajar;

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

class Absensi extends BC
{
    use ResponseTrait;
    private $JModel;
    public function __construct()
    {
        parent::__construct();
        $this->JModel = new Jadwal();
    }
    public function all($jadwalID)
    {
        if (!$this->request->isAJAX()) {
            // exit('No direct script access allowed');
        }
        $db = db_connect();
        $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']]);
        $builder = $db->table('jadwal_absensi_siswa jas');
        $builder->select('ms.nomor_induk,ms.nama,jas.siswa_id,jas.keterangan');
        $builder->join('m_siswa ms', 'ms.id=jas.siswa_id');
        $builder->where([
            'jas.jadwal_id' => $isExist['id'],
            "jas.created_at >=" => strtotime(date("Y-m-d 00:00:00")),
            "jas.created_at <=" => strtotime(date("Y-m-d 23:59:59"))
        ]);
        $data = $builder->orderBy('nama', 'ASC')->get()->getResultArray();
        // $daata = (new AbsensiSiswa())->getAll($isExist['id']);
        return $this->respond($data);
    }
    public function allIn($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']]);
        // untuk mencegah edit saat tidak di dalam jam pelajaran
        $ed = $isExist['startTime'] > $isExist['endTime'] ? $isExist['startDay'] + 1 : $isExist['startDay'];
        $ed = $ed > 7 ? 1 : $ed;
        if (get_time($isExist['startDay'], $isExist['startTime']) > time()) return $this->fail(['message' => "Kelas Belum Dimulai"]);
        if (get_time($ed, $isExist['endTime']) < time()) return $this->fail(['message' => "Kelas Sudah Berakhir"]);
        $state = (new AbsensiSiswa())->allIn($isExist['id']);
        if (!$state) return $this->fail(['message' => 'Gagal Menyimpan Presensi']);
        return $this->respond(['message' => "Berhasil"]);
    }
    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']]);
        // untuk mencegah edit saat tidak di dalam jam pelajaran
        $ed = $isExist['startTime'] > $isExist['endTime'] ? $isExist['startDay'] + 1 : $isExist['startDay'];
        $ed = $ed > 7 ? 1 : $ed;
        if (get_time($isExist['startDay'], $isExist['startTime']) > time()) return $this->fail(['message' => "Kelas Belum Dimulai"]);
        if (get_time($ed, $isExist['endTime']) < time()) return $this->fail(['message' => "Kelas Sudah Berakhir"]);
        $state = (new AbsensiSiswa())->simpan($isExist['id'], $this->request->getVar('siswa_id'), $this->request->getVar('keterangan'));
        if (!$state) return $this->fail(['message' => 'Gagal Menyimpan Presensi']);
        return $this->respond(['message' => "Berhasil"]);
    }
}
