<?php
session_start();
if (!isset($_SESSION["admin_logged_in"])) {
    header("Location: admin_login.php");
    exit();
}

$conn = new mysqli("localhost", "root", "", "hesed_admissions");
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Handle approve/disapprove
if (isset($_GET['action']) && isset($_GET['id'])) {
    $status = $_GET['action'] === 'approve' ? 'Approved' : 'Disapproved';

    $stmt = $conn->prepare("UPDATE applications SET status=? WHERE id=?");
    $stmt->bind_param("si", $status, $_GET['id']);
    $stmt->execute();

    $_SESSION['email_success'] = "Status updated successfully.";

    header("Location: dashboard.php");
    exit();
}

// Fetch application data
$filter = $_GET['filter'] ?? 'all';
switch ($filter) {
    case 'approved':
        $applications = $conn->query("SELECT * FROM applications WHERE status='Approved' ORDER BY id DESC");
        break;
    case 'disapproved':
        $applications = $conn->query("SELECT * FROM applications WHERE status='Disapproved' ORDER BY id DESC");
        break;
    default:
        $applications = $conn->query("SELECT * FROM applications ORDER BY id DESC");
        break;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Admin Dashboard</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background: #eef2f5;
            padding: 20px;
            margin: 0;
        }
        h2 {
            text-align: center;
            margin-bottom: 20px;
            color: #333;
        }
        .filter-print {
            text-align: center;
            margin-bottom: 15px;
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            gap: 10px;
        }
        .filter-print form {
            display: inline-block;
        }
        select {
            padding: 5px 8px;
            font-size: 16px;
            border-radius: 4px;
            border: 1px solid #ccc;
            cursor: pointer;
        }
        .btn {
            padding: 6px 14px;
            border-radius: 4px;
            background: #28a745;
            color: white;
            border: none;
            cursor: pointer;
            text-decoration: none;
            font-size: 14px;
            display: inline-block;
            white-space: nowrap;
        }
        .btn.disapprove {
            background: #dc3545;
        }
        .btn.print {
            background: #6c757d;
        }
        .status {
            font-weight: bold;
        }
        .message {
            margin: 15px 0;
            text-align: center;
            font-weight: bold;
            font-size: 16px;
        }
        .message.success {
            color: green;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            background: white;
            box-shadow: 0 0 10px rgba(0,0,0,0.05);
        }
        th, td {
            padding: 10px;
            border: 1px solid #ddd;
            font-size: 14px;
            text-align: center;
        }
        th {
            background: #007bff;
            color: white;
        }

        /* Responsive: make table horizontally scrollable */
        .table-wrapper {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            margin-bottom: 20px;
        }

        @media (max-width: 600px) {
            body {
                padding: 10px;
            }
            th, td {
                font-size: 12px;
                padding: 8px;
            }
            .btn {
                padding: 5px 10px;
                font-size: 12px;
            }
            .filter-print {
                flex-direction: column;
                align-items: center;
            }
        }
    </style>
</head>
<body>

<h2>Hesed Academy Applications Dashboard</h2>

<?php if (isset($_SESSION['email_success'])): ?>
    <div class="message success"><?= htmlspecialchars($_SESSION['email_success']) ?></div>
    <?php unset($_SESSION['email_success']); ?>
<?php endif; ?>

<div class="filter-print">
    <form method="get">
        <label for="filter">Filter:</label>
        <select name="filter" id="filter" onchange="this.form.submit()">
            <option value="all" <?= $filter === 'all' ? 'selected' : '' ?>>All</option>
            <option value="approved" <?= $filter === 'approved' ? 'selected' : '' ?>>Approved</option>
            <option value="disapproved" <?= $filter === 'disapproved' ? 'selected' : '' ?>>Disapproved</option>
        </select>
    </form>
    <button onclick="window.print()" class="btn print">Print List</button>
</div>

<div class="table-wrapper">
<table>
    <thead>
    <tr>
        <th>Form No</th>
        <th>Name</th>
        <th>Class</th>
        <th>Passport</th>
        <th>Receipt</th>
        <th>Date</th>
        <th>Status</th>
        <th>Action</th>
    </tr>
    </thead>
    <tbody>
    <?php while ($row = $applications->fetch_assoc()): ?>
        <tr>
            <td><?= htmlspecialchars($row["form_number"]) ?></td>
            <td><?= htmlspecialchars($row["full_name"]) ?></td>
            <td><?= htmlspecialchars($row["class_intended"]) ?></td>
            <td>
                <?= !empty($row['passport_path'])
                    ? "<a href='" . htmlspecialchars($row['passport_path']) . "' target='_blank'>View</a>"
                    : "<span class='text-muted'>Not Uploaded</span>"; ?>
            </td>
            <td>
                <?= !empty($row['receipt_path']) && file_exists($row['receipt_path'])
                    ? "<a href='" . htmlspecialchars($row['receipt_path']) . "' target='_blank'>View</a>"
                    : "<span class='text-muted'>Not Uploaded</span>"; ?>
            </td>
            <td><?= htmlspecialchars($row["created_at"]) ?></td>
            <td class="status"><?= htmlspecialchars($row["status"] ?? "Pending") ?></td>
            <td>
                <a class="btn" href="?action=approve&id=<?= $row['id'] ?>" onclick="return confirm('Approve this application?');">Approve</a>
                <a class="btn disapprove" href="?action=disapprove&id=<?= $row['id'] ?>" onclick="return confirm('Disapprove this application?');">Disapprove</a>

            </td>
        </tr>
    <?php endwhile; ?>
    </tbody>
</table>
</div>

</body>
</html>
