<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>فاتورة #{{ $order->id }}</title>
    <style>
        * { font-family: 'xbriyaz', sans-serif; }
        body { margin: 20px; font-size: 12px; color: #333; direction: rtl; text-align: right; }
        .header { text-align: center; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 2px solid #10b981; }
        .logo { font-size: 24px; font-weight: bold; color: #10b981; }
        .invoice-title { font-size: 18px; margin-top: 10px; }
        .meta { margin: 20px 0; }
        .meta-row { padding: 5px 0; }
        .meta-label { font-weight: bold; color: #666; }
        table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        th, td { border: 1px solid #ddd; padding: 10px; text-align: right; }
        th { background: #f8fafc; font-weight: bold; }
        .summary { margin-top: 30px; margin-right: 0; width: 350px; }
        .summary-row { padding: 8px 0; border-bottom: 1px solid #eee; }
        .summary-label { font-weight: bold; color: #555; display: inline-block; width: 200px; }
        .summary-value { font-weight: bold; }
        .total-row { font-size: 16px; color: #10b981; border-bottom: none; margin-top: 10px; padding-top: 10px; border-top: 2px solid #10b981; }
        .footer { margin-top: 40px; text-align: center; font-size: 10px; color: #888; }
    </style>
</head>
<body dir="rtl">
    <div class="header">
        <div class="logo">تاجر بلس</div>
        <div class="invoice-title">فاتورة عميل - #{{ $order->id }}</div>
    </div>

    <div class="meta">
        <div class="meta-row"><span class="meta-label">رقم الفاتورة:</span> #{{ $order->id }}</div>
        <div class="meta-row"><span class="meta-label">التاريخ:</span> {{ $order->created_at->format('Y-m-d') }}</div>
        <div class="meta-row"><span class="meta-label">العميل:</span> {{ $order->user->name }}</div>
        @if($order->user->phone)
        <div class="meta-row"><span class="meta-label">الهاتف:</span> {{ $order->user->phone }}</div>
        @endif
        <div class="meta-row"><span class="meta-label">طريقة الدفع:</span> {{ $order->paymentMethod?->name ?? $order->payment_method ?? '-' }}</div>
    </div>

    <table dir="rtl">
        <thead>
            <tr>
                <th>#</th>
                <th>المنتج</th>
                <th>إجمالي الكمية</th>
                <th>سعر الوحدة</th>
                <th>الاجمالي</th>
            </tr>
        </thead>
        <tbody>
            @foreach($order->getItemsGroupedByProduct() as $i => $group)
            <tr>
                <td>{{ $i + 1 }}</td>
                <td>{{ $group['product_name'] }}</td>
                <td>{{ $group['total_quantity'] }}</td>
                <td>{{ number_format($group['unit_price'], 2) }} ج.م</td>
                <td>{{ number_format($group['total_line_amount'], 2) }} ج.م</td>
            </tr>
            @endforeach
        </tbody>
    </table>

    <div class="summary">
        <div class="summary-row"><span class="summary-label">اجمالي البضاعة:</span><span class="summary-value">{{ number_format($order->subtotal, 2) }} ج.م</span></div>
        @if($order->discount_from_points > 0)
        <div class="summary-row"><span class="summary-label">خصم النقاط:</span><span class="summary-value">-{{ number_format($order->discount_from_points, 2) }} ج.م</span></div>
        @endif
        <div class="summary-row"><span class="summary-label">مصاريف الشحن:</span><span class="summary-value">{{ number_format($order->shipping_cost ?? 0, 2) }} ج.م</span></div>
        @if(($order->payment_fee ?? 0) > 0)
        <div class="summary-row"><span class="summary-label">رسوم الدفع:</span><span class="summary-value">{{ number_format($order->payment_fee, 2) }} ج.م</span></div>
        @endif
        @if(($order->extra_charges ?? 0) > 0)
        <div class="summary-row"><span class="summary-label">مصروفات اضافية:</span><span class="summary-value">{{ number_format($order->extra_charges, 2) }} ج.م</span></div>
        @endif
        <div class="summary-row total-row"><span class="summary-label">الاجمالي النهائي المطلوب دفعه:</span><span class="summary-value">{{ number_format($order->total, 2) }} ج.م</span></div>
    </div>

    @if($order->notes)
    <div style="margin-top: 25px; padding: 10px; background: #f8fafc; border-radius: 6px;">
        <strong>ملاحظات:</strong> {{ $order->notes }}
    </div>
    @endif

    <div class="footer" style="margin-top: 50px;">
        تاجر بلس - شكراً لتعاملكم معنا
    </div>
</body>
</html>
