@php
    $fields = $data['fields'] ?? [];
    $settings = $data['settings'] ?? [];
    $sections = $data['sections'] ?? [];
    $currency = $settings['currency_symbol'] ?? 'ج.م';

    $sectionEnabled = fn(string $key) => collect($sections)->firstWhere('key', $key)['enabled'] ?? true;
@endphp
<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="UTF-8">
    <title>{{ __('إيصال') }} #{{ $data['receipt_number'] ?? '' }}</title>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: 'Cairo', 'Noto Sans Arabic', sans-serif;
            font-size: {{ ($settings['font_size'] ?? 'normal') === 'small' ? '11px' : '13px' }};
            width: {{ $settings['paper_width'] ?? '80mm' }};
            padding: 8px;
            color: #000;
        }
        .text-center { text-align: center; }
        .text-end { text-align: left; }
        .text-start { text-align: right; }
        .font-bold { font-weight: bold; }
        .mt-2 { margin-top: 8px; }
        .mt-1 { margin-top: 4px; }
        .mb-2 { margin-bottom: 8px; }
        .border-top { border-top: 1px dashed #000; padding-top: 6px; margin-top: 6px; }
        .border-bottom { border-bottom: 1px dashed #000; padding-bottom: 6px; margin-bottom: 6px; }
        .flex { display: flex; justify-content: space-between; }
        .text-sm { font-size: 11px; }
        .text-xs { font-size: 10px; }
        .text-lg { font-size: 16px; }
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 3px 2px; text-align: right; font-size: 11px; }
        th { border-bottom: 1px solid #000; font-weight: bold; }
        td.num { text-align: left; direction: ltr; }
        .total-row { font-weight: bold; font-size: 14px; }
        @media print {
            body { width: {{ $settings['paper_width'] ?? '80mm' }}; }
            @page { size: {{ $settings['paper_width'] ?? '80mm' }} auto; margin: 0; }
        }
    </style>
</head>
<body>
    {{-- Header --}}
    @if($sectionEnabled('header'))
    <div class="text-center mb-2 border-bottom">
        @if(($fields['header_logo'] ?? true) && isset($data['logo_url']))
            <img src="{{ $data['logo_url'] }}" style="height: {{ $settings['logo_height'] ?? '40px' }}; margin: 0 auto 6px;">
        @endif
        @if($fields['header_academy_name'] ?? true)
            <div class="font-bold text-lg">{{ $data['academy_name'] ?? '' }}</div>
        @endif
        @if($fields['header_branch_name'] ?? true)
            <div>{{ $data['branch_name'] ?? '' }}</div>
        @endif
        @if(($fields['header_branch_address'] ?? true) && !empty($data['branch_address']))
            <div class="text-xs">{{ $data['branch_address'] }}</div>
        @endif
        @if(($fields['header_branch_phone'] ?? true) && !empty($data['branch_phone']))
            <div class="text-xs" dir="ltr">{{ $data['branch_phone'] }}</div>
        @endif
    </div>
    @endif

    {{-- Receipt info --}}
    @if($sectionEnabled('receipt_info'))
    <div class="border-bottom">
        <div class="flex">
            <span>{{ __('رقم الإيصال') }}:</span>
            <span class="font-bold" dir="ltr">{{ $data['receipt_number'] ?? '' }}</span>
        </div>
        @if($fields['receipt_date'] ?? true)
        <div class="flex">
            <span>{{ __('التاريخ') }}:</span>
            <span dir="ltr">{{ $data['date'] ?? '' }}</span>
        </div>
        @endif
        @if($fields['receipt_time'] ?? true)
        <div class="flex">
            <span>{{ __('الوقت') }}:</span>
            <span dir="ltr">{{ $data['time'] ?? '' }}</span>
        </div>
        @endif
        @if(($fields['cashier_name'] ?? true) && !empty($data['cashier_name']))
        <div class="flex">
            <span>{{ __('الكاشير') }}:</span>
            <span>{{ $data['cashier_name'] }}</span>
        </div>
        @endif
    </div>
    @endif

    {{-- Customer info --}}
    @if($sectionEnabled('customer_info') && ($fields['participant_name'] ?? true) && !empty($data['participant_name']))
    <div class="border-bottom">
        <div class="flex">
            <span>{{ __('العميل') }}:</span>
            <span>{{ $data['participant_name'] }}</span>
        </div>
        @if(($fields['participant_code'] ?? false) && !empty($data['participant_code']))
        <div class="flex text-xs">
            <span>{{ __('الكود') }}:</span>
            <span dir="ltr">{{ $data['participant_code'] }}</span>
        </div>
        @endif
    </div>
    @endif

    {{-- Items --}}
    @if($sectionEnabled('items') && ($fields['items_table'] ?? true))
    <table class="mt-1 mb-2">
        <thead>
            <tr>
                <th>{{ __('الصنف') }}</th>
                @if($fields['item_quantity'] ?? true)<th class="num">{{ __('كمية') }}</th>@endif
                @if($fields['item_unit_price'] ?? true)<th class="num">{{ __('سعر') }}</th>@endif
                @if($fields['item_discount'] ?? true)<th class="num">{{ __('خصم') }}</th>@endif
                @if($fields['item_total'] ?? true)<th class="num">{{ __('إجمالي') }}</th>@endif
            </tr>
        </thead>
        <tbody>
            @foreach($data['items'] ?? [] as $item)
            <tr>
                <td>{{ $item['name'] }}</td>
                @if($fields['item_quantity'] ?? true)<td class="num">{{ $item['quantity'] }}</td>@endif
                @if($fields['item_unit_price'] ?? true)<td class="num">{{ number_format($item['unit_price'] / 100, 2) }}</td>@endif
                @if($fields['item_discount'] ?? true)<td class="num">{{ $item['discount'] > 0 ? number_format($item['discount'] / 100, 2) : '-' }}</td>@endif
                @if($fields['item_total'] ?? true)<td class="num">{{ number_format($item['total'] / 100, 2) }}</td>@endif
            </tr>
            @endforeach
        </tbody>
    </table>
    @endif

    {{-- Totals --}}
    @if($sectionEnabled('totals'))
    <div class="border-top">
        @if($fields['subtotal'] ?? true)
        <div class="flex">
            <span>{{ __('المجموع') }}:</span>
            <span dir="ltr">{{ number_format(($data['subtotal'] ?? 0) / 100, 2) }} {{ $currency }}</span>
        </div>
        @endif
        @if(($fields['discount_total'] ?? true) && ($data['discount_amount'] ?? 0) > 0)
        <div class="flex">
            <span>{{ __('الخصم') }}:</span>
            <span dir="ltr">-{{ number_format($data['discount_amount'] / 100, 2) }} {{ $currency }}</span>
        </div>
        @endif
        @if(($fields['tax_amount'] ?? true) && ($data['tax_amount'] ?? 0) > 0)
        <div class="flex">
            <span>{{ __('الضريبة') }}:</span>
            <span dir="ltr">{{ number_format($data['tax_amount'] / 100, 2) }} {{ $currency }}</span>
        </div>
        @endif
        @if(($data['service_fee_amount'] ?? 0) > 0)
        <div class="flex">
            <span>{{ __('مصاريف خدمة') }}:</span>
            <span dir="ltr">{{ number_format($data['service_fee_amount'] / 100, 2) }} {{ $currency }}</span>
        </div>
        @endif
        <div class="flex total-row mt-1 border-top">
            <span>{{ __('الإجمالي') }}:</span>
            <span dir="ltr">{{ number_format(($data['total_amount'] ?? 0) / 100, 2) }} {{ $currency }}</span>
        </div>
    </div>
    @endif

    {{-- Payment method --}}
    @if($sectionEnabled('payment') && ($fields['payment_method'] ?? true))
    <div class="mt-2 border-top">
        <div class="flex">
            <span>{{ __('طريقة الدفع') }}:</span>
            <span>{{ $data['payment_method_label'] ?? '' }}</span>
        </div>
        @if(($fields['payment_split_details'] ?? true) && !empty($data['split_payments']))
            @foreach($data['split_payments'] as $sp)
            <div class="flex text-xs">
                <span>{{ $sp['method'] }}:</span>
                <span dir="ltr">{{ number_format($sp['amount'] / 100, 2) }} {{ $currency }}</span>
            </div>
            @endforeach
        @endif
        @if(($fields['invoice_number'] ?? false) && !empty($data['invoice_number']))
        <div class="flex text-xs mt-1">
            <span>{{ __('رقم الفاتورة') }}:</span>
            <span dir="ltr">{{ $data['invoice_number'] }}</span>
        </div>
        @endif
    </div>
    @endif

    {{-- Footer --}}
    @if($sectionEnabled('footer'))
    <div class="mt-2 border-top text-center text-xs">
        @if(($fields['footer_text'] ?? true) && !empty($data['footer_text']))
            <p class="mt-1 font-bold">{{ $data['footer_text'] }}</p>
        @endif
        @if(($fields['footer_return_policy'] ?? true) && !empty($data['return_policy']))
            <p class="mt-1">{{ $data['return_policy'] }}</p>
        @endif
        @if(($fields['footer_thank_you'] ?? true))
            <p class="mt-2 font-bold">{{ __('شكراً لزيارتكم') }}</p>
        @endif
    </div>
    @endif

    <script>window.onload = function() { window.print(); }</script>
</body>
</html>
