<?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 + Bootstrap Icons -->
    <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: linear-gradient(to right, #f0f8ff, #ffffff);
            font-family: 'Segoe UI', sans-serif;
            padding-bottom: 40px;
        }
        .form-container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            max-width: 600px;
            margin: 60px auto 40px auto;
        }
        h3 {
            color: #007bff;
            font-weight: 700;
            text-align: center;
            margin-bottom: 30px;
        }
        .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;
            transition: background-color 0.3s ease;
        }
        .logout-btn:hover {
            background-color: #c82333;
        }
        label .bi {
            margin-right: 6px;
            vertical-align: middle;
        }
        /* Status option colors */
        option.done {
            background-color: #d4edda;
            color: #155724;
            font-weight: 600;
        }
        option.notdone {
            background-color: #f8d7da;
            color: #721c24;
            font-weight: 600;
        }
        @media (max-width: 576px) {
            .form-container {
                margin: 30px 15px 30px 15px;
                padding: 20px;
            }
        }
    </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" novalidate>
            <input type="hidden" name="id" value="<?= $applicant['id'] ?>">

            <!-- Name -->
            <div class="mb-3">
                <label for="name" class="form-label"><i class="bi bi-person-fill"></i>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"><i class="bi bi-card-text"></i>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"><i class="bi bi-calendar-event-fill"></i>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"><i class="bi bi-check2-circle"></i>Status:</label>
                <select id="status" name="status" class="form-select" required>
                    <option value="" disabled <?= $status === '' ? 'selected' : '' ?>>-- Select Status --</option>
                    <option value="done" class="done" <?= $status == 'done' ? 'selected' : '' ?>>Done</option>
                    <option value="not done" class="notdone" <?= $status == 'not done' ? 'selected' : '' ?>>Not Done</option>
                </select>
            </div>

            <!-- Submit -->
            <button type="submit" class="btn btn-primary">Update Appointment</button>
        </form>
    </div>
</div>

<!-- Bootstrap JS Bundle (Popper + Bootstrap) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>

</body>
</html>
