<!DOCTYPE html>
<html lang="ar" dir="rtl">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="cache-control" content="no-cache" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="-1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style>
        * {
            box-sizing: border-box;
            font-family: 'Tahoma', sans-serif;
        }

        body {
            direction: rtl;
            margin: 0;
            padding: 0;
            font-size: 11px;
            color: #000;
        }

        .bill {
            width: 100%;
            max-width: 80mm;
            padding: 0 5px;
            margin: auto;
        }

        .header {
            text-align: center;
            border-bottom: 1px dashed #999;
            padding-bottom: 5px;
            margin-bottom: 5px;
        }

        .header img {
            max-height: 60px;
            display: block;
            margin: 0 auto 5px;
        }

        .company-info {
            font-size: 11px;
            line-height: 1.4;
        }

        .qr-logo {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 5px;
        }

        .qr-logo img {
            max-height: 60px;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        .info-table td {
            padding: 2px 0;
            vertical-align: top;
        }

        .info-label {
            font-weight: bold;
            width: 35%;
        }

        .divider {
            border-bottom: 1px dashed #999;
            margin: 5px 0;
        }

        .products th, .products td {
            padding: 3px 0;
            text-align: right;
        }

        .products th {
            border-bottom: 1px solid #000;
            font-weight: bold;
            font-size: 11px;
        }

        .totals td {
            padding: 2px 0;
        }

        .totals .label {
            font-weight: bold;
        }

        .footer {
            text-align: center;
            margin-top: 8px;
            border-top: 1px dashed #999;
            padding-top: 5px;
        }

        .footer p {
            margin: 2px 0;
            font-size: 10px;
        }
    </style>
</head>

<body>
    <div class="bill">
        <div class="qr-logo">
            <img width="105px" src="{{ asset(setting('logo')) }}" alt="Logo">
            <img width="105px" src="{{ asset('qr.jpeg') }}" alt="QR Code">
        </div>

        <div class="header">
            <h3>{{ setting('title_ar') }}</h3>
            <p class="company-info">
                {{ setting('email') }} <br> {{ setting('phone') }}
            </p>
        </div>

        <table class="info-table">
            <tr><td class="info-label">رقم الطلب:</td><td>{{ $Order->id }}</td></tr>
            @if($Order->client_id != 1)
            <tr><td class="info-label">الاسم:</td><td>{{ $Order->Client->name }}</td></tr>
            <tr><td class="info-label">الهاتف:</td><td>{{ $Order->Client->Country->phone_code . $Order->Client->phone }}</td></tr>
            <tr><td class="info-label">الايميل:</td><td>{{ $Order->Client->email }}</td></tr>
            @endif
            <tr><td class="info-label">التاريخ:</td><td>{{ $Order->created_at->format('Y-m-d') }}</td></tr>
            <tr><td class="info-label">الوقت:</td><td>{{ $Order->created_at->format('h:i A') }}</td></tr>
            @if($Order->Payment)
                <tr><td class="info-label">الدفع:</td><td>{{ $Order->Payment->title_ar }}</td></tr>
            @endif
        </table>

        @if($Order->Address)
            <div class="divider"></div>
            <table class="info-table">
                @if($Order->Address->Region || $Order->Address->City)
                    <tr><td class="info-label">المنطقة:</td><td>{{ $Order->Address->Region?->title_ar }} {{ $Order->Address->City?->title_ar }}</td></tr>
                @endif
                @if($Order->Address->block)
                    <tr><td class="info-label">المجمع:</td><td>{{ $Order->Address->block }}</td></tr>
                @endif
                @if($Order->Address->road)
                    <tr><td class="info-label">الطريق:</td><td>{{ $Order->Address->road }}</td></tr>
                @endif
                @if($Order->Address->building)
                    <tr><td class="info-label">البناية:</td><td>{{ $Order->Address->building }}</td></tr>
                @endif
                @if($Order->Address->apartment)
                    <tr><td class="info-label">الشقة:</td><td>{{ $Order->Address->apartment }}</td></tr>
                @endif
                @if($Order->Address->additional_directions)
                    <tr><td class="info-label">ملاحظات:</td><td>{{ $Order->Address->additional_directions }}</td></tr>
                @endif
            </table>
        @endif

        <div class="divider"></div>

        <table class="products">
            <thead>
                <tr>
                    <th>المنتج</th>
                    <th style="text-align:center;">الكمية</th>
                    <th style="text-align:left;">السعر</th>
                </tr>
            </thead>
            <tbody>
                @foreach($Order->Products as $Item)
                    <tr>
                        <td>
                            {{ $Item->Product->title_ar }}
                            @foreach($Item->Items as $option)
                                <br><small>{!! preg_replace('/-(.*?):/', '<b>$1</b>:', str_replace('*', '', $option->title_ar)) !!}</small>
                            @endforeach
                            @if($Item->note)
                                <br><small>📋 {{ $Item->note }}</small>
                            @endif
                        </td>
                        <td style="text-align:center;">{{ $Item->quantity }}</td>
                        <td style="text-align:left;">{{ format_number($Item->total * Country()->currancy_value) }} {{ Country()->currancy_code }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>

        <div class="divider"></div>


        <table class="totals">
            <tr>
                <td class="label">المجموع الفرعي:</td>
                <td style="text-align:left;">{{ format_number($Order->sub_total * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
            </tr>
        
            @if($Order->discount > 0)
                <tr>
                    <td class="label">الخصم:</td>
                    <td style="text-align:left;">{{ format_number($Order->discount * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                </tr>
            @endif
        
            @if($Order->coupon > 0)
                <tr>
                    <td class="label">الخصم:</td>
                    <td style="text-align:left;">{{ format_number($Order->coupon * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                </tr>
            @endif
        
        
            @if($Order->points_value > 0)
                <tr>
                    <td class="label">النقاط:</td>
                    <td style="text-align:left;">{{ format_number($Order->points_value * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                </tr>
            @endif
        
            @if($Order->vat > 0)
                <tr>
                    <td class="label">ضريبة القيمة المضافة ({{ $Order->vat_percentage ?? 0 }}%):</td>
                    <td style="text-align:left;">{{ format_number($Order->vat * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                </tr>
            @endif
        
            @if($Order->charge_cost > 0)
                <tr>
                    <td class="label">التوصيل:</td>
                    <td style="text-align:left;">{{ format_number($Order->charge_cost * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                </tr>
            @endif


        
            <tr>
                <td class="label">الإجمالي:</td>
                <td style="text-align:left; font-weight:bold;">{{ format_number($Order->net_total * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
            </tr>
            
            @if($Order->payment_details)
                <br>
                <br>
                @php
                    $payments = json_decode($Order->payment_details, true);
                @endphp
                @foreach($payments as $payment)
                
                    <tr>
                        <td class="label">{{ $payment['payment_name'] }}</td>
                        <td style="text-align:left;">{{ format_number($payment['amount'] * Country()->currancy_value) }} {{ Country()->currancy_code_ar }}</td>
                    </tr>
                @endforeach
            @endif
            
            @if($Order->delivery_date)
                <tr>
                    <th class="label">{{ __("trans.date") }}</th>
                    <td style="text-align:left;">{{ $Order->delivery_date }}</td>
                </tr>
                <tr>
                    <th class="label">{{ __("trans.time") }}</th>
                    <td style="text-align:left;">{{ $Order->delivery_time_from }} - {{ $Order->delivery_time_to }}</td>
                </tr>
            @endif
        </table>


        <div class="footer">
            <p>شكرًا لتسوقكم معنا</p>
            <p>{{ setting('title_ar') }}</p>
        </div>
    </div>
</body>
</html>
