<?php
include "dbconnect.php";
$id = $_GET['id'] ?? 0;
$error = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'] ?? '';
    $phone = $_POST['phone'] ?? '';
    $passport_no = $_POST['passport_no'] ?? '';
    $country_applied = $_POST['country_applied'] ?? '';
    $job_position = $_POST['job_position'] ?? '';
    $agency_name = $_POST['agency_name'] ?? '';

    $stmt = $con->prepare("UPDATE emigrations SET name=?, phone=?, passport_no=?, country_applied=?, job_position=?, agency_name=? WHERE id=?");
    $stmt->bind_param("ssssssi", $name, $phone, $passport_no, $country_applied, $job_position, $agency_name, $id);

    if ($stmt->execute()) {
        header("Location: emigration_list.php");
        exit();
    } else {
        $error = "Update failed: " . $stmt->error;
    }
}

$stmt = $con->prepare("SELECT * FROM emigrations WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();

if (!$data) {
    die("Application not found.");
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Edit Application</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>
        /* Animated background gradient */
        body {
            min-height: 100vh;
            background: linear-gradient(135deg, #6a11cb, #2575fc, #6a11cb, #2575fc);
            background-size: 400% 400%;
            animation: gradientBG 15s ease infinite;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            padding: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
        }
        @keyframes gradientBG {
            0% {background-position: 0% 50%;}
            50% {background-position: 100% 50%;}
            100% {background-position: 0% 50%;}
        }

        /* Card container */
        .edit-card {
            background: #fff;
            border-radius: 15px;
            padding: 30px 25px;
            max-width: 480px;
            width: 100%;
            box-shadow: 0 10px 30px rgba(0,0,0,0.15);
        }

        h2 {
            font-weight: 700;
            color: #2575fc;
            text-align: center;
            margin-bottom: 25px;
        }

        /* Input styling */
        .form-control {
            border-radius: 30px;
            padding: 15px 20px;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }

        .form-control:focus {
            border-color: #6a11cb;
            box-shadow: 0 0 8px rgba(106, 17, 203, 0.3);
        }

        /* Buttons */
        .btn-primary {
            background: #6a11cb;
            border: none;
            border-radius: 30px;
            padding: 12px 25px;
            font-weight: 600;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 8px;
            transition: background 0.3s ease;
        }
        .btn-primary:hover {
            background: #4b0d8c;
        }

        .btn-secondary {
            border-radius: 30px;
            padding: 12px 25px;
            font-weight: 600;
            font-size: 1rem;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .buttons-row {
            display: flex;
            justify-content: space-between;
            margin-top: 25px;
            gap: 15px;
            flex-wrap: wrap;
        }

        /* Responsive tweaks */
        @media (max-width: 576px) {
            .edit-card {
                padding: 25px 15px;
            }

            .btn-primary, .btn-secondary {
                width: 100%;
                justify-content: center;
            }

            .buttons-row {
                flex-direction: column;
                gap: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="edit-card shadow-sm" role="main" aria-label="Edit Emigration Application Form">
        <h2><i class="bi bi-pencil-square"></i> Edit Application</h2>

        <?php if ($error): ?>
            <div class="alert alert-danger" role="alert"><?= htmlspecialchars($error) ?></div>
        <?php endif; ?>

        <form method="POST" novalidate>
            <label for="name" class="form-label visually-hidden">Name</label>
            <input type="text" id="name" name="name" class="form-control mb-3" placeholder="Full Name" value="<?= htmlspecialchars($data['name']) ?>" required autocomplete="name" />

            <label for="phone" class="form-label visually-hidden">Phone</label>
            <input type="tel" id="phone" name="phone" class="form-control mb-3" placeholder="Phone Number" value="<?= htmlspecialchars($data['phone']) ?>" required autocomplete="tel" />

            <label for="passport_no" class="form-label visually-hidden">Passport Number</label>
            <input type="text" id="passport_no" name="passport_no" class="form-control mb-3" placeholder="Passport Number" value="<?= htmlspecialchars($data['passport_no']) ?>" required autocomplete="off" />

            <label for="country_applied" class="form-label visually-hidden">Country Applied</label>
            <input type="text" id="country_applied" name="country_applied" class="form-control mb-3" placeholder="Country Applied" value="<?= htmlspecialchars($data['country_applied']) ?>" required autocomplete="off" />

            <label for="job_position" class="form-label visually-hidden">Job Position</label>
            <input type="text" id="job_position" name="job_position" class="form-control mb-3" placeholder="Job Position" value="<?= htmlspecialchars($data['job_position']) ?>" required autocomplete="off" />

            <label for="agency_name" class="form-label visually-hidden">Agency Name</label>
            <input type="text" id="agency_name" name="agency_name" class="form-control mb-4" placeholder="Agency Name" value="<?= htmlspecialchars($data['agency_name']) ?>" required autocomplete="off" />

            <div class="buttons-row">
                <button type="submit" class="btn btn-primary" aria-label="Update Application">
                    <i class="bi bi-save-fill"></i> Update
                </button>
                <a href="emigration_list.php" class="btn btn-secondary" aria-label="Cancel and go back to list">
                    <i class="bi bi-x-circle-fill"></i> Cancel
                </a>
            </div>
        </form>
    </div>

    <!-- Bootstrap JS Bundle -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
