<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

include "dbconnect.php"; // Your database connection

$error_message = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST['name']);
    $phone = trim($_POST['phone']);
    $passport_no = trim($_POST['passport_no']);
    $country_applied = trim($_POST['country_applied']);
    $job_position = trim($_POST['job_position']);
    $agency_name = trim($_POST['agency_name']);

    // Duplicate Check
    $stmt = $con->prepare("SELECT id FROM emigrations WHERE passport_no = ?");
    $stmt->bind_param("s", $passport_no);
    $stmt->execute();
    $stmt->store_result();

    if ($stmt->num_rows > 0) {
        $error_message = "⚠️ This Passport Number is already registered.";
    } else {
        $sql = "INSERT INTO emigrations (name, phone, passport_no, country_applied, job_position, agency_name) 
                VALUES (?, ?, ?, ?, ?, ?)";
        $stmt = $con->prepare($sql);
        $stmt->bind_param("ssssss", $name, $phone, $passport_no, $country_applied, $job_position, $agency_name);

        if ($stmt->execute()) {
            $error_message = "✅ Emigration Application Submitted Successfully!";
        } else {
            $error_message = "⚠️ Error: " . $stmt->error;
        }
    }

    $stmt->close();
    $con->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <a href="logout.php" class="logout-btn">Logout</a>
    <meta charset="UTF-8">
    <title>Emigration Application Form</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <style>
        body {
            background: linear-gradient(45deg, #6a11cb, #2575fc);
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding-top: 50px;
            color: #fff;
        }

        .logout-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            font-size: 16px;
            padding: 10px 20px;
            border-radius: 25px;
            background-color: #e74a3b;
            color: #fff;
            text-decoration: none;
        }

        .container {
            max-width: 700px;
            background: #ffffff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1);
            margin: 20px auto;
            position: relative;
            z-index: 2;
        }

        h4 {
            color: #007bff;
            text-align: center;
            margin-bottom: 25px;
        }

        .form-control {
            border-radius: 10px;
            border: 1px solid #007bff;
            padding: 12px;
            margin-bottom: 15px;
        }

        .form-control:focus {
            border-color: #28a745;
            box-shadow: 0 0 5px rgba(40, 167, 69, 0.6);
        }

        .btn-primary {
            background-color: #007bff;
            border: none;
            border-radius: 25px;
            padding: 12px;
            width: 100%;
            font-size: 16px;
        }

        .btn-primary:hover {
            background-color: #0056b3;
        }

        .error-message,
        .success-message {
            padding: 10px;
            margin-bottom: 15px;
            border-radius: 5px;
            text-align: center;
        }

        .error-message {
            background-color: #f8d7da;
            color: #721c24;
        }

        .success-message {
            background-color: #d4edda;
            color: #155724;
        }
    </style>
</head>
<body>

<div class="container">
    <h4>Emigration Application Form</h4>
    <?php if (!empty($error_message)) { ?>
    <div class="<?php echo strpos($error_message, '✅') !== false ? 'success-message' : 'error-message'; ?>">
        <?php echo $error_message; ?>
    </div>
    <?php } ?>

    <form action="" method="POST">
        <input type="text" name="name" class="form-control" placeholder="Full Name" required>
        <input type="text" name="phone" class="form-control" placeholder="Phone Number" required>
        <input type="text" name="passport_no" class="form-control" placeholder="Passport Number" required>
        <input type="text" name="country_applied" class="form-control" placeholder="Country Applying For" required>
        <input type="text" name="job_position" class="form-control" placeholder="Job Position" required>
        <input type="text" name="agency_name" class="form-control" placeholder="Agency Name" required>
        <button type="submit" class="btn btn-primary">Submit Application</button>
    </form>
</div>

</body>
</html>
