<?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'];

// Get patient basic information
$patient = $conn->query("
    SELECT first_name, last_name, email, phone, date_of_birth, gender, address 
    FROM patients 
    WHERE patient_id = '$user_id'
")->fetch_assoc();

// Get medical statistics
$consultations = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE patient_id = '$user_id'")->fetch_assoc()['cnt'] ?? 0;
$completed_consults = $conn->query("SELECT COUNT(*) as cnt FROM consultations WHERE patient_id = '$user_id' AND status = 'completed'")->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;

// Get recent consultations
$recent_consults = $conn->query("
    SELECT c.*, d.name as doctor_name 
    FROM consultations c
    LEFT JOIN doctors d ON c.doctor_id = d.doctor_id
    WHERE c.patient_id = '$user_id'
    ORDER BY c.scheduled_time DESC LIMIT 5
");

$page_title = 'Medical Records | 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>Medical Records - 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; }
        
        .record-card {
            background: white;
            border-radius: 24px;
            padding: 25px;
            margin-bottom: 25px;
            border-left: 4px solid #d4a017;
        }
        
        .stat-box {
            background: #f8f9fa;
            border-radius: 16px;
            padding: 15px;
            text-align: center;
        }
        
        .stat-number {
            font-size: 1.8rem;
            font-weight: 800;
            color: #d4a017;
        }
        
        .info-row {
            display: flex;
            align-items: center;
            padding: 10px 0;
            border-bottom: 1px solid #e0e0e0;
        }
        
        .info-label {
            width: 140px;
            font-weight: 600;
            color: #0a3d2f;
        }
        
        .section-title {
            font-size: 1.2rem;
            font-weight: 700;
            color: #0a3d2f;
            margin-bottom: 15px;
            padding-bottom: 8px;
            border-bottom: 2px solid #d4a017;
        }
        
        .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);
        }
        
        .btn-print {
            background: #d4a017;
            color: #0a3d2f;
            border: none;
            border-radius: 40px;
            padding: 10px 25px;
            font-weight: 600;
        }
        
        .btn-print: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;
        }
        
        @media (max-width: 768px) {
            .record-card { padding: 15px; }
            .info-label { width: 120px; font-size: 0.85rem; }
            .stat-number { font-size: 1.3rem; }
        }
    </style>
</head>
<body>

<div class="container mt-4">
    <!-- Header -->
    <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-notes-medical me-2" style="color: #d4a017;"></i> Medical Records</h2>
            <p class="mb-0 text-white-50">Your complete health history and medical information</p>
        </div>
        <div class="mt-2 mt-md-0">
            <button class="btn-print me-2" onclick="window.print()"><i class="fas fa-print me-2"></i> Print Report</button>
            <a href="index.php" class="back-btn"><i class="fas fa-arrow-left me-2"></i> Back</a>
        </div>
    </div>

    <!-- Patient Information -->
    <div class="record-card">
        <h5 class="section-title"><i class="fas fa-user-circle me-2"></i> Personal Information</h5>
        <div class="row">
            <div class="col-md-6">
                <div class="info-row"><div class="info-label">Full Name:</div><div><?= htmlspecialchars($patient['first_name'] ?? '') . ' ' . htmlspecialchars($patient['last_name'] ?? '') ?></div></div>
                <div class="info-row"><div class="info-label">Date of Birth:</div><div><?= htmlspecialchars($patient['date_of_birth'] ?? 'Not specified') ?></div></div>
                <div class="info-row"><div class="info-label">Gender:</div><div><?= htmlspecialchars($patient['gender'] ?? 'Not specified') ?></div></div>
            </div>
            <div class="col-md-6">
                <div class="info-row"><div class="info-label">Email:</div><div><?= htmlspecialchars($patient['email'] ?? 'Not specified') ?></div></div>
                <div class="info-row"><div class="info-label">Phone:</div><div><?= htmlspecialchars($patient['phone'] ?? 'Not specified') ?></div></div>
                <div class="info-row"><div class="info-label">Address:</div><div><?= htmlspecialchars($patient['address'] ?? 'Not specified') ?></div></div>
            </div>
        </div>
    </div>

    <!-- Medical Summary Stats -->
    <div class="record-card">
        <h5 class="section-title"><i class="fas fa-chart-line me-2"></i> Medical Summary</h5>
        <div class="row g-3">
            <div class="col-md-3 col-6">
                <div class="stat-box">
                    <i class="fas fa-video fa-2x mb-2" style="color: #d4a017;"></i>
                    <div class="stat-number"><?= $consultations ?></div>
                    <small>Total Consultations</small>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-box">
                    <i class="fas fa-check-circle fa-2x mb-2" style="color: #28a745;"></i>
                    <div class="stat-number"><?= $completed_consults ?></div>
                    <small>Completed</small>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-box">
                    <i class="fas fa-prescription-bottle fa-2x mb-2" style="color: #d4a017;"></i>
                    <div class="stat-number"><?= $prescriptions ?></div>
                    <small>Prescriptions</small>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-box">
                    <i class="fas fa-flask fa-2x mb-2" style="color: #17a2b8;"></i>
                    <div class="stat-number"><?= $lab_results ?></div>
                    <small>Lab Results</small>
                </div>
            </div>
        </div>
    </div>

    <!-- Recent Consultations -->
    <div class="record-card">
        <h5 class="section-title"><i class="fas fa-history me-2"></i> Recent Consultations</h5>
        <?php if ($recent_consults && $recent_consults->num_rows > 0): ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead class="table-light">
                        <tr><th>Date</th><th>Doctor</th><th>Reason</th><th>Status</th><th>Action</th></tr>
                    </thead>
                    <tbody>
                        <?php while($c = $recent_consults->fetch_assoc()): ?>
                            <tr>
                                <td><?= date('M d, Y', strtotime($c['scheduled_time'])) ?></td>
                                <td>Dr. <?= htmlspecialchars($c['doctor_name'] ?? 'Unknown') ?></td>
                                <td><?= htmlspecialchars(substr($c['reason'] ?? '', 0, 40)) ?>...</td>
                                <td><span class="badge bg-<?= $c['status'] == 'completed' ? 'success' : 'warning' ?>"><?= ucfirst($c['status']) ?></span></td>
                                <td><a href="consultation_detail.php?id=<?= $c['id'] ?>" class="btn btn-sm btn-outline-primary">View</a></td>
                            </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
        <?php else: ?>
            <p class="text-muted text-center py-3">No consultations recorded yet.</p>
        <?php endif; ?>
    </div>

    <!-- Health Tips -->
    <div class="record-card">
        <h5 class="section-title"><i class="fas fa-lightbulb me-2"></i> Health Tips</h5>
        <div class="row">
            <div class="col-md-6">
                <ul>
                    <li>Regular check-ups help detect issues early</li>
                    <li>Keep your medical records up to date</li>
                    <li>Share your complete history with doctors</li>
                </ul>
            </div>
            <div class="col-md-6">
                <ul>
                    <li>Track your vitals regularly</li>
                    <li>Follow prescribed medication schedules</li>
                    <li>Maintain a healthy lifestyle</li>
                </ul>
            </div>
        </div>
    </div>
</div>

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