<?php
include "dbconnect.php";

// Ensure that the 'id' parameter is passed and valid
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    die('Invalid ID');
}

// Fetch the applicant record and corresponding appointment details
$sql = "SELECT ja.*, a.appointment_date, a.status 
        FROM job_applicants ja
        LEFT JOIN appointments a ON ja.id = a.applicant_id
        WHERE ja.id = $id";

$result = $con->query($sql);
$applicant = $result->fetch_assoc();

// If no record is found
if (!$applicant) {
    die('Applicant not found');
}

// Safely fetch the appointment date and status
$appointment_date = isset($applicant['appointment_date']) ? date('Y-m-d', strtotime($applicant['appointment_date'])) : '';
$status = isset($applicant['status']) ? $applicant['status'] : ''; // Default to empty if not set
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Appointment</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">

    <style>
        body {
            background: linear-gradient(to right, #f0f8ff, #ffffff);
            font-family: 'Segoe UI', sans-serif;
        }

        .form-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0px 5px 15px rgba(0, 0, 0, 0.1);
            max-width: 600px;
            margin: 60px auto 40px auto;
        }

        h3 {
            color: #007bff;
            font-weight: bold;
            text-align: center;
            margin-bottom: 25px;
        }

        .btn-primary {
            width: 100%;
            font-weight: 600;
            padding: 12px;
        }

        .logout-btn {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 999;
            background-color: #dc3545;
            color: #fff;
            padding: 8px 14px;
            border: none;
            border-radius: 6px;
        }

        .logout-btn:hover {
            background-color: #c82333;
        }

        @media (max-width: 768px) {
            .logout-btn {
                top: 10px;
                right: 10px;
                font-size: 14px;
                padding: 6px 10px;
            }
        }
    </style>
</head>
<body>

<!-- Logout Button -->
<form method="POST" action="logout.php">
    <button type="submit" class="logout-btn">Logout</button>
</form>

<div class="container">
    <div class="form-container">
        <h3>Edit Appointment</h3>
        <form method="POST" action="update_appointment.php">
            <input type="hidden" name="id" value="<?= $applicant['id'] ?>">

            <!-- Name -->
            <div class="mb-3">
                <label for="name" class="form-label">Name:</label>
                <input type="text" id="name" name="name" class="form-control" value="<?= htmlspecialchars($applicant['name']) ?>" readonly>
            </div>

            <!-- Passport -->
            <div class="mb-3">
                <label for="passport_no" class="form-label">Passport No:</label>
                <input type="text" id="passport_no" name="passport_no" class="form-control" value="<?= htmlspecialchars($applicant['passport_no']) ?>" readonly>
            </div>

            <!-- Appointment Date -->
            <div class="mb-3">
                <label for="appointment_date" class="form-label">Appointment Date:</label>
                <input type="date" id="appointment_date" name="appointment_date" class="form-control" value="<?= $appointment_date ?>" required>
            </div>

            <!-- Status -->
            <div class="mb-4">
                <label for="status" class="form-label">Status:</label>
                <select id="status" name="status" class="form-select" required>
                    <option value="done" <?= $status == 'done' ? 'selected' : '' ?>>Done</option>
                    <option value="not done" <?= $status == 'not done' ? 'selected' : '' ?>>Not Done</option>
                </select>
            </div>

            <!-- Submit -->
            <button type="submit" class="btn btn-primary">Update Appointment</button>
        </form>
    </div>
</div>

</body>
</html>
