<?php
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require 'db.php';
require 'helpers.php';
require_student();

$student_id = $_SESSION['student_id'];

// Get student data
$stmt = $mysqli1->prepare("
    SELECT s.*, c.name AS college_name, p.name AS program_name 
    FROM students s 
    JOIN colleges c ON s.college_id = c.id 
    JOIN programs p ON s.program_id = p.id 
    WHERE s.student_id = ?
");
$stmt->bind_param("s", $student_id);
$stmt->execute();
$student_data = $stmt->get_result()->fetch_assoc();

// Get medical record
$medical_record = $mysqli1->query("
    SELECT * FROM student_medical_records 
    WHERE student_id = '$student_id' 
    ORDER BY created_at DESC 
    LIMIT 1
")->fetch_assoc();

if (!$medical_record) {
    $_SESSION['error_message'] = 'لا توجد سجلات طبية';
    header('Location: student_profile.php');
    exit;
}

include 'header.php';
?>

<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>السجل الطبي</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Arial', sans-serif;
        }
        .medical-record-container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0,0,0,0.1);
            overflow: hidden;
            margin: 30px auto;
            max-width: 900px;
        }
        .record-header {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        .record-header h1 {
            margin: 0;
            font-size: 2rem;
            font-weight: bold;
        }
        .medical-info {
            padding: 30px;
        }
        .info-card {
            background: #f8fafc;
            border: 2px solid #e2e8f0;
            border-radius: 15px;
            padding: 25px;
            margin-bottom: 20px;
        }
        .info-card h5 {
            color: #1e293b;
            margin-bottom: 20px;
            border-bottom: 2px solid #e2e8f0;
            padding-bottom: 10px;
        }
        .info-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid #f1f5f9;
        }
        .info-item:last-child {
            border-bottom: none;
        }
        .info-label {
            font-weight: 600;
            color: #475569;
        }
        .info-value {
            color: #1e293b;
        }
        .badge-yes {
            background: #dc2626;
            color: white;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.9rem;
        }
        .badge-no {
            background: #059669;
            color: white;
            padding: 6px 12px;
            border-radius: 20px;
            font-size: 0.9rem;
        }
        .student-info {
            background: #e0f2fe;
            border-radius: 15px;
            padding: 20px;
            margin: 20px;
            border-left: 5px solid #0284c7;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="medical-record-container">
            <!-- Header -->
            <div class="record-header">
                <h1><i class="fas fa-file-medical"></i> السجل الطبي</h1>
                <p>بياناتك الطبية المسجلة في النظام</p>
            </div>

            <!-- Student Information -->
            <div class="student-info">
                <div class="row">
                    <div class="col-md-6">
                        <strong>الاسم:</strong> <?= e($student_data['student_name']) ?>
                    </div>
                    <div class="col-md-6">
                        <strong>الرقم الجامعي:</strong> <?= e($student_data['student_id']) ?>
                    </div>
                    <div class="col-md-6">
                        <strong>الكليّة:</strong> <?= e($student_data['college_name']) ?>
                    </div>
                    <div class="col-md-6">
                        <strong>البرنامج:</strong> <?= e($student_data['program_name']) ?>
                    </div>
                </div>
            </div>

            <!-- Medical Information -->
            <div class="medical-info">
                <!-- Basic Medical Info -->
                <div class="info-card">
                    <h5><i class="fas fa-info-circle"></i> المعلومات الأساسية</h5>
                    <div class="info-item">
                        <span class="info-label">تاريخ آخر فحص طبي:</span>
                        <span class="info-value"><?= e($medical_record['medical_date']) ?></span>
                    </div>
                </div>

                <!-- Medical Conditions -->
                <div class="info-card">
                    <h5><i class="fas fa-stethoscope"></i> الحالات الطبية</h5>
                    <?php
                    $conditions = [
                        'إعاقة حركية' => $medical_record['has_disability'],
                        'أمراض مزمنة' => $medical_record['has_chronic_diseases'],
                        'أمراض نفسية' => $medical_record['has_psychological_diseases'],
                        'مشاكل في الرؤية' => $medical_record['has_vision_problems'],
                        'مشاكل في السمع' => $medical_record['has_hearing_problems'],
                        'تطعيم كوفيد-19' => $medical_record['covid_vaccinated'],
                        'سكري' => $medical_record['has_diabetes'],
                        'ضغط دم' => $medical_record['has_blood_pressure'],
                        'أمراض وراثية في العائلة' => $medical_record['has_family_diseases']
                    ];
                    
                    foreach ($conditions as $label => $value):
                    ?>
                    <div class="info-item">
                        <span class="info-label"><?= $label ?>:</span>
                        <span class="info-value">
                            <span class="badge <?= $value === 'yes' ? 'badge-yes' : 'badge-no' ?>">
                                <?= $value === 'yes' ? 'نعم' : 'لا' ?>
                            </span>
                        </span>
                    </div>
                    <?php endforeach; ?>
                </div>

                <!-- Additional Details -->
                <?php if (!empty($medical_record['chronic_diseases_details'])): ?>
                <div class="info-card">
                    <h5><i class="fas fa-notes-medical"></i> تفاصيل الأمراض المزمنة</h5>
                    <p><?= e($medical_record['chronic_diseases_details']) ?></p>
                </div>
                <?php endif; ?>

                <?php if (!empty($medical_record['family_diseases_details'])): ?>
                <div class="info-card">
                    <h5><i class="fas fa-dna"></i> تفاصيل الأمراض الوراثية في العائلة</h5>
                    <p><?= e($medical_record['family_diseases_details']) ?></p>
                </div>
                <?php endif; ?>

                <?php if (!empty($medical_record['additional_notes'])): ?>
                <div class="info-card">
                    <h5><i class="fas fa-clipboard-list"></i> ملاحظات إضافية</h5>
                    <p><?= e($medical_record['additional_notes']) ?></p>
                </div>
                <?php endif; ?>

                <!-- Actions -->
                <div class="text-center mt-4">
                    <a href="student_medical_form.php" class="btn btn-primary me-2">
                        <i class="fas fa-edit"></i> تحديث البيانات
                    </a>
                    <a href="student_profile.php" class="btn btn-secondary">
                        <i class="fas fa-arrow-right"></i> العودة للصفحة الشخصية
                    </a>
                </div>
            </div>
        </div>
    </div>
</body>
</html>