<?php
include "dbconnect.php";  // Include your database connection file

// Check if the ID is provided in the URL
if (!isset($_GET['id']) || empty($_GET['id'])) {
    echo "Record not found!";
    exit;
}

$id = $_GET['id'];

// Fetch the record from the database
$sql = "SELECT * FROM medical_records WHERE id = ?";
$stmt = $con->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows === 0) {
    echo "Record not found!";
    exit;
}

$record = $result->fetch_assoc();

// Handle the form submission to update the record
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $applicant_id = $_POST['applicant_id'];
    $medical_date = $_POST['medical_date'];
    $medical_exp_date = $_POST['medical_exp_date'];
    $sleep_details = $_POST['sleep'];

    // File upload handling (if any)
    $uploaded_file = $_FILES['medical_upload']['name'];
    if ($uploaded_file) {
        // Define the file upload path
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($uploaded_file);
        move_uploaded_file($_FILES['medical_upload']['tmp_name'], $target_file);
    } else {
        // If no file was uploaded, retain the previous file
        $uploaded_file = $record['medical_upload'];
    }

    // Update the record in the database
    $update_sql = "UPDATE medical_records SET applicant_id = ?, medical_date = ?, medical_exp_date = ?, medical_upload = ?, sleep = ? WHERE id = ?";
    $stmt = $con->prepare($update_sql);
    $stmt->bind_param("sssssi", $applicant_id, $medical_date, $medical_exp_date, $uploaded_file, $sleep_details, $id);

    if ($stmt->execute()) {
        echo "<script>alert('Record updated successfully!'); window.location.href = 'medical_list.php';</script>";
    } else {
        echo "<script>alert('Failed to update record.');</script>";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit Medical Record</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-4">
    <h3 class="text-center text-primary">Edit Medical Record</h3>

    <form method="POST" enctype="multipart/form-data">
        <div class="mb-3">
            <label for="applicant_id" class="form-label">Applicant ID</label>
            <input type="text" name="applicant_id" class="form-control" value="<?= htmlspecialchars($record['applicant_id']) ?>" required>
        </div>
        <div class="mb-3">
            <label for="medical_date" class="form-label">Medical Date</label>
            <input type="date" name="medical_date" class="form-control" value="<?= htmlspecialchars($record['medical_date']) ?>" required>
        </div>
        <div class="mb-3">
            <label for="medical_exp_date" class="form-label">Medical Expiry Date</label>
            <input type="date" name="medical_exp_date" class="form-control" value="<?= htmlspecialchars($record['medical_exp_date']) ?>" required>
        </div>
        <div class="mb-3">
            <label for="medical_upload" class="form-label">Medical Document</label>
            <input type="file" name="medical_upload" class="form-control">
            <small>Current file: <?= htmlspecialchars($record['medical_upload']) ?></small>
        </div>
        <div class="mb-3">
            <label for="sleep" class="form-label">Sleep Details</label>
            <textarea name="sleep" class="form-control" rows="4"><?= htmlspecialchars($record['sleep']) ?></textarea>
        </div>
        <button type="submit" class="btn btn-success">Update Record</button>
    </form>
</div>
</body>
</html>
