<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Lab Report - <?= esc($order['OrderID'] ?? 'N/A') ?></title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: Arial, sans-serif;
            font-size: 12px;
            line-height: 1.4;
            color: #333;
            background: #fff;
            padding: 20px;
        }
        
        .report-container {
            max-width: 800px;
            margin: 0 auto;
            border: 1px solid #ccc;
            padding: 30px;
        }
        
        .header {
            text-align: center;
            border-bottom: 2px solid #333;
            padding-bottom: 20px;
            margin-bottom: 20px;
        }
        
        .header h1 {
            font-size: 24px;
            margin-bottom: 5px;
        }
        
        .header .subtitle {
            font-size: 14px;
            color: #666;
        }
        
        .patient-section {
            margin-bottom: 20px;
            padding: 15px;
            background: #f9f9f9;
            border: 1px solid #ddd;
        }
        
        .section-title {
            font-weight: bold;
            font-size: 14px;
            margin-bottom: 10px;
            color: #333;
            border-bottom: 1px solid #ccc;
            padding-bottom: 5px;
        }
        
        .info-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            gap: 10px;
        }
        
        .info-item {
            display: flex;
            flex-direction: column;
        }
        
        .info-label {
            font-weight: bold;
            color: #666;
            font-size: 11px;
        }
        
        .info-value {
            color: #333;
        }
        
        .order-section {
            margin-bottom: 20px;
            padding: 15px;
            background: #f9f9f9;
            border: 1px solid #ddd;
        }
        
        .results-section {
            margin-bottom: 20px;
        }
        
        .results-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }
        
        .results-table th,
        .results-table td {
            border: 1px solid #ccc;
            padding: 8px;
            text-align: left;
        }
        
        .results-table th {
            background: #e0e0e0;
            font-weight: bold;
            font-size: 11px;
        }
        
        .results-table tr:nth-child(even) {
            background: #f5f5f5;
        }
        
        .flag {
            display: inline-block;
            padding: 2px 6px;
            border-radius: 3px;
            font-weight: bold;
            font-size: 11px;
        }
        
        .flag-low {
            background: #fff3cd;
            color: #856404;
            border: 1px solid #ffc107;
        }
        
        .flag-high {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .reference-range {
            font-size: 10px;
            color: #666;
        }
        
        .footer {
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #ccc;
            font-size: 10px;
            color: #666;
        }
        
        .signature-section {
            margin-top: 40px;
            display: flex;
            justify-content: flex-end;
        }
        
        .signature-box {
            text-align: center;
            width: 200px;
        }
        
        .signature-line {
            border-bottom: 1px solid #333;
            margin-top: 60px;
            margin-bottom: 5px;
        }
        
        .print-button {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 20px;
            background: #007bff;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
        }
        
        .print-button:hover {
            background: #0056b3;
        }
        
        @media print {
            body {
                padding: 0;
            }
            
            .report-container {
                border: none;
                padding: 0;
            }
            
            .print-button {
                display: none;
            }
            
            .results-table {
                page-break-inside: auto;
            }
            
            .results-table tr {
                page-break-inside: avoid;
            }
        }
    </style>
</head>
<body>
    <?php
    // Helper function to calculate flag dynamically
    function calculateFlag($result) {
        if (empty($result['Result']) || !is_numeric($result['Result'])) {
            return null;
        }
        
        if (empty($result['Low']) || empty($result['High'])) {
            return null;
        }
        
        $value = floatval($result['Result']);
        $low = floatval($result['Low']);
        $high = floatval($result['High']);
        $lowSign = $result['LowSign'] ?? '<=';
        $highSign = $result['HighSign'] ?? '<=';
        
        // Check low
        if ($lowSign === '<=' && $value <= $low) {
            return 'L';
        }
        if ($lowSign === '<' && $value < $low) {
            return 'L';
        }
        
        // Check high
        if ($highSign === '>=' && $value >= $high) {
            return 'H';
        }
        if ($highSign === '>' && $value > $high) {
            return 'H';
        }
        
        return null; // Normal
    }
    ?>
    <button class="print-button" onclick="window.print()">Print Report</button>
    
    <div class="report-container">
        <div class="header">
            <h1>LABORATORY REPORT</h1>
            <div class="subtitle">Clinical Laboratory Quality Management System</div>
        </div>
        
        <div class="patient-section">
            <div class="section-title">Patient Information</div>
            <div class="info-grid">
                <div class="info-item">
                    <span class="info-label">Patient Name</span>
                    <span class="info-value"><?= esc(($patient['NameFirst'] ?? '') . ' ' . ($patient['NameLast'] ?? '')) ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Patient ID</span>
                    <span class="info-value"><?= esc($patient['PatientID'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Date of Birth</span>
                    <span class="info-value"><?= esc($patient['Birthdate'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Sex</span>
                    <span class="info-value"><?= esc($patient['Sex'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Phone</span>
                    <span class="info-value"><?= esc($patient['MobilePhone'] ?? $patient['Phone'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Address</span>
                    <span class="info-value"><?= esc(($patient['Street_1'] ?? '') . ' ' . ($patient['City'] ?? '')) ?></span>
                </div>
            </div>
        </div>
        
        <div class="order-section">
            <div class="section-title">Order Information</div>
            <div class="info-grid">
                <div class="info-item">
                    <span class="info-label">Order ID</span>
                    <span class="info-value"><?= esc($order['OrderID'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Order Date</span>
                    <span class="info-value"><?= esc($order['TrnDate'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Priority</span>
                    <span class="info-value"><?= esc($order['Priority'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Requesting Physician</span>
                    <span class="info-value"><?= esc($order['ReqApp'] ?? 'N/A') ?></span>
                </div>
                <div class="info-item">
                    <span class="info-label">Placer ID</span>
                    <span class="info-value"><?= esc($order['PlacerID'] ?? 'N/A') ?></span>
                </div>
            </div>
        </div>
        
        <div class="results-section">
            <div class="section-title">Test Results</div>
            <table class="results-table">
                <thead>
                    <tr>
                        <th>Test Name</th>
                        <th>Code</th>
                        <th>Result</th>
                        <th>Unit</th>
                        <th>Flag</th>
                        <th>Reference Range</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($results)): ?>
                        <?php foreach ($results as $result): ?>
                            <tr>
                                <td><?= esc($result['TestSiteName'] ?? 'N/A') ?></td>
                                <td><?= esc($result['TestSiteCode'] ?? 'N/A') ?></td>
                                <td><?= esc($result['Result'] ?? 'Pending') ?></td>
                                <td><?= esc($result['Unit1'] ?? '') ?></td>
                                <td>
                                    <?php 
                                    $flag = calculateFlag($result);
                                    if (!empty($flag)): 
                                    ?>
                                        <span class="flag flag-<?= $flag === 'H' ? 'high' : 'low' ?>">
                                            <?= $flag === 'H' ? 'HIGH' : 'LOW' ?>
                                        </span>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                                <td class="reference-range">
                                    <?php if (!empty($result['Low']) && !empty($result['High'])): ?>
                                        <?= esc($result['LowSign'] ?? '') ?> <?= esc($result['Low']) ?> - <?= esc($result['HighSign'] ?? '') ?> <?= esc($result['High']) ?>
                                    <?php else: ?>
                                        -
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr>
                            <td colspan="6" style="text-align: center;">No results found for this order</td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <div class="signature-section">
            <div class="signature-box">
                <div class="signature-line"></div>
                <div>Authorized Signature</div>
                <div style="font-size: 10px; color: #666; margin-top: 5px;">Pathologist / Lab Director</div>
            </div>
        </div>
        
        <div class="footer">
            <p><strong>Report Generated:</strong> <?= esc($generatedAt) ?></p>
            <p><strong>CLQMS</strong> - Clinical Laboratory Quality Management System</p>
            <p>This report is electronically generated and is valid without signature.</p>
        </div>
    </div>
</body>
</html>