<?php
namespace App\Models;

use Core\Database;

class JadwalUjian
{
    private $db;

    public function __construct()
    {
        $this->db = Database::getConnection();
    }

    public function getAll()
    {
        $stmt = $this->db->query("SELECT * FROM tb_jadwal_ujian ORDER BY tgl_mulai_ujian DESC");
        return $stmt->fetchAll();
    }

    public function tambah($data)
    {
        $sql = "INSERT INTO tb_jadwal_ujian (id_dmk, tgl_mulai_ujian, jam_mulai, tgl_berakhir, jam_berakhir, jenis_soal, tahun_akademik)
                VALUES (:id_dmk, :tgl_mulai_ujian, :jam_mulai, :tgl_berakhir, :jam_berakhir, :jenis_soal, :tahun_akademik)";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':id_dmk' => $data['id_dmk'],
            ':tgl_mulai_ujian' => $data['tgl_mulai_ujian'],
            ':jam_mulai' => $data['jam_mulai'],
            ':tgl_berakhir' => $data['tgl_berakhir'],
            ':jam_berakhir' => $data['jam_berakhir'],
            ':jenis_soal' => $data['jenis_soal'],
            ':tahun_akademik' => $data['tahun_akademik']
        ]);
    }

    public function update($id, $data)
    {
        $sql = "UPDATE tb_jadwal_ujian SET 
                id_dmk = :id_dmk,
                tgl_mulai_ujian = :tgl_mulai_ujian,
                jam_mulai = :jam_mulai,
                tgl_berakhir = :tgl_berakhir,
                jam_berakhir = :jam_berakhir,
                jenis_soal = :jenis_soal,
                tahun_akademik = :tahun_akademik
                WHERE id_jadwal_u = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([
            ':id' => $id,
            ':id_dmk' => $data['id_dmk'],
            ':tgl_mulai_ujian' => $data['tgl_mulai_ujian'],
            ':jam_mulai' => $data['jam_mulai'],
            ':tgl_berakhir' => $data['tgl_berakhir'],
            ':jam_berakhir' => $data['jam_berakhir'],
            ':jenis_soal' => $data['jenis_soal'],
            ':tahun_akademik' => $data['tahun_akademik']
        ]);
    }

    public function hapus($id)
    {
        $sql = "DELETE FROM tb_jadwal_ujian WHERE id_jadwal_u = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->execute([':id' => $id]);
    }
}
