<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>تفاصيل الفاتورة</title>
    <style>
        body { font-family: Arial, sans-serif; direction: rtl; line-height: 1.6; margin: 0; padding: 10px; }
        .header { text-align: center; margin-bottom: 30px; border-bottom: 2px solid #333; padding-bottom: 10px; }
        .header h1 { margin: 0; font-size: 24px; }
        .header p { margin: 5px 0; }
        .info-section { margin-bottom: 20px; }
        .info-section h3 { background: #f0f0f0; padding: 10px; margin: 0; font-size: 14px; }
        .info-row { display: flex; justify-content: space-between; padding: 8px 10px; border-bottom: 1px solid #eee; }
        .info-row .label { font-weight: bold; color: #333; }
        .info-row .value { color: #666; }
        .table { width: 100%; border-collapse: collapse; margin: 20px 0; }
        .table th { background: #333; color: white; padding: 10px; text-align: right; font-weight: bold; }
        .table td { padding: 10px; border-bottom: 1px solid #ddd; text-align: right; }
        .table tr:nth-child(even) { background: #f9f9f9; }
        .summary { float: left; width: 40%; margin-top: 20px; }
        .summary-row { display: flex; justify-content: space-between; padding: 8px; border-bottom: 1px solid #eee; }
        .summary-row .label { font-weight: bold; }
        .summary-row.total { background: #f0f0f0; font-size: 16px; font-weight: bold; }
        .summary-row.pending { background: #fff3cd; font-size: 16px; font-weight: bold; }
        .page-break { page-break-after: always; }
    </style>
</head>
<body>
    <div class="header">
        <h1>فاتورة مبيعات</h1>
        <p>رقم الفاتورة: {{ $invoice['invoice_number'] ?? '-' }}</p>
        <p>التاريخ: {{ isset($invoice['invoice_date']) ? \Carbon\Carbon::parse($invoice['invoice_date'])->format('d-m-Y') : '-' }}</p>
    </div>

    <div class="info-section">
        <h3>بيانات الفاتورة والعميل</h3>
        <div class="info-row">
            <span class="label">رقم الفاتورة:</span>
            <span class="value">{{ $invoice['invoice_number'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">تاريخ الفاتورة:</span>
            <span class="value">{{ isset($invoice['invoice_date']) ? \Carbon\Carbon::parse($invoice['invoice_date'])->format('d-m-Y') : '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">حالة الفاتورة:</span>
            <span class="value">{{ $invoice['status'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">اسم العميل:</span>
            <span class="value">{{ $invoice['customer_name'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">رقم العميل:</span>
            <span class="value">{{ $invoice['customer_code'] ?? '-' }}</span>
        </div>
        <div class="info-row">
            <span class="label">البريد الإلكتروني:</span>
            <span class="value">{{ $invoice['customer_email'] ?? '-' }}</span>
        </div>
    </div>

    <table class="table">
        <thead>
            <tr>
                <th>البند</th>
                <th>الوصف</th>
                <th>الكمية</th>
                <th>السعر</th>
                <th>الخصم %</th>
                <th>المبلغ</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoice['line_items'] ?? [] as $item)
                <tr>
                    <td>{{ $item['line_number'] ?? '-' }}</td>
                    <td>{{ $item['description'] ?? '-' }}</td>
                    <td>{{ number_format($item['quantity'] ?? 0, 2) }}</td>
                    <td>{{ number_format($item['unit_price'] ?? 0, 2) }}</td>
                    <td>{{ $item['discount_percentage'] ?? 0 }}%</td>
                    <td>{{ number_format($item['line_total'] ?? 0, 2) }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" style="text-align: center;">لا توجد بنود</td>
                </tr>
            @endforelse
        </tbody>
    </table>

    <div style="clear: both;"></div>

    <div class="summary">
        <div class="summary-row">
            <span class="label">المجموع الفرعي:</span>
            <span>{{ number_format($invoice['subtotal'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">الخصم:</span>
            <span>{{ number_format($invoice['discount_amount'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">الضريبة:</span>
            <span>{{ number_format($invoice['tax_amount'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row total">
            <span class="label">الإجمالي:</span>
            <span>{{ number_format($invoice['total_amount'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row">
            <span class="label">المدفوع:</span>
            <span>{{ number_format($invoice['paid_amount'] ?? 0, 2) }} ريال</span>
        </div>
        <div class="summary-row pending">
            <span class="label">المتبقي:</span>
            <span>{{ number_format(($invoice['total_amount'] ?? 0) - ($invoice['paid_amount'] ?? 0), 2) }} ريال</span>
        </div>
    </div>

    @if(count($invoice['payments'] ?? []) > 0)
    <div class="page-break"></div>
    <div class="info-section">
        <h3>سجل الدفعات</h3>
    </div>
    <table class="table">
        <thead>
            <tr>
                <th>تاريخ الدفع</th>
                <th>طريقة الدفع</th>
                <th>المرجع</th>
                <th>المبلغ</th>
            </tr>
        </thead>
        <tbody>
            @foreach($invoice['payments'] as $payment)
                <tr>
                    <td>{{ isset($payment['payment_date']) ? \Carbon\Carbon::parse($payment['payment_date'])->format('d-m-Y') : '-' }}</td>
                    <td>{{ $payment['payment_method'] ?? '-' }}</td>
                    <td>{{ $payment['reference_number'] ?? '-' }}</td>
                    <td>{{ number_format($payment['amount'] ?? 0, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>
    @endif

    <div style="margin-top: 50px; text-align: center; color: #999; font-size: 12px;">
        <p>تم إنشاء هذا التقرير بواسطة نظام إدارة المشاريع</p>
        <p>{{ date('d-m-Y H:i') }}</p>
    </div>
</body>
</html>
