<?php
require_once __DIR__ . '/db/config.php';
require_once __DIR__ . '/helpers.php';

require_once __DIR__ . '/includes/auth.php';
check_auth();

$db = db();
$lang = $_SESSION['lang'] ?? 'en';
$id = $_GET['id'] ?? 0;

if (!$id) {
    die("Invalid ID");
}

// Fetch Inquiry
$stmt = $db->prepare("
    SELECT xi.*, p.name as patient_name, p.gender, p.dob, p.phone, d.name_$lang as doctor_name
    FROM xray_inquiries xi
    LEFT JOIN patients p ON xi.patient_id = p.id
    LEFT JOIN visits v ON xi.visit_id = v.id
    LEFT JOIN employees d ON v.doctor_id = d.id
    WHERE xi.id = ?
");
$stmt->execute([$id]);
$inquiry = $stmt->fetch();

if (!$inquiry) {
    die("Inquiry not found");
}

// Fetch Items
$stmt = $db->prepare("
    SELECT it.*, t.name_$lang as xray_name
    FROM xray_inquiry_items it 
    JOIN xray_tests t ON it.xray_id = t.id 
    WHERE it.inquiry_id = ?
");
$stmt->execute([$id]);
$items = $stmt->fetchAll();

?>
<!DOCTYPE html>
<html lang="<?php echo $lang; ?>" dir="<?php echo $lang == 'ar' ? 'rtl' : 'ltr'; ?>">
<head>
    <meta charset="UTF-8">
    <title>X-Ray Inquiry #<?php echo $id; ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background: #fff; color: #000; }
        .header { border-bottom: 2px solid #000; padding-bottom: 20px; margin-bottom: 30px; }
        .logo { max-height: 80px; }
        .patient-info { background: #f8f9fa; padding: 15px; border-radius: 8px; margin-bottom: 30px; border: 1px solid #ddd; }
        .table th { background-color: #f1f1f1 !important; }
        @media print {
            .no-print { display: none !important; }
            .patient-info { background-color: #f8f9fa !important; -webkit-print-color-adjust: exact; }
            .table th { background-color: #f1f1f1 !important; -webkit-print-color-adjust: exact; }
        }
    </style>
</head>
<body class="p-4">

    <div class="container">
        <!-- Header -->
        <div class="header d-flex justify-content-between align-items-center">
            <div>
                <h2 class="fw-bold mb-0">X-Ray Report</h2>
                <div class="text-muted small">Inquiry #<?php echo $id; ?></div>
            </div>
            <div class="text-end">
                <div class="fw-bold"><?php echo date('F j, Y, g:i a', strtotime($inquiry['inquiry_date'])); ?></div>
                <div class="badge bg-secondary text-white p-2 mt-1"><?php echo htmlspecialchars($inquiry['status']); ?></div>
            </div>
        </div>

        <!-- Patient Info -->
        <div class="patient-info">
            <div class="row">
                <div class="col-md-6 mb-2">
                    <span class="text-muted me-2">Patient:</span>
                    <strong class="fs-5"><?php echo htmlspecialchars($inquiry['patient_name']); ?></strong>
                </div>
                <div class="col-md-6 mb-2">
                    <span class="text-muted me-2">Gender/Age:</span>
                    <strong>
                        <?php echo $inquiry['gender'] ?? '-'; ?> 
                        <?php if(!empty($inquiry['dob'])) echo ' / ' . date_diff(date_create($inquiry['dob']), date_create('today'))->y . ' Years'; ?>
                    </strong>
                </div>
                <div class="col-md-6">
                    <span class="text-muted me-2">Doctor:</span>
                    <strong><?php echo htmlspecialchars($inquiry['doctor_name'] ?? 'Walk-in'); ?></strong>
                </div>
                <div class="col-md-6">
                    <span class="text-muted me-2">Source:</span>
                    <strong><?php echo htmlspecialchars($inquiry['source']); ?></strong>
                </div>
            </div>
        </div>

        <!-- Results Table -->
        <h5 class="fw-bold border-bottom pb-2 mb-3">Examination Results</h5>
        <table class="table table-bordered mb-4">
            <thead>
                <tr>
                    <th style="width: 40%;">X-Ray Examination</th>
                    <th>Result / Findings</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td class="fw-bold"><?php echo htmlspecialchars($item['xray_name']); ?></td>
                    <td>
                        <?php echo nl2br(htmlspecialchars($item['result'] ?: 'Pending Result')); ?>
                        <?php if ($item['attachment']): ?>
                            <div class="mt-2 no-print">
                                <small class="text-muted"><i class="bi bi-paperclip"></i> Attachment available online</small>
                            </div>
                        <?php endif; ?>
                    </td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Notes -->
        <?php if (!empty($inquiry['notes'])): ?>
        <div class="mb-4">
            <h5 class="fw-bold border-bottom pb-2 mb-2">Notes</h5>
            <div class="p-3 bg-light border rounded">
                <?php echo $inquiry['notes']; // Summernote content is HTML ?>
            </div>
        </div>
        <?php endif; ?>

        <!-- Footer -->
        <div class="mt-5 pt-4 border-top text-center text-muted small">
            <p>Generated by Clinic Management System on <?php echo date('Y-m-d H:i:s'); ?></p>
        </div>

        <div class="text-center mt-4 no-print">
            <button onclick="window.print()" class="btn btn-primary px-4"><i class="bi bi-printer"></i> Print Report</button>
            <button onclick="window.close()" class="btn btn-secondary px-4">Close</button>
        </div>
    </div>

</body>
</html>