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

function getReceiptData($sales) {
    $db = new Database();
    $pdo = $db->getConnection();
    
    $receipt_data = [];
    $total = 0;
    
    foreach ($sales as $sale) {
        $stmt = $pdo->prepare("SELECT p.name, s.quantity, s.price 
                              FROM sales s 
                              JOIN products p ON p.id = s.product_id 
                              WHERE s.id = ?");
        $stmt->execute([$sale]);
        $item = $stmt->fetch(PDO::FETCH_ASSOC);
        
        if ($item) {
            $subtotal = $item['quantity'] * $item['price'];
            $total += $subtotal;
            $receipt_data[] = [
                'name' => $item['name'],
                'quantity' => $item['quantity'],
                'price' => $item['price'],
                'subtotal' => $subtotal
            ];
        }
    }
    
    return [
        'items' => $receipt_data,
        'total' => $total,
        'date' => date('Y-m-d H:i:s')
    ];
}

function generateReceipt($sale_ids) {
    $data = getReceiptData($sale_ids);
    
    $html = '
    <!DOCTYPE html>
    <html dir="rtl">
    <head>
        <meta charset="UTF-8">
        <title>فاتورة</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 20px;
                direction: rtl;
            }
            .receipt {
                width: 80mm;
                margin: 0 auto;
                padding: 10px;
                border: 1px solid #000;
            }
            .header {
                text-align: center;
                margin-bottom: 20px;
            }
            .items {
                width: 100%;
                margin-bottom: 20px;
            }
            .items th, .items td {
                padding: 5px;
                text-align: right;
            }
            .total {
                text-align: left;
                margin-top: 20px;
                border-top: 1px solid #000;
                padding-top: 10px;
            }
            .footer {
                text-align: center;
                margin-top: 20px;
                font-size: 12px;
            }
            @media print {
                body {
                    width: 80mm;
                    margin: 0;
                    padding: 0;
                }
                .receipt {
                    border: none;
                }
                .no-print {
                    display: none;
                }
            }
        </style>
    </head>
    <body>
        <div class="receipt">
            <div class="header">
                <h2>بقالتي</h2>
                <p>'.date('Y-m-d H:i:s').'</p>
            </div>
            
            <table class="items">
                <tr>
                    <th>المنتج</th>
                    <th>الكمية</th>
                    <th>السعر</th>
                    <th>المجموع</th>
                </tr>';
    
    foreach ($data['items'] as $item) {
        $html .= sprintf('
                <tr>
                    <td>%s</td>
                    <td>%d</td>
                    <td>%.2f</td>
                    <td>%.2f</td>
                </tr>',
            htmlspecialchars($item['name']),
            $item['quantity'],
            $item['price'],
            $item['subtotal']
        );
    }
    
    $html .= sprintf('
            </table>
            
            <div class="total">
                <h3>المجموع الكلي: %.2f درهم</h3>
            </div>
            
            <div class="footer">
                <p>شكراً لزيارتكم</p>
            </div>
        </div>
        <div class="no-print" style="text-align: center; margin-top: 20px;">
            <button onclick="window.print()">طباعة الفاتورة</button>
            <button onclick="window.close()">إغلاق</button>
        </div>
    </body>
    </html>', $data['total']);
    
    return $html;
}

// Si la page est appelée directement avec des IDs de vente
if (isset($_GET['sales'])) {
    $sale_ids = explode(',', $_GET['sales']);
    echo generateReceipt($sale_ids);
}
?>
