<?php
// config.php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', '');
define('DB_NAME', 'c2fls3n_2');

// Koneksi database
function connectDB() {
    $conn = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);

    if ($conn->connect_error) {
        die("Koneksi gagal: " . $conn->connect_error);
    }

    return $conn;
}

// Start session jika belum dimulai
function startSession() {
    if (session_status() == PHP_SESSION_NONE) {
        session_start();
    }
}

// Periksa login
function isLoggedIn() {
    startSession();
    return isset($_SESSION['user_id']) && !empty($_SESSION['user_id']);
}

// Periksa role
function hasRole($role) {
    startSession();
    return isset($_SESSION['role']) && $_SESSION['role'] === $role;
}

// Redirect jika tidak login
function requireLogin() {
    if (!isLoggedIn()) {
        header("Location: ../admin");
        exit;
    }
}

// Redirect jika tidak memiliki role yang benar
function requireRole($role) {
    requireLogin();
    if (!hasRole($role)) {
        if ($role === 'admin') {
            // Jika juri mencoba akses halaman admin
            header("Location: juri_dashboard");
        } else {
            // Jika admin mencoba akses halaman juri
            header("Location: admin_dashboard");
        }
        exit;
    }
}

// Fungsi untuk memeriksa domisili juri
function isDomisiliJuri($domisiliPeserta) {
    startSession();
    if ($_SESSION['role'] !== 'juri') {
        return true; // Admin dapat mengakses semua
    }

    return $_SESSION['domisili'] === $domisiliPeserta;
}

// Fungsi sanitasi input
function sanitizeInput($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

// Fungsi untuk mendapatkan nama cabang lomba berdasarkan ID
function getNamaCabangLomba($id) {
    $conn = connectDB();
    $stmt = $conn->prepare("SELECT Bidang_Lomba FROM cabang_lomba WHERE ID = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        return $row['Bidang_Lomba'];
    }

    return "Tidak ditemukan";
}

// Fungsi untuk mendapatkan data user berdasarkan ID
function getUserById($id) {
    $conn = connectDB();
    $stmt = $conn->prepare("SELECT * FROM users WHERE id = ?");
    $stmt->bind_param("i", $id);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        return $result->fetch_assoc();
    }

    return null;
}

// Fungsi untuk logout
function logout() {
    startSession();
    // Hapus semua variabel session
    $_SESSION = array();

    // Hancurkan session
    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }

    session_destroy();

    // Redirect ke halaman login
    header("Location: ../admin");
    exit;
}
