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

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('HTTP/1.1 403 Forbidden');
    exit('غير مصرح لك بالوصول');
}

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

// Log export activity
logActivity($conn, $_SESSION['user_id'], 'تصدير المضادات الحيوية Excel', 'antibiotics');

// Get data
$stmt = $conn->query("
    SELECT 
        a.*,
        COUNT(ar.id) as usage_count
    FROM antibiotics a
    LEFT JOIN antibiotic_reports ar ON a.id = ar.antibiotic_id
    GROUP BY a.id
    ORDER BY a.antibiotic_name
");
$data = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Create Excel file with HTML table
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header('Content-Disposition: attachment; filename="antibiotics_' . date('Y-m-d_H-i-s') . '.xls"');
header('Cache-Control: max-age=0');
header('Pragma: public');

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%;
            direction: rtl;
            font-family: 'Arial', 'Tahoma', sans-serif;
        }
        .data-table td {
            padding: 8px 10px;
            text-align: center;
            border: 1px solid #adb5bd;
            font-size: 10pt;
            vertical-align: middle;
        }
        .data-table tr:nth-child(odd) td {
            background-color: #ffffff;
        }
        .data-table tr:nth-child(even) td {
            background-color: #f2f7ff;
        }
        .status-active {
            background-color: #d1e7dd;
            color: #0f5132;
            font-weight: bold;
            padding: 4px 8px;
        }
        .status-inactive {
            background-color: #f8d7da;
            color: #842029;
            font-weight: bold;
            padding: 4px 8px;
        }
    </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;">قائمة المضادات الحيوية</td></tr>
        <tr><td colspan="10" style="background-color:#2e5c8a; color:#ffffff; font-size:10pt; text-align:center; padding:5px;">مستشفى مدينة نصر التخصصي - Nasr City Specialized Hospital | تاريخ التصدير: <?php echo date('Y-m-d h:i A'); ?></td></tr>
        <tr><td colspan="10" style="height:8px; border:none;"></td></tr>
    </table>

    <!-- Data Table -->
    <table class="data-table">
        <thead>
            <tr>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:80px;">الرقم</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:180px;">اسم المضاد الحيوي</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:180px;">الاسم العلمي</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:140px;">الفئة</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:140px;">الشكل الصيدلاني</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:120px;">التركيز</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:250px;">دواعي الاستعمال</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:120px;">عدد الاستخدامات</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:110px;">الحالة</th>
                <th style="background-color:#4472C4; color:#ffffff; font-weight:bold; padding:10px; border:1px solid #2e5c8a; width:160px;">تاريخ الإضافة</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($data as $row): ?>
            <tr>
                <td><?php echo htmlspecialchars($row['id']); ?></td>
                <td><?php echo htmlspecialchars($row['antibiotic_name']); ?></td>
                <td><?php echo htmlspecialchars($row['generic_name']); ?></td>
                <td><?php echo htmlspecialchars($row['category']); ?></td>
                <td><?php echo htmlspecialchars($row['dosage_form']); ?></td>
                <td><?php echo htmlspecialchars($row['strength']); ?></td>
                <td style="text-align:right;"><?php echo htmlspecialchars($row['indication']); ?></td>
                <td><?php echo htmlspecialchars($row['usage_count']); ?></td>
                <td class="<?php echo $row['is_active'] ? 'status-active' : 'status-inactive'; ?>">
                    <?php echo $row['is_active'] ? 'نشط' : 'معطل'; ?>
                </td>
                <td><?php echo htmlspecialchars($row['created_at']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>

    <!-- Footer Table -->
    <table>
        <tr><td colspan="10" style="background-color:#f2f2f2; text-align:center; padding:10px; font-size:9pt; color:#555; border:1px solid #ccc;">إجمالي المضادات الحيوية: <?php echo count($data); ?> | نظام الصيدلة الإكلينيكية المتكامل (ICS) - مستشفى مدينة نصر التخصصي | تم التصدير بواسطة: <?php echo htmlspecialchars($_SESSION['full_name']); ?> | التاريخ: <?php echo date('Y-m-d h:i A'); ?></td></tr>
    </table>
</body>
</html>
