<?php
session_start();
error_reporting(E_ALL);
ini_set('display_errors', 1);

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

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

// CORRECTED: Using hospital_id (not id) from your partner_hospitals table
$result = $conn->query("
    SELECT hospital_id, hospital_name, address, phone, email, status 
    FROM partner_hospitals 
    WHERE status = 'active'
    ORDER BY hospital_name ASC
");

if ($result && $result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $hospitals[] = $row;
    }
}

$page_title = 'Partner Hospitals | UHD';
include '../../include/header.php';
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Partner Hospitals - 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">
    <style>
        body { background: linear-gradient(135deg, #0a3d2f, #064e3b); font-family: 'Segoe UI', sans-serif; }
        
        .header-section {
            background: rgba(255,255,255,0.08);
            backdrop-filter: blur(10px);
            border-radius: 24px;
            padding: 25px;
            margin-bottom: 30px;
            border: 1px solid rgba(255,255,255,0.1);
        }
        
        .hospital-card {
            background: white;
            border-radius: 20px;
            padding: 20px;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            border-left: 4px solid #d4a017;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        
        .hospital-card:hover {
            transform: translateX(5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.15);
        }
        
        .hospital-name {
            font-size: 1.2rem;
            font-weight: 700;
            color: #0a3d2f;
            margin-bottom: 8px;
        }
        
        .badge-active {
            background: #28a745;
            color: white;
            padding: 4px 12px;
            border-radius: 20px;
            font-size: 0.7rem;
            font-weight: 600;
            display: inline-block;
        }
        
        .btn-link-hospital {
            background: #d4a017;
            color: #0a3d2f;
            border: none;
            border-radius: 30px;
            padding: 6px 18px;
            font-size: 0.75rem;
            font-weight: 600;
            margin-top: 12px;
            transition: all 0.2s;
            width: 100%;
        }
        
        .btn-link-hospital:hover {
            background: #b8860b;
            color: white;
            transform: translateY(-2px);
        }
        
        .info-icon {
            width: 25px;
            color: #d4a017;
        }
        
        .back-btn {
            background: rgba(255,255,255,0.15);
            border: 1px solid rgba(255,255,255,0.2);
            color: white;
            border-radius: 40px;
            padding: 8px 20px;
            transition: 0.2s;
            text-decoration: none;
        }
        
        .back-btn:hover {
            background: #d4a017;
            color: #0a3d2f;
        }
        
        @media (max-width: 768px) {
            .hospital-name { font-size: 1rem; }
            .hospital-card { padding: 15px; }
        }
    </style>
</head>
<body>

<div class="container mt-4">
    <!-- Header Section -->
    <div class="header-section text-white">
        <div class="row align-items-center">
            <div class="col-md-8">
                <h2 class="fw-bold mb-2"><i class="fas fa-hospital me-2" style="color: #d4a017;"></i> UHD Partner Hospitals</h2>
                <p class="mb-0 opacity-75">Connect your medical records with any of our partner hospitals. Seamless access to your health data across facilities.</p>
            </div>
            <div class="col-md-4 text-center mt-3 mt-md-0">
                <a href="index.php" class="back-btn"><i class="fas fa-arrow-left me-2"></i> Back to Medical Hub</a>
            </div>
        </div>
    </div>

    <!-- Hospitals Grid -->
    <?php if (count($hospitals) > 0): ?>
        <div class="row">
            <?php foreach ($hospitals as $h): ?>
                <div class="col-md-6 col-lg-4">
                    <div class="hospital-card" onclick="viewHospitalDetails(<?= $h['hospital_id'] ?>, '<?= addslashes($h['hospital_name']) ?>')">
                        <div class="d-flex justify-content-between align-items-start">
                            <div class="hospital-name"><?= htmlspecialchars($h['hospital_name']) ?></div>
                            <?php if ($h['status'] == 'active'): ?>
                                <span class="badge-active"><i class="fas fa-check-circle me-1"></i> Partner</span>
                            <?php endif; ?>
                        </div>
                        
                        <?php if (!empty($h['address'])): ?>
                            <p class="text-muted small mb-1">
                                <i class="fas fa-map-marker-alt info-icon me-1"></i> <?= htmlspecialchars(substr($h['address'], 0, 80)) ?>
                            </p>
                        <?php endif; ?>
                        
                        <?php if (!empty($h['phone'])): ?>
                            <p class="text-muted small mb-1">
                                <i class="fas fa-phone info-icon me-1"></i> <?= htmlspecialchars($h['phone']) ?>
                            </p>
                        <?php endif; ?>
                        
                        <?php if (!empty($h['email'])): ?>
                            <p class="text-muted small mb-2">
                                <i class="fas fa-envelope info-icon me-1"></i> <?= htmlspecialchars($h['email']) ?>
                            </p>
                        <?php endif; ?>
                        
                        <button class="btn-link-hospital" onclick="event.stopPropagation(); requestLink(<?= $h['hospital_id'] ?>, '<?= addslashes($h['hospital_name']) ?>')">
                            <i class="fas fa-link me-2"></i> Request to Link Records
                        </button>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
        
        <!-- Info Footer -->
        <div class="text-center mt-4">
            <small class="text-white-50">
                <i class="fas fa-shield-alt me-1"></i> Your data is encrypted and secure | 
                <i class="fas fa-clock ms-2 me-1"></i> Link requests approved within 24 hours
            </small>
        </div>
        
    <?php else: ?>
        <!-- Empty State -->
        <div class="alert alert-info text-center py-5" style="background: rgba(255,255,255,0.1); border: none; backdrop-filter: blur(5px);">
            <i class="fas fa-building fa-4x mb-3" style="color: #d4a017;"></i>
            <h4 class="text-white mb-2">No Partner Hospitals Yet</h4>
            <p class="text-white-50 mb-3">Partner hospitals will appear here once they join the UHD network.</p>
            <hr class="bg-white-50" style="width: 50%; margin: 20px auto;">
            <p class="small text-white-50">To add a test hospital, run this SQL in phpMyAdmin:</p>
            <code class="small bg-dark text-white p-2 d-inline-block rounded" style="white-space: pre-wrap; font-size: 0.7rem;">
                INSERT INTO partner_hospitals (hospital_name, address, phone, email, status) VALUES 
                ('UHD General Hospital Lagos', '12 Allen Avenue, Ikeja, Lagos', '+234 123 456 7890', 'info@uhdlagos.com', 'active');
            </code>
        </div>
    <?php endif; ?>
</div>

<script>
function viewHospitalDetails(hospitalId, hospitalName) {
    alert(`Viewing details for ${hospitalName}\n\nMore features coming soon!`);
}

function requestLink(hospitalId, hospitalName) {
    if(confirm(`Request to link your medical records with ${hospitalName}?\n\nYour health data will be shared securely with this hospital.`)) {
        alert(`✓ Link request sent to ${hospitalName}\n\nYou will be notified once approved.`);
    }
}
</script>

<?php include '../../include/footer.php'; ?>