<?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'];
$user_name = $_SESSION['patient_name'];
$home_hospital = null;

// Get the primary hospital for this patient using actual column names
$result = $conn->query("
    SELECT phl.*, hp.hospital_name, hp.address, hp.phone, hp.email
    FROM patient_hospital_links phl
    LEFT JOIN partner_hospitals hp ON phl.hospital_id = hp.hospital_id
    WHERE phl.patient_id = '$user_id' AND phl.consent_given = 1
    LIMIT 1
");

if ($result && $result->num_rows > 0) {
    $home_hospital = $result->fetch_assoc();
}

$page_title = 'Home Hospital | 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>Home 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">
    <style>
        body { background: linear-gradient(135deg, #0a3d2f, #064e3b); font-family: 'Segoe UI', sans-serif; }
        
        .hospital-card {
            background: white;
            border-radius: 24px;
            padding: 30px;
            margin-bottom: 25px;
            border-left: 4px solid #d4a017;
        }
        
        .info-row {
            display: flex;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .info-icon {
            width: 40px;
            color: #d4a017;
        }
        
        .badge-primary {
            background: #d4a017;
            color: #0a3d2f;
            padding: 5px 15px;
            border-radius: 30px;
            font-size: 0.75rem;
            font-weight: 600;
            display: inline-block;
        }
        
        .btn-link-hospital {
            background: #d4a017;
            color: #0a3d2f;
            border: none;
            border-radius: 40px;
            padding: 10px 25px;
            font-weight: 600;
            text-decoration: none;
            display: inline-block;
        }
        
        .btn-link-hospital:hover {
            background: #b8860b;
            color: white;
        }
        
        .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;
            text-decoration: none;
        }
        
        .back-btn:hover {
            background: #d4a017;
            color: #0a3d2f;
        }
        
        .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);
        }
        
        @media (max-width: 768px) {
            .hospital-card { padding: 20px; }
        }
    </style>
</head>
<body>

<div class="container mt-4">
    <!-- Header Section -->
    <div class="header-section d-flex justify-content-between align-items-center flex-wrap">
        <div>
            <h2 class="fw-bold text-white mb-2"><i class="fas fa-home me-2" style="color: #d4a017;"></i> My Home Hospital</h2>
            <p class="mb-0 text-white-50">Your primary healthcare facility where your medical records are hosted.</p>
        </div>
        <a href="index.php" class="back-btn mt-2 mt-md-0"><i class="fas fa-arrow-left me-2"></i> Back to Medical Hub</a>
    </div>

    <?php if ($home_hospital): ?>
        <!-- Hospital Details -->
        <div class="hospital-card">
            <div class="d-flex justify-content-between align-items-start mb-3 flex-wrap">
                <h3 class="mb-2"><?= htmlspecialchars($home_hospital['hospital_name'] ?? 'Your Hospital') ?></h3>
                <?php if($home_hospital['consent_given'] == 1): ?>
                    <span class="badge-primary"><i class="fas fa-check-circle me-1"></i> Linked & Active</span>
                <?php endif; ?>
            </div>
            
            <?php if (!empty($home_hospital['address'])): ?>
                <div class="info-row">
                    <div class="info-icon"><i class="fas fa-map-marker-alt fa-lg"></i></div>
                    <div><strong>Address</strong><br><?= htmlspecialchars($home_hospital['address']) ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (!empty($home_hospital['phone'])): ?>
                <div class="info-row">
                    <div class="info-icon"><i class="fas fa-phone fa-lg"></i></div>
                    <div><strong>Phone</strong><br><?= htmlspecialchars($home_hospital['phone']) ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (!empty($home_hospital['email'])): ?>
                <div class="info-row">
                    <div class="info-icon"><i class="fas fa-envelope fa-lg"></i></div>
                    <div><strong>Email</strong><br><?= htmlspecialchars($home_hospital['email']) ?></div>
                </div>
            <?php endif; ?>
            
            <?php if (!empty($home_hospital['hospital_patient_id'])): ?>
                <div class="info-row">
                    <div class="info-icon"><i class="fas fa-id-card fa-lg"></i></div>
                    <div><strong>Hospital Patient ID</strong><br><?= htmlspecialchars($home_hospital['hospital_patient_id']) ?></div>
                </div>
            <?php endif; ?>
            
            <div class="info-row">
                <div class="info-icon"><i class="fas fa-calendar-alt fa-lg"></i></div>
                <div><strong>Linked Since</strong><br><?= date('F j, Y', strtotime($home_hospital['linked_at'])) ?></div>
            </div>
            
            <?php if ($home_hospital['last_sync']): ?>
                <div class="info-row">
                    <div class="info-icon"><i class="fas fa-sync-alt fa-lg"></i></div>
                    <div><strong>Last Sync</strong><br><?= date('F j, Y', strtotime($home_hospital['last_sync'])) ?></div>
                </div>
            <?php endif; ?>
            
            <div class="mt-4 p-3 bg-light rounded-3">
                <i class="fas fa-info-circle me-2" style="color: #d4a017;"></i>
                <strong>About Your Home Hospital</strong>
                <p class="mb-0 mt-2 small">Your home hospital securely stores your medical records and coordinates your care across specialists. You have given consent for data sharing.</p>
            </div>
            
            <div class="mt-4 text-center">
                <button class="btn-link-hospital" onclick="alert('Request to change home hospital feature coming soon')">
                    <i class="fas fa-exchange-alt me-2"></i> Request Change
                </button>
            </div>
        </div>
        
        <!-- Records Summary -->
        <div class="hospital-card">
            <h5 class="mb-3"><i class="fas fa-chart-line me-2" style="color: #d4a017;"></i> Your Medical Records Summary</h5>
            <div class="row g-3">
                <?php
                $consultations = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE patient_id = '$user_id'")->fetch_assoc()['cnt'] ?? 0;
                $prescriptions = $conn->query("SELECT COUNT(*) as cnt FROM prescriptions WHERE patient_id = '$user_id'")->fetch_assoc()['cnt'] ?? 0;
                $lab_results = $conn->query("SELECT COUNT(*) as cnt FROM lab_results WHERE patient_id = '$user_id'")->fetch_assoc()['cnt'] ?? 0;
                $vitals = $conn->query("SELECT COUNT(*) as cnt FROM vital_signs WHERE patient_id = '$user_id'")->fetch_assoc()['cnt'] ?? 0;
                ?>
                <div class="col-6">
                    <div class="text-center p-3 bg-light rounded-3">
                        <h3 class="mb-0 text-success"><?= $consultations ?></h3>
                        <small>Consultations</small>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-center p-3 bg-light rounded-3">
                        <h3 class="mb-0 text-success"><?= $prescriptions ?></h3>
                        <small>Prescriptions</small>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-center p-3 bg-light rounded-3">
                        <h3 class="mb-0 text-success"><?= $lab_results ?></h3>
                        <small>Lab Results</small>
                    </div>
                </div>
                <div class="col-6">
                    <div class="text-center p-3 bg-light rounded-3">
                        <h3 class="mb-0 text-success"><?= $vitals ?></h3>
                        <small>Vital Signs</small>
                    </div>
                </div>
            </div>
        </div>
        
    <?php else: ?>
        <!-- Empty State -->
        <div class="hospital-card text-center">
            <i class="fas fa-building fa-4x mb-3" style="color: #d4a017;"></i>
            <h3>No Home Hospital Assigned</h3>
            <p class="text-muted mb-4">You haven't linked any hospital as your primary health record holder.</p>
            <div class="alert alert-info">
                <i class="fas fa-info-circle me-2"></i>
                <strong>Why link a home hospital?</strong><br>
                Your home hospital securely stores your medical records and coordinates your care across specialists.
            </div>
            <a href="list_hospital.php" class="btn-link-hospital mt-2">
                <i class="fas fa-search me-2"></i> Find Hospitals to Link
            </a>
        </div>
    <?php endif; ?>
</div>

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