<?php
session_start();
require_once '../config/database.php';

if (!isset($_SESSION['user_id'])) {
    header('Location: ../login.php');
    exit();
}

$db = new Database();
$conn = $db->getConnection();

$report_date = $_GET['date'] ?? date('Y-m-d');

// جلب البيانات
$stmt = $conn->prepare("
    SELECT sal.*, a.antibiotic_name
    FROM surgery_antibiotics_log sal
    LEFT JOIN antibiotics a ON sal.antibiotic_id = a.id
    WHERE sal.report_date = ?
    ORDER BY sal.serial_number ASC
");
$stmt->execute([$report_date]);
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);

// إعداد headers للتصدير
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename="Medication_Dispensed_per_Operation_' . $report_date . '.xls"');
header('Pragma: no-cache');
header('Expires: 0');

echo "\xEF\xBB\xBF";
?>
<!DOCTYPE html>
<html dir="rtl">
<head>
    <meta charset="UTF-8">
    <style>
        body {
            font-family: 'Arial', 'Tahoma', sans-serif;
            direction: rtl;
            margin: 0;
            padding: 0;
        }
        table {
            border-collapse: collapse;
            width: 100%;
        }
        .data-table th {
            background-color: #4472C4;
            color: #ffffff;
            font-weight: bold;
            padding: 8px;
            border: 1px solid #2e5c8a;
            text-align: center;
        }
        .data-table td {
            border: 1px solid #adb5bd;
            padding: 8px;
            text-align: center;
        }
        .data-table tr:nth-child(odd) td {
            background-color: #ffffff;
        }
        .data-table tr:nth-child(even) td {
            background-color: #f2f7ff;
        }
        .matched td {
            background-color: #d4edda !important;
        }
    </style>
</head>
<body>
    <!-- Header Table -->
    <table>
        <tr><td colspan="10" style="background-color:#1a3a5c; color:#ffffff; font-size:16pt; font-weight:bold; text-align:center; padding:12px;">Medication Dispensed per Operation</td></tr>
        <tr><td colspan="10" style="background-color:#2e5c8a; color:#ffffff; font-size:10pt; text-align:center; padding:5px;">التاريخ: <?php echo $report_date; ?></td></tr>
        <tr><td colspan="10" style="height:8px; border:none;"></td></tr>
    </table>

    <!-- Data Table -->
    <table class="data-table">
        <thead>
            <tr>
                <th>مسلسل</th>
                <th>اسم المريض</th>
                <th>الرقم الطبي</th>
                <th>نوع العملية</th>
                <th>اسم الاستشاري</th>
                <th>اسم الدواء</th>
                <th>الكود</th>
                <th>العدد</th>
                <th>الإدخال</th>
                <th>المطابقة</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($records as $record): ?>
            <tr<?php echo $record['is_matched'] ? ' class="matched"' : ''; ?>>
                <td><?php echo $record['serial_number']; ?></td>
                <td><?php echo htmlspecialchars($record['patient_name']); ?></td>
                <td><?php echo htmlspecialchars($record['medical_number']); ?></td>
                <td><?php echo htmlspecialchars($record['surgery_type']); ?></td>
                <td><?php echo htmlspecialchars($record['consultant_name']); ?></td>
                <td><?php echo htmlspecialchars($record['antibiotic_name'] ?? '-'); ?></td>
                <td><?php echo htmlspecialchars($record['antibiotic_code']); ?></td>
                <td><?php echo htmlspecialchars($record['quantity']); ?></td>
                <td><?php echo date('H:i', strtotime($record['entry_time'])); ?></td>
                <td><?php echo ($record['is_matched'] ? '✓' : '✗'); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Footer Table -->
    <table>
        <tr><td colspan="3" style="background-color:#f2f2f2; text-align:center; padding:10px; font-size:9pt; color:#555; border:1px solid #ccc;">إجمالي السجلات: <?php echo count($records); ?></td></tr>
    </table>
</body>
</html>
