<?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" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Emigration Application Form</title>

    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <!-- Bootstrap Icons -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet" />

    <style>
        /* Background gradient with subtle animation */
        body {
            background: linear-gradient(135deg, #6a11cb, #2575fc);
            background-size: 400% 400%;
            animation: gradientBG 15s ease infinite;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            min-height: 100vh;
            color: #fff;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        @keyframes gradientBG {
            0% {background-position: 0% 50%;}
            50% {background-position: 100% 50%;}
            100% {background-position: 0% 50%;}
        }

        .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;
            z-index: 1000;
            transition: background-color 0.3s ease;
        }
        .logout-btn:hover {
            background-color: #c0392b;
        }

        .container {
            max-width: 500px;
            background: #ffffff;
            padding: 30px 25px 35px 25px;
            border-radius: 15px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
            color: #333;
        }

        h4 {
            color: #007bff;
            text-align: center;
            margin-bottom: 25px;
            font-weight: 700;
        }

        /* Form group with icons */
        .input-group-text {
            background-color: #007bff;
            border: none;
            color: #fff;
            border-radius: 10px 0 0 10px;
            font-size: 18px;
        }

        .form-control {
            border-radius: 0 10px 10px 0;
            border: 1px solid #007bff;
            padding: 12px 15px;
            font-size: 16px;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
        }

        .form-control:focus {
            border-color: #28a745;
            box-shadow: 0 0 8px rgba(40, 167, 69, 0.7);
            outline: none;
        }

        .btn-primary {
            background-color: #007bff;
            border: none;
            border-radius: 25px;
            padding: 12px;
            width: 100%;
            font-size: 18px;
            font-weight: 600;
            transition: background-color 0.3s ease;
        }

        .btn-primary:hover {
            background-color: #0056b3;
        }

        .error-message,
        .success-message {
            padding: 12px;
            margin-bottom: 20px;
            border-radius: 8px;
            text-align: center;
            font-weight: 600;
            font-size: 14px;
        }

        .error-message {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .success-message {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        /* Responsive adjustments */
        @media (max-width: 576px) {
            .container {
                padding: 25px 20px 30px 20px;
                max-width: 90vw;
            }

            .btn-primary {
                font-size: 16px;
            }
        }
    </style>
</head>
<body>

<div class="container shadow-sm">
    <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" novalidate>
        <!-- Name -->
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
            <input type="text" name="name" class="form-control" placeholder="Full Name" required />
        </div>

        <!-- Phone -->
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-telephone-fill"></i></span>
            <input type="tel" name="phone" class="form-control" placeholder="Phone Number" required pattern="[0-9+\-\s]{7,15}" />
        </div>

        <!-- Passport Number -->
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-credit-card-2-front-fill"></i></span>
            <input type="text" name="passport_no" class="form-control" placeholder="Passport Number" required />
        </div>

        <!-- Country Applying For -->
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-geo-alt-fill"></i></span>
            <input type="text" name="country_applied" class="form-control" placeholder="Country Applying For" required />
        </div>

        <!-- Job Position -->
        <div class="input-group mb-3">
            <span class="input-group-text"><i class="bi bi-briefcase-fill"></i></span>
            <input type="text" name="job_position" class="form-control" placeholder="Job Position" required />
        </div>

        <!-- Agency Name -->
        <div class="input-group mb-4">
            <span class="input-group-text"><i class="bi bi-building"></i></span>
            <input type="text" name="agency_name" class="form-control" placeholder="Agency Name" required />
        </div>

        <button type="submit" class="btn btn-primary btn-lg">
            <i class="bi bi-send-fill me-2"></i> Submit Application
        </button>
    </form>
</div>

<!-- Bootstrap JS (optional for some interactive components) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
