<?php
namespace App\Models;

use Core\Database;
use PDO;

class ManagePMB
{
    protected $pdo;

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

    public function getStatusPmb()
    {
        $stmt = $this->pdo->query("SELECT status FROM tb_akses_pmb LIMIT 1");
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row['status'] ?? 'Tidak Aktif';
    }

    public function updateStatusPmb($status)
    {
        $stmt = $this->pdo->prepare("UPDATE tb_akses_pmb SET status = ?");
        $stmt->execute([$status]);
    }

    public function getStatusSoal()
    {
        $stmt = $this->pdo->query("SELECT status FROM tb_soal_testpmb LIMIT 1");
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        return $row['status'] ?? 'Tidak Aktif';
    }

    public function updateStatusSoal($status)
    {
        $stmt = $this->pdo->prepare("UPDATE tb_soal_testpmb SET status = ?"); // Perlu disesuaikan logika jika per soal
        $stmt->execute([$status]);
    }

	public function getTanggalHasilTes()
	{
		$stmt = $this->pdo->query("SELECT DISTINCT tanggal FROM tb_pmb ORDER BY tanggal DESC");
		return $stmt->fetchAll(\PDO::FETCH_ASSOC);
	}

    public function tambahGelombang($gelombang, $tanggal)
    {
        $stmt = $this->pdo->prepare("INSERT INTO tb_gel_pmb (gelombang, tanggal, status, t_pnrimaan_a) VALUES (?, ?, 'Aktif', '')");
        $stmt->execute([$gelombang, $tanggal]);
    }

    public function getGelombangList()
    {
        $stmt = $this->pdo->query("SELECT * FROM tb_gel_pmb ORDER BY tanggal DESC");
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function editGelombang($id, $gelombang, $tanggal)
    {
        $stmt = $this->pdo->prepare("UPDATE tb_gel_pmb SET gelombang = ?, tanggal = ? WHERE id_gel_pmb = ?");
        $stmt->execute([$gelombang, $tanggal, $id]);
    }

    public function hapusGelombang($id)
    {
        $stmt = $this->pdo->prepare("DELETE FROM tb_gel_pmb WHERE id_gel_pmb = ?");
        $stmt->execute([$id]);
    }

}
