<?php
include "dbconnect.php";
$id = $_GET['id'];
$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();
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Edit Application</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css" rel="stylesheet" />
    <style>
        body {
            background-color: #eef2f7;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            min-height: 100vh;
            padding-top: 80px;
            margin: 0;
        }

        .container {
            max-width: 600px;
            background: #ffffff;
            padding: 35px 40px;
            border-radius: 10px;
            box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
        }

        h2 {
            color: #0d6efd;
            font-weight: 700;
            margin-bottom: 30px;
            text-align: center;
        }

        .input-group-text {
            background-color: #0d6efd;
            color: white;
            border-radius: 0.375rem 0 0 0.375rem;
            border: none;
        }

        .form-control {
            border-radius: 0 0.375rem 0.375rem 0;
            border-left: none;
        }

        .btn-primary {
            background-color: #0d6efd;
            border: none;
            padding: 12px 25px;
            font-weight: 600;
            border-radius: 6px;
            width: 100%;
            font-size: 1.1rem;
            box-shadow: 0 4px 12px rgba(13, 110, 253, 0.4);
            transition: background-color 0.3s ease;
            margin-bottom: 10px;
        }

        .btn-primary:hover,
        .btn-primary:focus {
            background-color: #084298;
            box-shadow: 0 6px 14px rgba(8, 66, 152, 0.6);
        }

        .btn-secondary {
            width: 100%;
            padding: 12px 25px;
            font-weight: 600;
            border-radius: 6px;
            font-size: 1.1rem;
        }

        .alert-danger {
            font-weight: 600;
            margin-bottom: 20px;
        }

        /* Logout button */
        .logout-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            background-color: #dc3545;
            color: white;
            border-radius: 6px;
            padding: 10px 22px;
            font-weight: 700;
            box-shadow: 0 2px 10px rgba(220, 53, 69, 0.5);
            text-decoration: none;
            transition: background-color 0.3s ease;
            z-index: 1000;
        }
        .logout-btn:hover,
        .logout-btn:focus {
            background-color: #a71d2a;
            color: white;
            text-decoration: none;
        }

        /* Responsive */
        @media (max-width: 767px) {
            body {
                padding-top: 100px;
                padding-left: 15px;
                padding-right: 15px;
            }
            .container {
                padding: 25px 20px;
                max-width: 100%;
            }
            h2 {
                font-size: 2rem;
                margin-bottom: 25px;
            }
            .btn-primary,
            .btn-secondary {
                font-size: 1rem;
                padding: 12px;
            }
            .logout-btn {
                padding: 8px 16px;
                font-size: 0.9rem;
                top: 15px;
                right: 15px;
            }
        }
    </style>
</head>
<body>

<!-- Logout button -->
<a href="logout.php" class="logout-btn"><i class="bi bi-box-arrow-right"></i> Logout</a>

<div class="container">
    <h2>Edit Emigration Application</h2>

    <?php if ($error): ?>
        <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
    <?php endif; ?>

    <form method="POST" autocomplete="off">
        <div class="mb-3 input-group">
            <span class="input-group-text"><i class="bi bi-person-fill"></i></span>
            <input type="text" name="name" class="form-control" placeholder="Name" value="<?= htmlspecialchars($data['name']) ?>" required />
        </div>
        <div class="mb-3 input-group">
            <span class="input-group-text"><i class="bi bi-telephone-fill"></i></span>
            <input type="text" name="phone" class="form-control" placeholder="Phone" value="<?= htmlspecialchars($data['phone']) ?>" required />
        </div>
        <div class="mb-3 input-group">
            <span class="input-group-text"><i class="bi bi-passport-fill"></i></span>
            <input type="text" name="passport_no" class="form-control" placeholder="Passport Number" value="<?= htmlspecialchars($data['passport_no']) ?>" required />
        </div>
        <div class="mb-3 input-group">
            <span class="input-group-text"><i class="bi bi-globe2"></i></span>
            <input type="text" name="country_applied" class="form-control" placeholder="Country Applied" value="<?= htmlspecialchars($data['country_applied']) ?>" required />
        </div>
        <div class="mb-3 input-group">
            <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" value="<?= htmlspecialchars($data['job_position']) ?>" required />
        </div>
        <div class="mb-4 input-group">
            <span class="input-group-text"><i class="bi bi-building"></i></span>
            <input type="text" name="agency_name" class="form-control" placeholder="Agency Name" value="<?= htmlspecialchars($data['agency_name']) ?>" required />
        </div>

        <button type="submit" class="btn btn-primary">Update</button>
        <a href="emigration_list.php" class="btn btn-secondary">Cancel</a>
    </form>
</div>

</body>
</html>
