<?php
include('con.php');

$msg = '';

// ================= HANDLE RESULT FILE UPLOAD =================
if (isset($_POST['upload_result'])) {
    $id = intval($_POST['history_id']);
    $file = $_FILES['result_file']['name'];
    $upload_dir = "new/ll_results/";

    if (!is_dir($upload_dir)) {
        mkdir($upload_dir, 0777, true);
    }

    $file_path = $upload_dir . time() . "_" . basename($file);
    $allowed_types = ['jpg', 'jpeg', 'png', 'webp', 'pdf'];
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));

    if (!in_array($ext, $allowed_types)) {
        $msg = "Only JPG, JPEG, PNG, WEBP, and PDF files are allowed.";
    } else {
        if (move_uploaded_file($_FILES['result_file']['tmp_name'], $file_path)) {
            // Update result_file path and mark status as Completed
            mysqli_query($ahk_conn, "UPDATE ll_exam_history SET result_file='$file_path', status=2 WHERE id='$id'");
            $msg = "Result file uploaded successfully!";
        } else {
            $msg = "Error uploading file.";
        }
    }
}

// ================= HANDLE STATUS UPDATE =================
if (isset($_POST['update_status'])) {
    $id = intval($_POST['history_id']);
    $status = intval($_POST['status']);
    mysqli_query($ahk_conn, "UPDATE ll_exam_history SET status='$status' WHERE id='$id'");
    $msg = "Status updated successfully!";
}

// ================= FETCH PENDING LL EXAM REQUESTS =================
$result = mysqli_query($ahk_conn, "
    SELECT * 
    FROM ll_exam_history 
    WHERE status IN (0,1) 
    ORDER BY id DESC
");

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>LL Exam Admin Panel</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
</head>
<body class="bg-light">

<div class="container mt-5">
    <h4 class="text-center mb-4 text-primary">LL Exam Requests</h4>

    <?php if ($msg): ?>
        <script>
            Swal.fire({
                icon: 'info',
                title: 'Notice',
                text: <?php echo json_encode($msg); ?>
            });
        </script>
    <?php endif; ?>

    <table class="table table-bordered table-striped">
        <thead class="table-dark text-center">
            <tr>
                <th>ID</th>
                <th>Application No</th>
                <th>DOB</th>
                <th>Password</th>
                <th>State</th>
                <th>Username</th>
                <th>Order ID</th>
                <th>Status</th>
                <th>Result File</th>
                <th>Upload Result</th>
                <th>Update Status</th>
            </tr>
        </thead>
        <tbody class="text-center">
        <?php while ($row = mysqli_fetch_assoc($result)): ?>
            <tr>
                <td><?= $row['id']; ?></td>
                <td><?= $row['application_no']; ?></td>
                <td><?= $row['dob']; ?></td>
                    <td><?= $row['password']; ?></td>
                <td><?= $row['state']; ?></td>
                <td><?= $row['username']; ?></td>
                <td><?= $row['order_id']; ?></td>
                <td>
                    <?php
                        $status_text = ['0'=>'Pending','1'=>'Processing','2'=>'Completed'];
                        echo isset($status_text[$row['status']]) ? $status_text[$row['status']] : 'Unknown';
                    ?>
                </td>
                <td>
                    <?php if ($row['result_file']): ?>
                        <a href="<?= $row['result_file']; ?>" target="_blank">View</a>
                    <?php else: ?>
                        N/A
                    <?php endif; ?>
                </td>
                <td>
                    <form method="POST" enctype="multipart/form-data">
                        <input type="file" name="result_file" accept="image/*,application/pdf" required class="form-control mb-1">
                        <input type="hidden" name="history_id" value="<?= $row['id']; ?>">
                        <button type="submit" name="upload_result" class="btn btn-sm btn-success w-100">Upload</button>
                    </form>
                </td>
                <td>
                    <form method="POST">
                        <select name="status" class="form-select mb-1">
                            <option value="0" <?= ($row['status']==0)?'selected':''; ?>>Pending</option>
                            <option value="1" <?= ($row['status']==1)?'selected':''; ?>>Processing</option>
                            <option value="2" <?= ($row['status']==2)?'selected':''; ?>>Completed</option>
                        </select>
                        <input type="hidden" name="history_id" value="<?= $row['id']; ?>">
                        <button type="submit" name="update_status" class="btn btn-sm btn-primary w-100">Update</button>
                    </form>
                </td>
            </tr>
        <?php endwhile; ?>
        </tbody>
    </table>
</div>

</body>
</html>
