<?php
include "dbconnect.php";

// Get the applicant ID from the query string
$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;
if ($id <= 0) {
    die('Invalid ID');
}

// Prepare and execute query
$stmt = $con->prepare("SELECT ja.*, mr.medical_date, mr.medical_exp_date, mr.medical_upload, mr.sleep, mr.sleep_message
                       FROM job_applicants ja
                       LEFT JOIN medical_records mr ON ja.id = mr.applicant_id
                       WHERE ja.id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$applicant = $result->fetch_assoc();

if (!$applicant) {
    die('Applicant not found');
}

$medical_date = !empty($applicant['medical_date']) ? date('Y-m-d', strtotime($applicant['medical_date'])) : '';
$medical_exp_date = !empty($applicant['medical_exp_date']) ? date('Y-m-d', strtotime($applicant['medical_exp_date'])) : '';
$sleep = $applicant['sleep'] ?? 0;
$sleep_message = $applicant['sleep_message'] ?? '';
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Medical Record</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Bootstrap + Font Awesome -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">

    <style>
        body {
            background: linear-gradient(135deg, #00c9ff, #92fe9d);
            font-family: 'Segoe UI', sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            margin-top: 60px;
            max-width: 700px;
            background-color: #fff;
            padding: 30px;
            border-radius: 12px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
        }

        h3 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
        }

        label {
            font-weight: bold;
            color: #333;
        }

        .form-control {
            border-radius: 8px;
        }

        .form-check-label {
            font-weight: normal;
            color: #333;
        }

        .btn-primary {
            width: 100%;
            border-radius: 8px;
            padding: 12px;
        }

        .form-icon {
            margin-right: 8px;
            color: #007bff;
        }

        @media (max-width: 768px) {
            .container {
                margin: 20px;
                padding: 20px;
            }

            h3 {
                font-size: 22px;
            }

            .btn-primary {
                padding: 10px;
            }
        }

        .back-link {
            text-decoration: none;
            display: inline-block;
            margin-bottom: 20px;
            color: #007bff;
            font-weight: 500;
        }

        .back-link i {
            margin-right: 6px;
        }

        .current-file {
            font-size: 14px;
            color: #555;
        }
    </style>
</head>
<body>

<div class="container">
    <a href="medicallist.php" class="back-link"><i class="fas fa-arrow-left"></i> Back to Records</a>

    <h3><i class="fas fa-user-edit form-icon"></i> Edit Medical Record</h3>

    <form method="POST" action="update_medical.php" enctype="multipart/form-data">
        <input type="hidden" name="id" value="<?= $applicant['id'] ?>">

        <!-- Name -->
        <div class="mb-3">
            <label for="name"><i class="fas fa-user form-icon"></i> Name</label>
            <input type="text" id="name" name="name" class="form-control" value="<?= htmlspecialchars($applicant['name']) ?>" required>
        </div>

        <!-- Passport No -->
        <div class="mb-3">
            <label for="passport_no"><i class="fas fa-passport form-icon"></i> Passport Number</label>
            <input type="text" id="passport_no" name="passport_no" class="form-control" value="<?= htmlspecialchars($applicant['passport_no']) ?>" required>
        </div>

        <!-- Medical Date -->
        <div class="mb-3">
            <label for="medical_date"><i class="fas fa-calendar-alt form-icon"></i> Medical Date</label>
            <input type="date" id="medical_date" name="medical_date" class="form-control" value="<?= $medical_date ?>" required>
        </div>

        <!-- Medical Expiry Date -->
        <div class="mb-3">
            <label for="medical_exp_date"><i class="fas fa-calendar-check form-icon"></i> Medical Expiry Date</label>
            <input type="date" id="medical_exp_date" name="medical_exp_date" class="form-control" value="<?= $medical_exp_date ?>" required>
        </div>

        <!-- Sleep Checkbox -->
        <div class="mb-3 form-check">
            <input type="checkbox" class="form-check-input" id="sleep" name="sleep" <?= $sleep ? 'checked' : '' ?>>
            <label class="form-check-label" for="sleep"><i class="fas fa-bed form-icon"></i> Has Sleep Issues</label>
        </div>

        <!-- Sleep Message -->
        <div class="mb-3">
            <label for="sleep_message"><i class="fas fa-comment-dots form-icon"></i> Sleep Message</label>
            <textarea class="form-control" id="sleep_message" name="sleep_message" rows="3"><?= htmlspecialchars($sleep_message) ?></textarea>
        </div>

        <!-- File Upload -->
        <div class="mb-3">
            <label for="medical_upload"><i class="fas fa-file-upload form-icon"></i> Upload Medical File</label>
            <input type="file" class="form-control" name="medical_upload" id="medical_upload" accept=".pdf,.jpg,.jpeg,.png">
            <?php if (!empty($applicant['medical_upload'])): ?>
                <p class="current-file">Current File: <a href="<?= htmlspecialchars($applicant['medical_upload']) ?>" target="_blank">View</a></p>
            <?php endif; ?>
        </div>

        <!-- Submit Button -->
        <button type="submit" class="btn btn-primary"><i class="fas fa-save"></i> Update Record</button>
    </form>
</div>

</body>
</html>
