<?php
session_start();

// التحقق من تسجيل الدخول
if (!isset($_SESSION['user_id'])) {
    header("Location: index.php");
    exit();
}

require_once 'db_connect.php';

// جلب قائمة العملاء
$clients = [];
try {
    $clients = $conn->query("SELECT id, name FROM clients ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    error_log("Error fetching clients: " . $e->getMessage());
    $clients = [];
}

// تهيئة المتغيرات
$report_data = [];
$selected_client = null;
$date_from = null;
$date_to = null;
$total_purchases = 0;
$total_paid = 0;
$total_remaining = 0;
$success_message = '';
$error_message = '';

// معالجة طلب البحث
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['generate_report'])) {
    $client_id = $_POST['client_id'] ?? 'all';
    $date_from = $_POST['date_from'] ?? '';
    $date_to = $_POST['date_to'] ?? '';

    // التحقق من صحة التواريخ
    if (!empty($date_from) && !empty($date_to)) {
        if (strtotime($date_from) > strtotime($date_to)) {
            $error_message = "تاريخ البداية يجب أن يكون قبل تاريخ النهاية!";
        }
    }

    if (empty($error_message)) {
        try {
            // استعلام الفواتير
            $query = "
                SELECT 
                    si.id AS invoice_id,
                    si.invoice_number,
                    si.date,
                    si.total,
                    si.discount,
                    si.final_total,
                    si.paid_amount,
                    si.remaining_amount,
                    si.payment_type,
                    c.id AS client_id,
                    c.name AS client_name,
                    i.name AS item_name,
                    sii.quantity,
                    sii.price,
                    (sii.quantity * sii.price) AS item_total
                FROM sales_invoices si
                LEFT JOIN clients c ON si.client_id = c.id
                LEFT JOIN sales_invoice_items sii ON si.id = sii.invoice_id
                LEFT JOIN items i ON sii.item_id = i.id
                WHERE 1=1
            ";

            $params = [];
            if ($client_id !== 'all') {
                $query .= " AND si.client_id = :client_id";
                $params[':client_id'] = $client_id;
                $selected_client = $clients[array_search($client_id, array_column($clients, 'id'))]['name'] ?? 'غير معروف';
            }

            if (!empty($date_from)) {
                $query .= " AND si.date >= :date_from";
                $params[':date_from'] = $date_from;
            }

            if (!empty($date_to)) {
                $query .= " AND si.date <= :date_to";
                $params[':date_to'] = $date_to . ' 23:59:59';
            }

            $query .= " ORDER BY si.date ASC";

            $stmt = $conn->prepare($query);
            $stmt->execute($params);
            $invoices = $stmt->fetchAll(PDO::FETCH_ASSOC);

            // استعلام المدفوعات
            $payment_query = "
                SELECT 
                    tt.client_id,
                    tt.amount AS payment_amount,
                    tt.date,
                    c.name AS client_name
                FROM treasury_transactions tt
                LEFT JOIN clients c ON tt.client_id = c.id
                WHERE tt.transaction_type = 'income' AND tt.client_id IS NOT NULL
            ";

            $payment_params = [];
            if ($client_id !== 'all') {
                $payment_query .= " AND tt.client_id = :client_id";
                $payment_params[':client_id'] = $client_id;
            }

            if (!empty($date_from)) {
                $payment_query .= " AND tt.date >= :date_from";
                $payment_params[':date_from'] = $date_from;
            }

            if (!empty($date_to)) {
                $payment_query .= " AND tt.date <= :date_to";
                $payment_params[':date_to'] = $date_to . ' 23:59:59';
            }

            $payment_query .= " ORDER BY tt.date ASC";

            $stmt = $conn->prepare($payment_query);
            $stmt->execute($payment_params);
            $payments = $stmt->fetchAll(PDO::FETCH_ASSOC);

            // دمج الفواتير والمدفوعات مع توزيع المدفوعات
            $report_data = [];
            $total_purchases = 0;
            $total_paid = 0;
            $total_remaining = 0;

            // إضافة الفواتير إلى البيانات
            foreach ($invoices as $invoice) {
                $report_data[] = array_merge($invoice, [
                    'payment_amount' => null,
                    'record_type' => 'invoice',
                    'total_paid' => (float)$invoice['paid_amount'],
                    'adjusted_remaining' => (float)$invoice['remaining_amount']
                ]);
                $total_purchases += (float)$invoice['final_total'];
                $total_paid += (float)$invoice['paid_amount'];
                $total_remaining += (float)$invoice['remaining_amount'];
            }

            // توزيع المدفوعات على الفواتير
            foreach ($payments as $payment) {
                $remaining_payment = (float)$payment['payment_amount'];
                $payment_date = $payment['date'];

                foreach ($invoices as $invoice) {
                    if ($remaining_payment <= 0) {
                        break;
                    }

                    $invoice_remaining = (float)$invoice['remaining_amount'];
                    if ($invoice_remaining > 0) {
                        $payment_to_apply = min($remaining_payment, $invoice_remaining);
                        $remaining_payment -= $payment_to_apply;

                        // إنشاء سجل دفعة بنفس بيانات الفاتورة
                        $report_data[] = [
                            'invoice_id' => $invoice['invoice_id'],
                            'invoice_number' => $invoice['invoice_number'],
                            'date' => $payment_date,
                            'total' => $invoice['total'],
                            'discount' => $invoice['discount'],
                            'final_total' => $invoice['final_total'],
                            'paid_amount' => null,
                            'remaining_amount' => max(0, $invoice_remaining - $payment_to_apply),
                            'payment_type' => 'تسديد',
                            'client_id' => $invoice['client_id'],
                            'client_name' => $invoice['client_name'],
                            'item_name' => $invoice['item_name'],
                            'quantity' => $invoice['quantity'],
                            'price' => $invoice['price'],
                            'item_total' => $invoice['item_total'],
                            'payment_amount' => $payment_to_apply,
                            'record_type' => 'payment',
                            'total_paid' => $payment_to_apply,
                            'adjusted_remaining' => max(0, $invoice_remaining - $payment_to_apply)
                        ];

                        $total_paid += $payment_to_apply;
                        $total_remaining -= $payment_to_apply;
                    }
                }
            }

            // ترتيب البيانات حسب التاريخ
            usort($report_data, function ($a, $b) {
                return strtotime($a['date']) <=> strtotime($b['date']);
            });

            if (empty($report_data)) {
                $success_message = "لا توجد بيانات مطابقة لمعايير البحث.";
            } else {
                // تصحيح أخطاء: تسجيل عدد السجلات ومحتوى أول صف
                error_log("Report data count: " . count($report_data));
                error_log("First row sample: " . json_encode($report_data[0], JSON_UNESCAPED_UNICODE));
            }
        } catch (PDOException $e) {
            error_log("Error fetching report data: " . $e->getMessage());
            $error_message = "حدث خطأ أثناء جلب البيانات: " . $e->getMessage();
        }
    }
}
?>

