<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="UTF-8">
    <title>{{ __('فاتورة') }} - {{ $invoice->number }}</title>
    @php
        $branding = app(\App\Domain\Shared\Services\SettingsService::class);
        $brandLogo = $branding->get('branding.logo');
        $brandPrimary = $branding->get('branding.primary_color', '#1e40af');
        $receiptFooter = $branding->get('branding.receipt_footer_text', __('تم إنشاء هذه الفاتورة إلكترونياً ولا تحتاج إلى توقيع'));
        $academyName = app()->bound('current_academy') ? app('current_academy')->name_ar : 'الكابتن';
        $branch = \App\Domain\Identity\Models\Branch::where('academy_id', app('current_academy')?->id)->first();
    @endphp
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body { font-family: 'Cairo', 'Noto Sans Arabic', sans-serif; direction: rtl; padding: 30px; font-size: 13px; color: #333; }
        .header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 30px; padding-bottom: 20px; border-bottom: 2px solid {{ $brandPrimary }}; }
        .header h1 { font-size: 24px; color: {{ $brandPrimary }}; }
        .header .meta { text-align: left; font-size: 12px; color: #666; }
        .header .meta p { margin-bottom: 3px; }
        .header .brand { display: flex; align-items: center; gap: 12px; }
        .header .brand img { height: 50px; object-fit: contain; }
        .parties { display: grid; grid-template-columns: 1fr 1fr; gap: 30px; margin-bottom: 25px; }
        .party { padding: 15px; background: #f8fafc; border-radius: 8px; }
        .party h3 { font-size: 12px; color: #64748b; margin-bottom: 8px; text-transform: uppercase; }
        .party p { margin-bottom: 3px; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
        th { background: {{ $brandPrimary }}; color: white; padding: 10px 12px; text-align: right; font-size: 12px; }
        td { padding: 10px 12px; border-bottom: 1px solid #e2e8f0; }
        .totals { margin-right: auto; width: 280px; }
        .totals .row { display: flex; justify-content: space-between; padding: 6px 0; font-size: 13px; }
        .totals .row.total { font-weight: bold; font-size: 16px; border-top: 2px solid #333; padding-top: 10px; margin-top: 5px; }
        .status-badge { display: inline-block; padding: 3px 12px; border-radius: 12px; font-size: 11px; font-weight: bold; }
        .status-paid { background: #dcfce7; color: #166534; }
        .status-sent { background: #dbeafe; color: #1e40af; }
        .status-overdue { background: #fef2f2; color: #991b1b; }
        .status-partially_paid { background: #fef9c3; color: #854d0e; }
        .payments { margin-top: 20px; }
        .payments h3 { font-size: 14px; margin-bottom: 10px; }
        .footer { margin-top: 40px; text-align: center; font-size: 11px; color: #94a3b8; border-top: 1px solid #e2e8f0; padding-top: 15px; }
        .no-print { margin-bottom: 15px; }
        @media print { .no-print { display: none; } body { padding: 15px; } }
    </style>
</head>
<body>
    <div class="no-print">
        <button onclick="window.print()" style="padding: 8px 20px; background: {{ $brandPrimary }}; color: white; border: none; border-radius: 6px; cursor: pointer;">{{ __('طباعة') }}</button>
    </div>

    <div class="header">
        <div class="brand">
            @if($brandLogo)
            <img src="{{ Storage::disk('public')->url($brandLogo) }}" alt="{{ $academyName }}">
            @endif
            <div>
                <h1>{{ __('فاتورة') }}</h1>
                <p style="color: #64748b; margin-top: 4px;">{{ $academyName }}</p>
                @if($branch)
                <p style="color: #64748b; font-size: 11px;">{{ $branch->name_ar }}</p>
                @if($branch->address)
                <p style="color: #94a3b8; font-size: 11px;">{{ $branch->address }}</p>
                @endif
                @if($branch->phone)
                <p style="color: #94a3b8; font-size: 11px;" dir="ltr">{{ $branch->phone }}</p>
                @endif
                @endif
            </div>
        </div>
        <div class="meta">
            <p><strong>{{ __('رقم الفاتورة') }}:</strong> {{ $invoice->number }}</p>
            <p><strong>{{ __('التاريخ') }}:</strong> {{ $invoice->issue_date ?? $invoice->created_at?->format('Y-m-d') }}</p>
            @if($invoice->due_date)
            <p><strong>{{ __('تاريخ الاستحقاق') }}:</strong> {{ $invoice->due_date }}</p>
            @endif
            <p style="margin-top: 5px;">
                <span class="status-badge status-{{ $invoice->status }}">{{ __($invoice->status) }}</span>
            </p>
        </div>
    </div>

    <div class="parties">
        <div class="party">
            <h3>{{ __('العميل') }}</h3>
            @if($invoice->billable)
            <p><strong>{{ $invoice->billable->full_name ?? $invoice->billable->name_ar ?? '-' }}</strong></p>
            @if($invoice->billable->phone ?? null)
            <p dir="ltr">{{ $invoice->billable->phone }}</p>
            @endif
            @endif
        </div>
        <div class="party">
            <h3>{{ __('تفاصيل') }}</h3>
            @if($invoice->notes)
            <p>{{ $invoice->notes }}</p>
            @endif
        </div>
    </div>

    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>{{ __('الوصف') }}</th>
                <th>{{ __('الكمية') }}</th>
                <th>{{ __('سعر الوحدة') }}</th>
                <th>{{ __('الإجمالي') }}</th>
            </tr>
        </thead>
        <tbody>
            @forelse($invoice->items as $i => $item)
            <tr>
                <td>{{ $i + 1 }}</td>
                <td>{{ $item->description }}</td>
                <td>{{ $item->quantity }}</td>
                <td dir="ltr">{{ format_money($item->unit_price) }}</td>
                <td dir="ltr">{{ format_money($item->total_amount) }}</td>
            </tr>
            @empty
            <tr><td colspan="5" style="text-align: center; color: #999;">{{ __('لا توجد بنود') }}</td></tr>
            @endforelse
        </tbody>
    </table>

    <div class="totals">
        <div class="row">
            <span>{{ __('المجموع الفرعي') }}</span>
            <span dir="ltr">{{ format_money($invoice->subtotal_amount ?? $invoice->total_amount) }}</span>
        </div>
        @if($invoice->discount_amount > 0)
        <div class="row">
            <span>{{ __('الخصم') }}</span>
            <span dir="ltr" style="color: #dc2626;">-{{ format_money($invoice->discount_amount) }}</span>
        </div>
        @endif
        @if(($invoice->service_fee_amount ?? 0) > 0)
        <div class="row">
            <span>{{ __('رسوم الخدمة') }}</span>
            <span dir="ltr">{{ format_money($invoice->service_fee_amount) }}</span>
        </div>
        @endif
        @if($invoice->tax_amount > 0)
        <div class="row">
            <span>{{ __('الضريبة') }}</span>
            <span dir="ltr">{{ format_money($invoice->tax_amount) }}</span>
        </div>
        @endif
        <div class="row total">
            <span>{{ __('الإجمالي') }}</span>
            <span dir="ltr">{{ format_money($invoice->total_amount) }}</span>
        </div>
        @if($invoice->paid_amount > 0)
        <div class="row" style="color: #166534;">
            <span>{{ __('المدفوع') }}</span>
            <span dir="ltr">{{ format_money($invoice->paid_amount) }}</span>
        </div>
        <div class="row" style="font-weight: bold; color: #dc2626;">
            <span>{{ __('المتبقي') }}</span>
            <span dir="ltr">{{ format_money($invoice->due_amount) }}</span>
        </div>
        @endif
    </div>

    @if($invoice->payments && $invoice->payments->count())
    <div class="payments">
        <h3>{{ __('المدفوعات') }}</h3>
        <table>
            <thead>
                <tr>
                    <th>{{ __('التاريخ') }}</th>
                    <th>{{ __('الطريقة') }}</th>
                    <th>{{ __('المبلغ') }}</th>
                    <th>{{ __('الحالة') }}</th>
                </tr>
            </thead>
            <tbody>
                @foreach($invoice->payments as $payment)
                <tr>
                    <td dir="ltr">{{ $payment->created_at?->format('Y-m-d') }}</td>
                    <td>{{ __($payment->method) }}</td>
                    <td dir="ltr">{{ format_money($payment->amount) }}</td>
                    <td>{{ __($payment->status) }}</td>
                </tr>
                @endforeach
            </tbody>
        </table>
    </div>
    @endif

    <div class="footer">
        {{ $receiptFooter }}
    </div>
</body>
</html>
