<?php
// get_nilai.php
require_once 'config.php';
requireLogin();

// Memastikan parameter yang dibutuhkan ada
if (!isset($_GET['nisn']) || !isset($_GET['domisili'])) {
    http_response_code(400);
    echo json_encode(['error' => 'Parameter tidak lengkap']);
    exit;
}

$nisn = sanitizeInput($_GET['nisn']);
$domisili = sanitizeInput($_GET['domisili']);
$id_juri = $_SESSION['user_id'];

$conn = connectDB();

// Ambil data penilaian
$stmt = $conn->prepare("SELECT nilai_teknik, nilai_penampilan, nilai_kreativitas, catatan, total_nilai FROM penilaian WHERE id_juri = ? AND nisn_peserta = ? AND domisili = ?");
$stmt->bind_param("iss", $id_juri, $nisn, $domisili);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $data = $result->fetch_assoc();
    echo json_encode($data);
} else {
    http_response_code(404);
    echo json_encode(['error' => 'Data tidak ditemukan']);
}

$conn->close();