<?php
// Start session
session_start();

// Enable error reporting for debugging
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Include the PDO config file for database connection
include($_SERVER['DOCUMENT_ROOT'] . '/config.php'); 

// Check if the admin is logged in
if (!isset($_SESSION['admin_id'])) {
    header('Location: admin_login.php');
    exit();
}

// Handle status updates
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['update_status'])) {
    $kycId = $_POST['kyc_id'];
    $newStatus = $_POST['status'];

    $updateQuery = "UPDATE kyc_requests SET status = :status WHERE id = :id";
    $stmt = $conn->prepare($updateQuery);
    $stmt->bindParam(':status', $newStatus);
    $stmt->bindParam(':id', $kycId, PDO::PARAM_INT);

    if ($stmt->execute()) {
        $message = "KYC status updated successfully.";
    } else {
        $message = "Error updating KYC status.";
    }
}

// Fetch all KYC requests
$query = "SELECT * FROM kyc_requests ORDER BY created_at DESC";
$stmt = $conn->prepare($query);
$stmt->execute();
$kycRequests = $stmt->fetchAll(PDO::FETCH_ASSOC);

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin KYC Requests</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            padding: 20px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }

        th, td {
            padding: 12px;
            border: 1px solid #ccc;
            text-align: left;
        }

        th {
            background-color: #007bff;
            color: white;
        }

        .status-pending { color: orange; }
        .status-approved { color: green; }
        .status-rejected { color: red; }

        .details {
            margin: 20px 0;
            padding: 10px;
            background: #f8f9fa;
            border: 1px solid #ddd;
        }

        .btn {
            padding: 8px 12px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-transform: uppercase;
            font-weight: bold;
        }

        .btn-approve {
            background-color: green;
            color: white;
        }

        .btn-reject {
            background-color: red;
            color: white;
        }

        .btn-pending {
            background-color: orange;
            color: white;
        }

        .btn:hover {
            opacity: 0.9;
        }

        .file-link {
            color: blue;
            text-decoration: underline;
        }

        .file-link:hover {
            text-decoration: none;
        }
    </style>
</head>
<body>
    <h1>Admin KYC Requests</h1>

    <?php if (!empty($message)): ?>
        <p><?= htmlspecialchars($message); ?></p>
    <?php endif; ?>

    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>User ID</th>
                <th>Full Name</th>
                <th>Phone Number</th>
                <th>Medicare</th>
                <th>Status</th>
                <th>ID Card Front</th>
                <th>ID Card Back</th>
                <th>Birth Certificate</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($kycRequests as $request): ?>
                <tr>
                    <td><?= htmlspecialchars($request['id']); ?></td>
                    <td><?= htmlspecialchars($request['user_id']); ?></td>
                    <td><?= htmlspecialchars($request['full_name']); ?></td>
                    <td><?= htmlspecialchars($request['phone_number']); ?></td>
                    <td><?= htmlspecialchars($request['medicare']); ?></td>
                    <td>
                        <span class="status-<?= htmlspecialchars($request['status']); ?>">
                            <?= htmlspecialchars(ucfirst($request['status'])); ?>
                        </span>
                    </td>
                    <td>
                        <!-- Display link to ID Card Front Image from the root directory -->
                        <a href="/uploads/kyc/<?= htmlspecialchars($request['id_card_front']); ?>" target="_blank" class="file-link">View</a>
                    </td>
                    <td>
                        <!-- Display link to ID Card Back Image from the root directory -->
                        <a href="/uploads/kyc/<?= htmlspecialchars($request['id_card_back']); ?>" target="_blank" class="file-link">View</a>
                    </td>
                    <td>
                        <!-- Display link to Birth Certificate Image from the root directory -->
                        <a href="/uploads/kyc/<?= htmlspecialchars($request['birth_certificate']); ?>" target="_blank" class="file-link">View</a>
                    </td>
                    <td>
                        <form action="" method="POST" style="display: inline;">
                            <input type="hidden" name="kyc_id" value="<?= htmlspecialchars($request['id']); ?>">
                            <select name="status" required>
                                <option value="pending" <?= $request['status'] === 'pending' ? 'selected' : ''; ?>>Pending</option>
                                <option value="approved" <?= $request['status'] === 'approved' ? 'selected' : ''; ?>>Approved</option>
                                <option value="rejected" <?= $request['status'] === 'rejected' ? 'selected' : ''; ?>>Rejected</option>
                            </select>
                            <button type="submit" name="update_status" class="btn">Update</button>
                        </form>
                    </td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</body>
</html>
