<?php
namespace App\Controllers;

use Core\Database;
use PDO;

class ManagePegKompreController
{
    private $db;

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

public function index()
{
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    // ==========================
    // TOGGLE STATUS KOMPRE
    // ==========================
    if (isset($_POST['toggle_status'])) {

        $stmt = $this->db->query("SELECT status FROM tb_kompre_active WHERE id_active_k = 1");
        $currentStatus = $stmt->fetchColumn();

        $newStatus = ($currentStatus === 'Aktif') ? 'Tutup' : 'Aktif';

        $update = $this->db->prepare("UPDATE tb_kompre_active SET status = :status WHERE id_active_k = 1");
        $update->bindValue(':status', $newStatus);
        $update->execute();

        header("Location: /dashboard/pegawai-manage_kompre");
        exit;
    }

    // ==========================
    // AMBIL STATUS KOMPRE
    // ==========================
    $stmtStatus = $this->db->query("SELECT status FROM tb_kompre_active WHERE id_active_k = 1");
    $status_kompre = $stmtStatus->fetchColumn();

    // ==========================
    // PAGINATION
    // ==========================
    $perPage = 15;
    $page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int) $_GET['page'] : 1;
    $offset = ($page - 1) * $perPage;

    // ==========================
    // SEARCH
    // ==========================
    $search = isset($_GET['search']) ? trim($_GET['search']) : '';
    $searchSql = '';
    $params = [];

    if (!empty($search)) {
        $searchSql = "WHERE m.nim LIKE :search OR m.nama LIKE :search";
        $params['search'] = "%$search%";
    }

    // ==========================
    // HITUNG TOTAL DATA
    // ==========================
    $countSql = "
        SELECT COUNT(*) 
        FROM tb_ujiankompre uk
        JOIN tb_mahasiswa m ON uk.nim COLLATE utf8mb4_unicode_ci = m.nim COLLATE utf8mb4_unicode_ci
        JOIN tb_token_kompre tk ON uk.nim COLLATE utf8mb4_unicode_ci = tk.nim COLLATE utf8mb4_unicode_ci
        $searchSql
    ";

    $countStmt = $this->db->prepare($countSql);

    foreach ($params as $key => $val) {
        $countStmt->bindValue(":$key", $val);
    }

    $countStmt->execute();
    $totalData = $countStmt->fetchColumn();
    $totalPages = ceil($totalData / $perPage);

    // ==========================
    // AMBIL DATA KOMPRE
    // ==========================
    $sql = "
        SELECT 
            uk.id_ujiankompre,
            uk.nim,
            uk.waktu_mulai,
            uk.waktu_selesai,
            uk.skor,
            m.nama,
            m.id_prodi,
            m.jenis_kelamin,
            m.alamat,
            m.telpon,
            m.tmpt_lahir,
            m.tgl_lahir,
            m.agama,
            m.tahun_masuk,
            tk.token
        FROM tb_ujiankompre uk
        JOIN tb_mahasiswa m ON uk.nim COLLATE utf8mb4_unicode_ci = m.nim COLLATE utf8mb4_unicode_ci
        JOIN tb_token_kompre tk ON uk.nim COLLATE utf8mb4_unicode_ci = tk.nim COLLATE utf8mb4_unicode_ci
        $searchSql
        ORDER BY uk.waktu_mulai DESC
        LIMIT :offset, :perPage
    ";

    $stmt = $this->db->prepare($sql);

    foreach ($params as $key => $val) {
        $stmt->bindValue(":$key", $val);
    }

    $stmt->bindValue(":offset", $offset, PDO::PARAM_INT);
    $stmt->bindValue(":perPage", $perPage, PDO::PARAM_INT);

    $stmt->execute();

    $data_kompre = $stmt->fetchAll(PDO::FETCH_ASSOC);

    include __DIR__ . '/../Views/pegawai/data_kompre.php';
}

}
