<?php

namespace App\Controllers;

use Core\Controller;
use App\Models\Jadwal;
use App\Models\Presensi;

class MahasiswaJadwalController extends Controller
{
    public function index()
    {
        $page = $_GET['page'] ?? 'jadwal';
        $nim = $_SESSION['user']['nim'] ?? null;

        if (!$nim) {
            return $this->view('mahasiswa/jadwal', [
                'error' => 'NIM tidak ditemukan dalam session!',
                'page' => $page
            ]);
        }

        if ($page == 'jadwal') {
            $jadwalModel = new Jadwal();
            $data = $jadwalModel->getJadwalByNim($nim);

            return $this->view('mahasiswa/jadwal', [
                'page' => 'jadwal',
                'jadwal' => $data['jadwal'] ?? [],
                'kode_prodi' => $data['kode_prodi'] ?? null,
                'error' => $data['error'] ?? null
            ]);
        } elseif ($page == 'presensi') {
            $pertemuan = $_GET['pertemuan_ke'] ?? null;
            $presensiModel = new Presensi();
            $data = $presensiModel->getPresensiByNim($nim, $pertemuan);

            return $this->view('mahasiswa/jadwal', [
                'page' => 'presensi',
                'presensi' => $data['presensi'] ?? [],
                'pertemuan_ke' => $pertemuan,
                'kode_prodi' => $data['kode_prodi'] ?? null,
                'error' => $data['error'] ?? null
            ]);
        }
    }
}
