<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION["role"]) || $_SESSION["role"] !== "superadmin") {
    header("Location: ../html/index.php");
    exit();
}

require_once "../php/db_connect.php";

$id = $_GET['id'] ?? null;

if (!$id || !is_numeric($id)) {
    die("Invalid applicant ID.");
}

$stmt = $conn->prepare("SELECT * FROM users WHERE id = ? AND role = 'admin'");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    die("Applicant not found.");
}

$applicant = $result->fetch_assoc();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>View Applicant</title>
    <link rel="stylesheet" href="../css/applicant-request.css?v=1000">
    <style>
        body{
            font-family: Arial, Helvetica, sans-serif;
            background:#d9d9d9;
            margin:0;
            padding:30px;
        }

        .details-card{
            max-width:900px;
            margin:0 auto;
            background:#fff;
            border-radius:20px;
            padding:30px;
            box-shadow:0 4px 12px rgba(0,0,0,0.08);
        }

        .details-card h1{
            margin-bottom:10px;
            font-size:34px;
        }

        .details-card p.subtitle{
            color:#555;
            margin-bottom:25px;
            font-size:16px;
        }

        .details-grid{
            display:grid;
            grid-template-columns: 220px 1fr;
            gap:14px 20px;
        }

        .label{
            font-weight:bold;
            color:#222;
        }

        .value{
            color:#333;
            word-break:break-word;
        }

        .actions{
            margin-top:30px;
            display:flex;
            gap:12px;
            flex-wrap:wrap;
        }

        .btn{
            display:inline-block;
            padding:10px 18px;
            border-radius:10px;
            text-decoration:none;
            color:#fff;
            font-size:15px;
        }

        .approve-btn{ background:#2ecc71; }
        .reject-btn{ background:#e74c3c; }
        .back-btn{ background:#3498db; }

        .shop-preview{
    width: 220px;
    max-width: 100%;
    height: auto;
    border-radius: 12px;
    border: 1px solid #ddd;
    display: block;
}
.view-shop-link{
    display:inline-block;
    padding:8px 14px;
    background:#3498db;
    color:#fff;
    text-decoration:none;
    border-radius:8px;
    font-size:14px;
}

.view-shop-link:hover{
    background:#2c80b9;
}
    </style>
</head>
<body>

    <div class="details-card">
        <h1>Applicant Details</h1>
        <p class="subtitle">Review full information for this applicant.</p>

        <div class="details-grid">
            <div class="label">ID</div>
            <div class="value"><?php echo htmlspecialchars($applicant['id'] ?? ''); ?></div>

            <div class="label">First Name</div>
            <div class="value"><?php echo htmlspecialchars($applicant['first_name'] ?? ''); ?></div>

            <div class="label">Last Name</div>
            <div class="value"><?php echo htmlspecialchars($applicant['last_name'] ?? ''); ?></div>

            <div class="label">Email</div>
            <div class="value"><?php echo htmlspecialchars($applicant['email'] ?? ''); ?></div>

            <div class="label">Mobile Number</div>
            <div class="value"><?php echo htmlspecialchars($applicant['mobile_number'] ?? ''); ?></div>

            <div class="label">Birthdate</div>
            <div class="value"><?php echo htmlspecialchars($applicant['birthdate'] ?? ''); ?></div>

            <div class="label">Address</div>
            <div class="value"><?php echo htmlspecialchars($applicant['address'] ?? ''); ?></div>

            <div class="label">Shop Name</div>
            <div class="value"><?php echo htmlspecialchars($applicant['shop_name'] ?? ''); ?></div>

            <div class="label">Shop Type</div>
            <div class="value"><?php echo htmlspecialchars($applicant['shop_type'] ?? ''); ?></div>

            <div class="label">Main Services</div>
            <div class="value"><?php echo htmlspecialchars($applicant['main_services'] ?? ''); ?></div>

      
<div class="label">Shop Image</div>
<div class="value">
    <?php if (!empty($applicant['shop_image'])): ?>
        <a href="uploads/<?php echo htmlspecialchars($applicant['shop_image']); ?>"
           target="_blank"
           class="view-shop-link">
           View Shop Image
        </a>
    <?php else: ?>
        No shop image uploaded.
    <?php endif; ?>
</div>

            <div class="label">Business Permit Number</div>
            <div class="value"><?php echo htmlspecialchars($applicant['business_permit_number'] ?? ''); ?></div>

            <div class="label">Role</div>
            <div class="value"><?php echo htmlspecialchars($applicant['role'] ?? ''); ?></div>

            <div class="label">Status</div>
            <div class="value"><?php echo htmlspecialchars($applicant['status'] ?? ''); ?></div>

            <div class="label">Email Verified</div>
            <div class="value"><?php echo htmlspecialchars($applicant['email_verified'] ?? ''); ?></div>
        </div>

        <div class="actions">
            <a class="btn back-btn" href="superadmin.php?page=applicants">Back</a>

           <a class="btn approve-btn"
     href="update-applicant-status.php?id=<?php echo $applicant['id']; ?>&action=approve"
     onclick="return confirm('Approve this applicant?');">
     Approve
    </a>

<a class="btn reject-btn"
   href="update-applicant-status.php?id=<?php echo $applicant['id']; ?>&action=reject"
   onclick="return confirm('Reject this applicant?');">
   Reject
</a>
        </div>
    </div>

</body>
</html>