<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>فاتورة مبيعات #<?php echo $sale->sale_number; ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;900&display=swap" rel="stylesheet">
    
    <style>
        @page {
            size: 80mm auto;
            margin: 0;
        }
        
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Cairo', Arial, sans-serif;
            width: 80mm;
            margin: 0 auto;
            padding: 10mm 5mm;
            background: white;
            font-size: 11pt;
            line-height: 1.4;
            direction: rtl;
        }
        
        .receipt {
            width: 100%;
        }
        
        /* Header */
        .receipt-header {
            text-align: center;
            margin-bottom: 15px;
            padding-bottom: 15px;
            border-bottom: 2px dashed #000;
        }
        
        .company-logo {
            max-width: 50mm;
            max-height: 20mm;
            margin: 0 auto 10px;
        }
        
        .company-name {
            font-size: 18pt;
            font-weight: 900;
            margin-bottom: 5px;
        }
        
        .company-info {
            font-size: 9pt;
            color: #333;
            line-height: 1.6;
        }
        
        /* Receipt Info */
        .receipt-info {
            margin-bottom: 15px;
            font-size: 10pt;
        }
        
        .receipt-info-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            padding: 3px 0;
        }
        
        .receipt-info-row strong {
            font-weight: 700;
        }
        
        .receipt-number {
            font-size: 16pt;
            font-weight: 900;
            text-align: center;
            margin: 10px 0;
            color: #000;
            background: #f0f0f0;
            padding: 8px;
            border-radius: 5px;
        }
        
        /* Items Table */
        .items-table {
            width: 100%;
            margin-bottom: 15px;
            border-collapse: collapse;
        }
        
        .items-table thead {
            background: #000;
            color: white;
        }
        
        .items-table th {
            padding: 8px 5px;
            font-size: 10pt;
            font-weight: 700;
            text-align: center;
        }
        
        .items-table td {
            padding: 8px 5px;
            border-bottom: 1px solid #ddd;
            text-align: center;
            font-size: 10pt;
        }
        
        .items-table .item-name {
            text-align: right;
            font-weight: 600;
        }
        
        .items-table .item-qty {
            font-weight: 700;
        }
        
        .items-table .item-total {
            font-weight: 700;
        }
        
        /* Totals */
        .totals-section {
            margin-bottom: 15px;
            padding-top: 10px;
            border-top: 2px dashed #000;
        }
        
        .total-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 8px;
            font-size: 11pt;
        }
        
        .total-row.main-total {
            font-size: 16pt;
            font-weight: 900;
            margin-top: 10px;
            padding-top: 10px;
            border-top: 2px solid #000;
            background: #f0f0f0;
            padding: 10px;
            border-radius: 5px;
        }
        
        .total-label {
            font-weight: 600;
        }
        
        .total-value {
            font-weight: 700;
        }
        
        /* Payment Info */
        .payment-section {
            margin-bottom: 15px;
            padding: 10px;
            background: #f8f8f8;
            border-radius: 5px;
        }
        
        .payment-row {
            display: flex;
            justify-content: space-between;
            margin-bottom: 5px;
            font-size: 11pt;
        }
        
        .payment-method {
            font-weight: 900;
            font-size: 12pt;
            text-align: center;
            margin-bottom: 8px;
            color: #27ae60;
        }
        
        /* QR Code Section */
        .qr-section {
            text-align: center;
            margin: 20px 0;
            padding: 15px 0;
            border-top: 2px dashed #000;
            border-bottom: 2px dashed #000;
        }
        
        .qr-code {
            margin: 10px auto;
            width: 35mm;
            height: 35mm;
        }
        
        .qr-code img {
            width: 100%;
            height: 100%;
        }
        
        .qr-text {
            font-size: 9pt;
            color: #666;
            margin-top: 5px;
        }
        
        /* Footer */
        .receipt-footer {
            text-align: center;
            font-size: 10pt;
            margin-top: 15px;
            padding-top: 15px;
            border-top: 2px dashed #000;
        }
        
        .thank-you {
            font-size: 14pt;
            font-weight: 900;
            margin-bottom: 10px;
        }
        
        .footer-info {
            font-size: 9pt;
            color: #666;
            line-height: 1.6;
        }
        
        /* Print Styles */
        @media print {
            body {
                padding: 0;
            }
            
            .no-print {
                display: none !important;
            }
        }
        
        /* Print Button */
        .print-button {
            position: fixed;
            top: 20px;
            right: 20px;
            background: #27ae60;
            color: white;
            border: none;
            padding: 15px 30px;
            font-size: 16px;
            font-weight: 700;
            border-radius: 10px;
            cursor: pointer;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            z-index: 1000;
        }
        
        .print-button:hover {
            background: #229954;
        }
        
        @media print {
            .print-button {
                display: none;
            }
        }
    </style>
