<?php
session_start();
if (!isset($_SESSION['patient_id'])) {
    header("Location: ../login.php");
    exit();
}
include '../../include/config.php'; // ✅ fixed path

$user_id = $_SESSION['patient_id'];
$message = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $hospital_code = trim($_POST['hospital_code']);
    // Simple demo – you can expand later
    $check = $conn->query("SELECT id, hospital_name FROM hospitals WHERE hospital_code = '$hospital_code'");
    if ($check && $check->num_rows > 0) {
        $hospital = $check->fetch_assoc();
        // Insert link request (you can create a table later)
        $message = "Link request sent to {$hospital['hospital_name']}. Your records will be shared after approval.";
    } else {
        $message = "Invalid hospital code. Please check and try again.";
    }
}

$page_title = 'Link Hospital | UHD';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Link Hospital – UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
</head>
<body>
<div class="container mt-5">
    <div class="card shadow rounded-4">
        <div class="card-header bg-primary text-white">
            <h4><i class="fas fa-link me-2"></i>Link Your Hospital Records</h4>
        </div>
        <div class="card-body">
            <?php if ($message): ?>
                <div class="alert alert-info"><?= htmlspecialchars($message) ?></div>
            <?php endif; ?>
            <p>Enter the unique hospital code provided by your hospital’s UHD Lite administrator.</p>
            <form method="POST">
                <div class="mb-3">
                    <label class="form-label">Hospital Code</label>
                    <input type="text" name="hospital_code" class="form-control" placeholder="e.g., UHD-LAG-001" required>
                </div>
                <button type="submit" class="btn btn-success">Send Link Request</button>
                <a href="index.php" class="btn btn-secondary">Back to Medical Hub</a>
            </form>
        </div>
    </div>
</div>
<?php include '../../include/footer.php'; ?>