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

$msg = '';

// ================= HANDLE CERTIFICATE UPLOAD =================
if (isset($_POST['upload_cert'])) {
    $id = intval($_POST['history_id']);
    $file = $_FILES['certificate']['name'];
    $upload_dir = "new/vcc_cert/";

    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['certificate']['tmp_name'], $file_path)) {
            mysqli_query(
                $ahk_conn,
                "UPDATE vcchistory SET cert='$file_path', status=2 WHERE id='$id'"
            );
            $msg = "VCC certificate uploaded successfully!";
        } else {
            $msg = "Error uploading certificate.";
        }
    }
}

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

// ================= FETCH PENDING / PROCESSING VCC REQUESTS =================
$result = mysqli_query($ahk_conn, "
    SELECT * 
    FROM vcchistory 
    WHERE status IN (0,1)
    ORDER BY id DESC
");
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VCC 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>

<style>
.status-badge {
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: bold;
}
.bg-pending { background:#0d6efd; color:#fff; }
.bg-processing { background:#ffc107; color:#000; }
.bg-completed { background:#198754; color:#fff; }
</style>
</head>

<body class="bg-light">
<div class="container mt-5">

<h4 class="text-center mb-4 text-primary">VCC Requests – Admin Panel</h4>

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

<table class="table table-bordered table-striped table-hover align-middle text-center">
<thead class="table-dark">
<tr>
    <th>ID</th>
    <th>Name</th>
    <th>EPIC No</th>
    <th>Order ID</th>
    <th>User</th>
    <th>Date</th>
    <th>Status</th>
    <th>Certificate</th>
    <th>Upload Certificate</th>
    <th>Update Status</th>
</tr>
</thead>

<tbody>
<?php while ($row = mysqli_fetch_assoc($result)): ?>
<tr>
    <td><?= $row['id']; ?></td>
    <td><?= htmlspecialchars($row['name']); ?></td>
    <td><?= htmlspecialchars($row['epicno']); ?></td>
    <td><?= htmlspecialchars($row['order_id']); ?></td>
    <td><?= htmlspecialchars($row['username']); ?></td>
    <td><?= htmlspecialchars($row['date']); ?></td>
    <td>
        <?php
            $status = (int)$row['status'];
            $labels = ['Pending','Processing','Completed'];
            $classes = ['bg-pending','bg-processing','bg-completed'];
            echo "<span class='status-badge {$classes[$status]}'>{$labels[$status]}</span>";
        ?>
    </td>
    <td>
        <?php if (!empty($row['cert'])): ?>
            <a href="<?= $row['cert']; ?>" target="_blank">View</a>
        <?php else: ?>
            N/A
        <?php endif; ?>
    </td>

    <!-- Upload Certificate -->
    <td>
        <form method="POST" enctype="multipart/form-data">
            <input type="file" name="certificate" accept="image/*,application/pdf" required class="form-control mb-1">
            <input type="hidden" name="history_id" value="<?= $row['id']; ?>">
            <button type="submit" name="upload_cert" class="btn btn-sm btn-success w-100">
                Upload
            </button>
        </form>
    </td>

    <!-- Update Status -->
    <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>