</head>
<body>
    <button class="print-button no-print" onclick="window.print()">
        طباعة الفاتورة
    </button>
    
    <div class="receipt">
        <!-- Header -->
        <div class="receipt-header">
            <?php if (get_option('company_logo')): ?>
                <img src="<?php echo base_url('uploads/company/' . get_option('company_logo')); ?>" 
                     alt="Company Logo" 
                     class="company-logo">
            <?php endif; ?>
            
            <div class="company-name"><?php echo get_option('companyname'); ?></div>
            
            <div class="company-info">
                <?php if (get_option('company_address')): ?>
                    <?php echo get_option('company_address'); ?><br>
                <?php endif; ?>
                
                <?php if (get_option('company_phonenumber')): ?>
                    هاتف: <?php echo get_option('company_phonenumber'); ?><br>
                <?php endif; ?>
                
                <?php if (get_option('company_vat')): ?>
                    الرقم الضريبي: <?php echo get_option('company_vat'); ?>
                <?php endif; ?>
            </div>
        </div>

        <!-- Receipt Number -->
        <div class="receipt-number">
            فاتورة رقم: <?php echo $sale->sale_number; ?>
        </div>

        <!-- Receipt Info -->
        <div class="receipt-info">
            <div class="receipt-info-row">
                <span><strong>التاريخ:</strong></span>
                <span><?php echo date('Y-m-d', strtotime($sale->created_at)); ?></span>
            </div>
            <div class="receipt-info-row">
                <span><strong>الوقت:</strong></span>
                <span><?php echo date('h:i A', strtotime($sale->created_at)); ?></span>
            </div>
            <?php if ($sale->customer_name || $sale->customer_phone): ?>
            <div class="receipt-info-row">
                <span><strong>العميل:</strong></span>
                <span>
                    <?php echo $sale->customer_name ?: 'عميل'; ?>
                    <?php if ($sale->customer_phone): ?>
                        <br><?php echo $sale->customer_phone; ?>
                    <?php endif; ?>
                </span>
            </div>
            <?php endif; ?>
            <div class="receipt-info-row">
                <span><strong>الفرع:</strong></span>
                <span><?php echo $branch->name; ?></span>
            </div>
            <div class="receipt-info-row">
                <span><strong>الكاشير:</strong></span>
                <span><?php echo $register->name; ?></span>
            </div>
            <div class="receipt-info-row">
                <span><strong>البائع:</strong></span>
                <span><?php echo get_staff_full_name($sale->user_id); ?></span>
            </div>
            <div class="receipt-info-row">
                <span><strong>الحالة:</strong></span>
                <span>
                    <?php 
                    switch ($sale->status) {
                        case 'completed':
                            echo 'مبيعات';
                            break;
                        case 'refund':
                            echo 'مرتجع';
                            break;
                        case 'pending':
                            echo 'معلقة';
                            break;
                        case 'cancelled':
                            echo 'ملغية';
                            break;
                        default:
                            echo ucfirst($sale->status);
                    }
                    ?>
                </span>
            </div>
            <?php if ($sale->invoice_id): ?>
            <div class="receipt-info-row">
                <span><strong>رقم الفاتورة:</strong></span>
                <span><?php echo $sale->invoice_id; ?></span>
            </div>
            <?php endif; ?>
        </div>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width: 50%;">المنتج</th>
                    <th style="width: 15%;">الكمية</th>
                    <th style="width: 17.5%;">السعر</th>
                    <th style="width: 17.5%;">المجموع</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($items as $item): ?>
                <tr>
                    <td class="item-name"><?php echo $item->product_name; ?></td>
                    <td class="item-qty"><?php echo $item->quantity; ?></td>
                    <td><?php echo number_format($item->unit_price, 2); ?></td>
                    <td class="item-total"><?php echo number_format($item->total, 2); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Totals -->
        <div class="totals-section">
            <div class="total-row">
                <span class="total-label">المجموع الفرعي:</span>
                <span class="total-value"><?php echo number_format($sale->subtotal, 2); ?> ريال</span>
            </div>
            
            <?php if ($sale->discount_amount > 0): ?>
            <div class="total-row">
                <span class="total-label">الخصم:</span>
                <span class="total-value"><?php echo number_format($sale->discount_amount, 2); ?> ريال</span>
            </div>
            <?php endif; ?>
            
            <div class="total-row">
                <span class="total-label">ضريبة القيمة المضافة (15%):</span>
                <span class="total-value"><?php echo number_format($sale->tax_amount, 2); ?> ريال</span>
            </div>
            
            <div class="total-row main-total">
                <span class="total-label">الإجمالي:</span>
                <span class="total-value"><?php echo number_format($sale->total, 2); ?> ريال</span>
            </div>
        </div>

        <!-- Payment Info -->
        <div class="payment-section">
            <div class="payment-method">
                طريقة الدفع: 
                <?php 
                    if ($sale->payment_method == 'cash') {
                        echo 'نقدي';
                    } elseif ($sale->payment_method == 'card') {
                        echo 'بطاقة';
                    } elseif ($sale->payment_method == 'mixed') {
                        echo 'مختلط';
                    }
                ?>
            </div>
            
            <div class="payment-row">
                <span class="total-label">المدفوع:</span>
                <span class="total-value"><?php echo number_format($sale->paid_amount, 2); ?> ريال</span>
            </div>
            
            <?php if ($sale->change_amount > 0): ?>
            <div class="payment-row">
                <span class="total-label">الباقي:</span>
                <span class="total-value"><?php echo number_format($sale->change_amount, 2); ?> ريال</span>
            </div>
            <?php endif; ?>
        </div>

        <!-- QR Code Section -->
        <div class="qr-section">
            <div style="font-weight: 700; font-size: 12pt; margin-bottom: 10px;">
                امسح الكود للتحقق من الفاتورة
            </div>
            <div class="qr-code">
                <?php
                // Generate QR Code URL
                $qr_data = admin_url('pos/verify_receipt/' . $sale->id);
                $qr_url = 'https://api.qrserver.com/v1/create-qr-code/?size=300x300&data=' . urlencode($qr_data);
                ?>
                <img src="<?php echo $qr_url; ?>" alt="QR Code">
            </div>
            <div class="qr-text">
                Scan to verify receipt<br>
                <?php echo $sale->sale_number; ?>
            </div>
        </div>

        <!-- Footer -->
        <div class="receipt-footer">
            <div class="thank-you">
                شكراً لزيارتكم
            </div>
            <div class="footer-info">
                <?php if ($branch->receipt_footer): ?>
                    <?php echo nl2br($branch->receipt_footer); ?>
                <?php else: ?>
                    نسعد بخدمتكم دائماً<br>
                    يرجى الاحتفاظ بالفاتورة للمراجعة أو الاستبدال<br>
                    البضاعة المباعة لا ترد ولا تستبدل إلا بعذر مقبول
                <?php endif; ?>
            </div>
            
            <div style="margin-top: 15px; font-size: 8pt; color: #999;">
                Powered by POS System - <?php echo get_option('companyname'); ?>
            </div>
        </div>
    </div>

    <script>
        // Auto print on load
        window.addEventListener('load', function() {
            setTimeout(function() {
                window.print();
            }, 500);
        });
    </script>
</body>
</html>
