<?php

namespace App\Controllers\Panel\Ujian\Jadwal\Detail\Result;

use App\Controllers\Panel\BC;
use App\Models\Panel\Elearning\TahunPelajaran;
use App\Models\Panel\Ujian\Jadwal;
use CodeIgniter\API\ResponseTrait;

class Kehadiran extends BC
{
    use ResponseTrait;
    private $jadwalModel;
    public function __construct()
    {
        parent::__construct();
        $this->jadwalModel = new Jadwal();
    }
    public function index($id)
    {
        $id = base64_decode($id);
        $sid = $this->request->getGet('id') ?? '';
        $sid = explode(',', $sid);
        $jadwalData = $this->jadwalModel->findId($id);
        $data['page_title'] = "Daftar Hadir - " . $jadwalData['kode_ruang'] . " - " . $jadwalData['nama_draft'];
        $data['startDateTime'] = $jadwalData['startDateTime'];
        $data['endDateTime'] = $jadwalData['endDateTime'];
        $data['nama_mapel'] = $jadwalData['nama_mapel'];
        $data['nama_jenis'] = $jadwalData['nama_jenis'];
        $data['ruang'] = $jadwalData['kode_ruang'];
        $data['peserta'] = db_connect()->table('ujian_siswa');
        $data['peserta']->select('ujian_siswa.*,m_siswa.nama,m_siswa.nomor_induk,m_siswa.agama');
        $data['peserta']->join('m_siswa', 'm_siswa.id=ujian_siswa.siswa_id');
        $data['peserta']->where('jadwal_id', $jadwalData['id']);
        if ($sid) $data['peserta']->whereIn('ujian_siswa.id', $sid);
        $data['peserta'] = $data['peserta']->get()->getResultArray();
        $data['jumlah_peserta'] = count($data['peserta']);
        $data['limit_page'] = 25;
        $data['logo'] = logo();
        $data['kementrian'] = kementrian();
        $data['halaman'] = $data['jumlah_peserta'] <= 0 ? 0 : ceil($data['jumlah_peserta'] / $data['limit_page']);
        $data['tp'] = (new TahunPelajaran())->getActiveName();
        return view('Panel/Ujian/Jadwal/Detail/daftar-hadir', $data);
    }
}