<!DOCTYPE html>
<html lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Astra - تقارير العملاء</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            min-height: 100vh;
            direction: rtl;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #E74C3C, #3498DB, #2ECC71);
            overflow-x: hidden;
            animation: gradientShift 10s ease infinite;
            background-size: 400% 400%;
        }

        @keyframes gradientShift {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .container-fluid {
            padding: 40px;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }

        .card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 20px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            padding: 20px;
            margin-bottom: 20px;
            animation: fadeIn 1s ease-in-out;
        }

        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        h2 {
            color: #E74C3C;
            font-weight: bold;
            text-align: center;
            margin-bottom: 30px;
            text-shadow: 0 0 5px rgba(231, 76, 60, 0.5);
        }

        .form-label {
            color: #3498DB;
            font-weight: bold;
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            font-size: 0.9rem;
        }

        .form-label i {
            margin-left: 8px;
            color: #2ECC71;
        }

        .form-control, .form-select {
            border-radius: 15px;
            border: 2px solid #E74C3C;
            padding: 8px 12px;
            background: rgba(255, 255, 255, 0.8);
            transition: all 0.3s ease;
            box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.1);
            font-size: 0.9rem;
        }

        .form-control:focus, .form-select:focus {
            border-color: #2ECC71;
            box-shadow: 0 0 10px rgba(46, 204, 113, 0.5), inset 0 2px 5px rgba(0, 0, 0, 0.1);
            background: #fff;
        }

        .btn {
            border-radius: 15px;
            padding: 8px 20px;
            font-weight: bold;
            transition: all 0.4s ease;
            position: relative;
            overflow: hidden;
            font-size: 0.9rem;
        }

        .btn-generate {
            background: linear-gradient(45deg, #2ECC71, #27AE60);
            border: none;
            color: #fff;
        }

        .btn-generate:hover {
            background: linear-gradient(45deg, #27AE60, #219653);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(46, 204, 113, 0.5);
        }

        .btn-cancel {
            background: linear-gradient(45deg, #E74C3C, #C0392B);
            border: none;
            color: #fff;
        }

        .btn-cancel:hover {
            background: linear-gradient(45deg, #C0392B, #A93226);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(231, 76, 60, 0.5);
        }

        .btn-print {
            background: linear-gradient(45deg, #3498DB, #2980B9);
            border: none;
            color: #fff;
        }

        .btn-print:hover {
            background: linear-gradient(45deg, #2980B9, #2471A3);
            transform: translateY(-3px);
            box-shadow: 0 5px 15px rgba(52, 152, 219, 0.5);
        }

        .btn i {
            margin-left: 8px;
        }

        .success-message {
            color: #2ECC71;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease-in-out;
        }

        .error-message {
            color: #E74C3C;
            font-weight: bold;
            text-align: center;
            margin-bottom: 20px;
            animation: fadeIn 0.5s ease-in-out;
        }

        .table {
            background: #fff;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
            width: 100%;
        }

        .table thead {
            background: #E74C3C;
            color: #fff;
        }

        .table tbody tr:hover {
            background: rgba(231, 76, 60, 0.1);
        }

        .totals-box {
            background: linear-gradient(45deg, #F1C40F, #D4AC0D);
            color: #fff;
            padding: 15px;
            border-radius: 15px;
            box-shadow: 0 5px 15px rgba(241, 196, 15, 0.5);
            text-align: center;
            font-weight: bold;
            margin-top: 20px;
        }

        .table-responsive {
            max-height: 50vh;
            overflow-y: auto;
        }

        /* أنماط الطباعة */
        @media print {
            body * {
                visibility: hidden;
            }

            #printArea, #printArea * {
                visibility: visible !important;
                display: block !important;
            }

            #printArea {
                position: absolute;
                top: 0;
                left: 0;
                width: 100%;
                margin: 0;
                padding: 5mm;
                font-size: 8pt;
                line-height: 1.2;
                background: #fff;
                color: #333;
            }

            #printArea .report-header {
                text-align: center;
                margin-bottom: 5mm;
                border-bottom: 1px solid #E74C3C;
                padding-bottom: 3mm;
            }

            #printArea .report-header h1 {
                font-size: 12pt;
                color: #E74C3C;
                margin: 0;
            }

            #printArea .report-header p {
                font-size: 9pt;
                color: #333;
                margin: 2mm 0;
            }

            #printArea table {
                width: 100%;
                border-collapse: collapse;
                margin-bottom: 5mm;
                table-layout: fixed;
                page-break-inside: avoid;
            }

            #printArea table th, #printArea table td {
                border: 1px solid #333;
                padding: 2px;
                text-align: center;
                font-size: 8pt;
                color: #333;
                white-space: nowrap;
                overflow: hidden;
                text-overflow: ellipsis;
            }

            #printArea table th {
                background: #E74C3C;
                color: #fff;
                font-weight: bold;
            }

            #printArea table td {
                background: #fff;
            }

            /* تحديد عرض الأعمدة لتناسب صفحة واحدة */
            #printArea table th:nth-child(1), #printArea table td:nth-child(1) { width: 6%; } /* رقم الفاتورة */
            #printArea table th:nth-child(2), #printArea table td:nth-child(2) { width: 8%; } /* اسم العميل */
            #printArea table th:nth-child(3), #printArea table td:nth-child(3) { width: 7%; } /* التاريخ */
            #printArea table th:nth-child(4), #printArea table td:nth-child(4) { width: 8%; } /* اسم الصنف */
            #printArea table th:nth-child(5), #printArea table td:nth-child(5) { width: 5%; } /* الكمية */
            #printArea table th:nth-child(6), #printArea table td:nth-child(6) { width: 6%; } /* السعر */
            #printArea table th:nth-child(7), #printArea table td:nth-child(7) { width: 7%; } /* إجمالي الصنف */
            #printArea table th:nth-child(8), #printArea table td:nth-child(8) { width: 7%; } /* إجمالي الفاتورة */
            #printArea table th:nth-child(9), #printArea table td:nth-child(9) { width: 5%; } /* الخصم */
            #printArea table th:nth-child(10), #printArea table td:nth-child(10) { width: 6%; } /* الصافي */
            #printArea table th:nth-child(11), #printArea table td:nth-child(11) { width: 6%; } /* المدفوع */
            #printArea table th:nth-child(12), #printArea table td:nth-child(12) { width: 6%; } /* المتبقي */
            #printArea table th:nth-child(13), #printArea table td:nth-child(13) { width: 6%; } /* نوع الدفع */
            #printArea table th:nth-child(14), #printArea table td:nth-child(14) { width: 6%; } /* نوع السجل */

            #printArea .totals {
                text-align: left;
                font-size: 9pt;
                font-weight: bold;
                margin-top: 3mm;
                padding: 3mm;
                border-top: 1px solid #E74C3C;
            }

            #printArea .totals p {
                margin: 2mm 0;
            }

            @page {
                size: A4 landscape;
                margin: 5mm;
            }
        }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="card">
            <h2><i class="fas fa-file-alt me-2"></i> تقارير العملاء</h2>
            <?php if (!empty($success_message)): ?>
                <div class="success-message"><?php echo htmlspecialchars($success_message); ?></div>
            <?php endif; ?>
            <?php if (!empty($error_message)): ?>
                <div class="error-message"><?php echo htmlspecialchars($error_message); ?></div>
            <?php endif; ?>
            <form method="POST">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label"><i class="fas fa-user"></i> العميل</label>
                        <select class="form-select" name="client_id">
                            <option value="all">جميع العملاء</option>
                            <?php foreach ($clients as $client): ?>
                                <option value="<?php echo htmlspecialchars($client['id']); ?>" <?php echo (isset($_POST['client_id']) && $_POST['client_id'] == $client['id']) ? 'selected' : ''; ?>>
                                    <?php echo htmlspecialchars($client['name']); ?>
                                </option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><i class="fas fa-calendar-alt"></i> من تاريخ</label>
                        <input type="date" class="form-control" name="date_from" value="<?php echo htmlspecialchars($date_from ?? ''); ?>">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label"><i class="fas fa-calendar-alt"></i> إلى تاريخ</label>
                        <input type="date" class="form-control" name="date_to" value="<?php echo htmlspecialchars($date_to ?? ''); ?>">
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-generate mt-4" name="generate_report"><i class="fas fa-search"></i> إنشاء التقرير</button>
                    </div>
                    <div class="col-md-2">
                        <button type="button" class="btn btn-cancel mt-4" onclick="window.location.href='dashboard.php'"><i class="fas fa-times"></i> إلغاء</button>
                    </div>
                </div>
            </form>
        </div>

        <?php if (!empty($report_data)): ?>
            <div class="card">
                <h2><i class="fas fa-table me-2"></i> تفاصيل التقرير</h2>
                <div class="mb-3">
                    <button class="btn btn-print" onclick="printReport()"><i class="fas fa-print"></i> طباعة التقرير</button>
                </div>
                <div class="table-responsive">
                    <table class="table table-striped" id="reportTable">
                        <thead>
                            <tr>
                                <th>رقم الفاتورة</th>
                                <th>اسم العميل</th>
                                <th>التاريخ</th>
                                <th>اسم الصنف</th>
                                <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 ($report_data as $row): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($row['invoice_number'] ?? '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['client_name'] ?? 'غير معروف'); ?></td>
                                    <td><?php echo htmlspecialchars($row['date'] ? date('Y-m-d', strtotime($row['date'])) : '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['item_name'] ?? '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['quantity'] ?? '-'); ?></td>
                                    <td><?php echo $row['price'] !== null ? number_format($row['price'], 2) : '-'; ?></td>
                                    <td><?php echo $row['item_total'] !== null ? number_format($row['item_total'], 2) : '-'; ?></td>
                                    <td><?php echo $row['total'] !== null ? number_format($row['total'], 2) : '-'; ?></td>
                                    <td><?php echo $row['discount'] !== null ? number_format($row['discount'], 2) : '-'; ?></td>
                                    <td><?php echo $row['final_total'] !== null ? number_format($row['final_total'], 2) : '-'; ?></td>
                                    <td><?php echo number_format($row['total_paid'] ?? 0, 2); ?></td>
                                    <td><?php echo $row['adjusted_remaining'] !== null ? number_format($row['adjusted_remaining'], 2) : '-'; ?></td>
                                    <td><?php echo $row['payment_type'] ? ($row['payment_type'] == 'cash' ? 'نقدي' : ($row['payment_type'] == 'تسديد' ? 'تسديد' : 'آجل')) : '-'; ?></td>
                                    <td><?php echo $row['record_type'] === 'invoice' ? 'فاتورة' : 'مدفوعات'; ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                <div class="totals-box">
                    <p>إجمالي المشتريات: <?php echo number_format($total_purchases, 2); ?> ج.م</p>
                    <p>إجمالي المدفوع: <?php echo number_format($total_paid, 2); ?> ج.م</p>
                    <p>إجمالي المتبقي: <?php echo number_format($total_remaining, 2); ?> ج.م</p>
                </div>
            </div>

            <!-- منطقة الطباعة -->
            <div id="printArea" style="display: none;">
                <div class="report-header">
                    <h1>تقرير العميل: <?php echo htmlspecialchars($selected_client ?? 'جميع العملاء'); ?></h1>
                    <p>من تاريخ: <?php echo htmlspecialchars($date_from ?? 'غير محدد'); ?> إلى تاريخ: <?php echo htmlspecialchars($date_to ?? 'غير محدد'); ?></p>
                    <p>تاريخ التقرير: <?php echo date('Y-m-d'); ?></p>
                </div>
                <table>
                    <thead>
                        <tr>
                            <th>رقم الفاتورة</th>
                            <th>اسم العميل</th>
                            <th>التاريخ</th>
                            <th>اسم الصنف</th>
                            <th>الكمية</th>
                            <th>السعر</th>
                            <th>إجمالي الصنف</th>
                            <th>إجمالي الفاتورة</th>
                            <th>الخصم</th>
                            <th>الصافي</th>
                            <th>المدفوع</th>
                            <th>المتبقي</th>
                            <th>نوع الدفع</th>
                            <th>نوع السجل</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (!empty($report_data)): ?>
                            <?php foreach ($report_data as $index => $row): ?>
                                <tr>
                                    <td><?php echo htmlspecialchars($row['invoice_number'] ?? '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['client_name'] ?? 'غير معروف'); ?></td>
                                    <td><?php echo htmlspecialchars($row['date'] ? date('Y-m-d', strtotime($row['date'])) : '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['item_name'] ?? '-'); ?></td>
                                    <td><?php echo htmlspecialchars($row['quantity'] ?? '-'); ?></td>
                                    <td><?php echo $row['price'] !== null ? number_format($row['price'], 2) : '-'; ?></td>
                                    <td><?php echo $row['item_total'] !== null ? number_format($row['item_total'], 2) : '-'; ?></td>
                                    <td><?php echo $row['total'] !== null ? number_format($row['total'], 2) : '-'; ?></td>
                                    <td><?php echo $row['discount'] !== null ? number_format($row['discount'], 2) : '-'; ?></td>
                                    <td><?php echo $row['final_total'] !== null ? number_format($row['final_total'], 2) : '-'; ?></td>
                                    <td><?php echo number_format($row['total_paid'] ?? 0, 2); ?></td>
                                    <td><?php echo $row['adjusted_remaining'] !== null ? number_format($row['adjusted_remaining'], 2) : '-'; ?></td>
                                    <td><?php echo $row['payment_type'] ? ($row['payment_type'] == 'cash' ? 'نقدي' : ($row['payment_type'] == 'تسديد' ? 'تسديد' : 'آجل')) : '-'; ?></td>
                                    <td><?php echo $row['record_type'] === 'invoice' ? 'فاتورة' : 'مدفوعات'; ?></td>
                                </tr>
                                <?php
                                    // تصحيح أخطاء: تسجيل محتوى كل صف
                                    error_log("Print row $index: " . json_encode($row, JSON_UNESCAPED_UNICODE));
                                ?>
                            <?php endforeach; ?>
                        <?php else: ?>
                            <tr><td colspan="14">لا توجد بيانات للطباعة</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
                <div class="totals">
                    <p>إجمالي المشتريات: <?php echo number_format($total_purchases, 2); ?> ج.م</p>
                    <p>إجمالي المدفوع: <?php echo number_format($total_paid, 2); ?> ج.م</p>
                    <p>إجمالي المتبقي: <?php echo number_format($total_remaining, 2); ?> ج.م</p>
                </div>
            </div>
        <?php endif; ?>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function printReport() {
            const printTable = document.querySelector('#printArea table tbody');
            console.log('Print area row count: ' + printTable.children.length);

            if (printTable.children.length === 0) {
                alert('لا توجد بيانات للطباعة!');
                return;
            }

            // تأخير الطباعة لضمان تحميل DOM
            setTimeout(() => {
                window.print();
            }, 100);
        }
    </script>
</body>
</html>