<!DOCTYPE html>
<html lang="ar" dir="rtl">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Print Invoice', __('Print Invoice'))) }} -
        {{ $invoice->invoice_number }}</title>
    <style>
        @page {
            margin: 0.5cm;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'DejaVu Sans', sans-serif;
            background-color: #ffffff;
            direction: rtl;
            text-align: right;
            padding: 5px;
            color: #000;
            line-height: 1.4;
        }

        .invoice-container {
            width: 100%;
            margin: 0 auto;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 10px;
            table-layout: fixed;
        }

        /* Header Style */
        .header-table {
            border-bottom: 2px solid #000;
            margin-bottom: 15px;
        }

        .header-table td {
            vertical-align: top;
            padding-bottom: 10px;
        }

        .company-info h1 {
            font-size: 14px;
            font-weight: bold;
            margin-bottom: 4px;
        }

        .company-info p {
            font-size: 9px;
            margin-bottom: 2px;
        }

        .qr-code-container {
            width: 120px;
            text-align: left;
        }

        .qr-code-container img, .qr-code-container svg {
            width: 100px;
            height: 100px;
        }

        .qr-code-label {
            font-size: 7px;
            margin-top: 2px;
            font-weight: bold;
            text-align: center;
            width: 100px;
        }

        .company-logo-container {
            width: 120px;
            text-align: right;
        }

        .company-logo {
            width: 110px;
            height: 110px;
            border: 1px solid #000;
            padding: 2px;
            text-align: center;
            background-color: #fff;
            display: block;
        }

        .company-logo img {
            max-width: 100px;
            max-height: 100px;
        }

        /* Invoice Meta Details */
        .details-table {
            margin-bottom: 15px;
        }

        .details-table td {
            padding: 6px;
            border: 1px solid #000;
            width: 25%;
            vertical-align: top;
            text-align: right;
        }

        .detail-label {
            font-size: 8px;
            font-weight: bold;
            display: block;
            margin-bottom: 2px;
            color: #000;
        }

        .detail-value {
            font-size: 9px;
            font-weight: normal;
        }

        /* Customer Info Box */
        .customer-info-box {
            background-color: #eff8ff;
            border: 1px solid #000;
            margin-bottom: 15px;
        }

        .customer-info-box td {
            padding: 8px;
            border: 1px solid #000;
            vertical-align: top;
            text-align: right;
        }

        /* Items Table */
        .items-table th {
            background-color: #000;
            color: #ffffff;
            font-size: 9px;
            padding: 6px 4px;
            text-align: center;
            border: 1px solid #000;
        }

        .items-table td {
            font-size: 9px;
            padding: 6px 4px;
            border: 1px solid #000;
            text-align: center;
            color: #000;
        }

        .items-table td.text-right {
            text-align: right;
        }

        /* Totals */
        .totals-section {
            width: 100%;
            margin-top: 10px;
        }

        .totals-table {
            width: 250px;
            float: left;
            border: 2px solid #000;
        }

        .totals-table td {
            padding: 5px 8px;
            font-size: 9px;
            border-bottom: 1px solid #000;
            color: #000;
        }

        .totals-table td:first-child {
            text-align: right;
            font-weight: bold;
        }

        .totals-table td:last-child {
            text-align: left;
            font-weight: bold;
        }

        .total-row-grand {
            font-weight: bold;
            font-size: 11px !important;
            background-color: #f8f9fa;
            border-top: 2px solid #000 !important;
        }

        .status-badge {
            display: inline-block;
            padding: 2px 5px;
            border-radius: 2px;
            font-size: 9px;
            color: #fff;
            font-weight: bold;
        }

        .status-draft { background-color: #f0ad4e; }
        .status-sent { background-color: #5bc0de; }
        .status-paid { background-color: #5cb85c; }
        .status-partially_paid { background-color: #f0ad4e; }
        .status-overdue { background-color: #d9534f; }
        .status-cancelled { background-color: #000; }

        .payments-table th {
            background-color: #f8f9fa;
            color: #000;
            font-size: 8px;
            padding: 4px;
            border: 1px solid #000;
        }

        .payments-table td {
            font-size: 8px;
            padding: 4px;
            border: 1px solid #000;
            text-align: center;
        }

        .footer {
            margin-top: 20px;
            padding-top: 5px;
            border-top: 1px solid #000;
            text-align: center;
            font-size: 8px;
            clear: both;
        }
    </style>
</head>

<body>
    <div class="invoice-container">
        <!-- Header: [Logo(Right) | Info(Center) | QR(Left)] in RTL -->
        <table class="header-table">
            <tr>
                <!-- Right Side: Logo -->
                <td class="company-logo-container" style="width: 30%;">
                    @if ($logoBase64)
                        <div class="company-logo">
                            <img src="data:{{ $logoMimeType }};base64,{{ $logoBase64 }}" alt="Logo">
                        </div>
                    @elseif ($invoice->company->logo)
                        <div class="company-logo">
                            <img src="{{ public_path('storage/' . $invoice->company->logo) }}" alt="Logo">
                        </div>
                    @else
                        <div class="company-logo" style="line-height: 110px; font-weight: bold;">
                            Logo
                        </div>
                    @endif
                </td>

                <!-- Center: Company Info -->
                <td class="company-info" style="text-align: center; width: 40%; vertical-align: middle;">
                    <h1>{{ $p($invoice->company->name) }}</h1>
                    <p><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number'))) }}:</strong>
                        {{ $invoice->company->tax_number }}</p>
                    <p><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Commercial Register', __('Commercial Register'))) }}:</strong>
                        {{ $invoice->company->commercial_record }}</p>
                    <p><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Address', __('Address'))) }}:</strong>
                        {{ $p($invoice->company->address) }}</p>
                    <p><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Phone', __('Phone'))) }}:</strong>
                        {{ $invoice->company->phone }}</p>
                    <p><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Email', __('Email'))) }}:</strong>
                        {{ $invoice->company->email }}</p>
                </td>

                <!-- Left Side: QR Code -->
                <td class="qr-code-container" style="width: 30%; text-align: left;">
                    @if ($qrCode)
                        @php
                            $mime = (isset($qrType) && $qrType == 'svg') ? 'image/svg+xml' : 'image/png';
                        @endphp
                        <img src="data:{{ $mime }};base64,{{ $qrCode }}" style="width: 100px; height: 100px;" alt="QR Code">
                        <div class="qr-code-label" style="text-align: left; width: 100px;">
                            <span style="color: green;">✔</span>
                            {{ $p(\App\Helpers\TranslationHelper::transFromDatabase('ZATCA Compliant', __('ZATCA Compliant'))) }}
                            @if ($invoice->total_amount < 1000 && empty($invoice->customer->tax_number))
                                <br>{{ $p('فاتورة مبسطة') }}
                            @else
                                <br>{{ $p('فاتورة ضريبية') }}
                            @endif
                        </div>
                    @endif
                </td>
            </tr>
        </table>

        <!-- Invoice Meta Details (4 columns) -->
        <table class="details-table">
            <tr>
                <td>
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number'))) }}</span>
                    <span class="detail-value">{{ $invoice->invoice_number }}</span>
                </td>
                <td>
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Invoice Date', __('Invoice Date'))) }}</span>
                    <span class="detail-value">
                        @if ($invoice->invoice_date_time)
                            {{ $invoice->invoice_date_time->format('Y-m-d H:i:s') }}
                        @else
                            {{ $invoice->invoice_date->format('Y-m-d H:i:s') }}
                        @endif
                    </span>
                </td>
                <td>
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date'))) }}</span>
                    <span class="detail-value">{{ $invoice->due_date ? $invoice->due_date->format('Y-m-d') : $p(\App\Helpers\TranslationHelper::transFromDatabase('Not Set', __('Not Set'))) }}</span>
                </td>
                <td>
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status'))) }}</span>
                    <span class="detail-value">
                        <span class="status-badge status-{{ $invoice->status }}">
                            @php
                                $statusTrans = '';
                                switch($invoice->status) {
                                    case 'draft': $statusTrans = 'Draft'; break;
                                    case 'sent': $statusTrans = 'Sent'; break;
                                    case 'paid': $statusTrans = 'Paid'; break;
                                    case 'partially_paid': $statusTrans = 'Partially Paid'; break;
                                    case 'overdue': $statusTrans = 'Overdue'; break;
                                    case 'cancelled': $statusTrans = 'Cancelled'; break;
                                    default: $statusTrans = ucfirst($invoice->status);
                                }
                            @endphp
                            {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($statusTrans, __($statusTrans))) }}
                        </span>
                    </span>
                </td>
            </tr>
        </table>

        <!-- Customer Information -->
        <table class="customer-info-box">
            <tr>
                <td style="width: 50%;">
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Customer Information', __('Customer Information'))) }}</span>
                    <p style="font-size: 10px; font-weight: bold; margin-bottom: 2px;">
                        <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Customer Name', __('Customer Name'))) }}:</strong> {{ $p($invoice->customer_name) }}
                    </p>
                    <p style="font-size: 9px;">{{ $invoice->customer->email }}</p>
                    @if (!empty($invoice->customer->tax_number))
                        <p style="font-size: 9px;"><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number'))) }}:</strong> {{ $invoice->customer->tax_number }}</p>
                    @endif
                    @if (!empty($invoice->customer->address))
                        <p style="font-size: 9px;"><strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Address', __('Address'))) }}:</strong> {{ $p($invoice->customer->address) }}</p>
                    @endif
                </td>
                <td style="width: 25%;">
                    <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Contact Number', __('Contact Number'))) }}</span>
                    <span class="detail-value">{{ $invoice->customer->phone }}</span>
                </td>
                <td style="width: 25%;">
                    @if ($invoice->customer_contract_id)
                        <span class="detail-label">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Contract Title', __('Contract Title'))) }}</span>
                        <span class="detail-value">{{ $invoice->contract->contract_number }}</span>
                    @endif
                </td>
            </tr>
        </table>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th style="width: 3%;">#</th>
                    <th style="width: 20%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Item Name', __('Item Name'))) }}</th>
                    <th style="width: 10%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Item Unit', __('Item Unit'))) }}</th>
                    <th style="width: 20%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Description', __('Description'))) }}</th>
                    <th style="width: 8%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Quantity', __('Quantity'))) }}</th>
                    <th style="width: 10%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Unit Price', __('Unit Price'))) }}</th>
                    <th style="width: 8%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Discount %', __('Discount %'))) }}</th>
                    <th style="width: 8%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Tax %', __('Tax %'))) }}</th>
                    <th style="width: 8%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('WHT %', __('خصم/إضافة %'))) }}</th>
                    <th style="width: 11%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Line Total', __('Line Total'))) }}</th>
                </tr>
            </thead>
            <tbody>
                @forelse($items as $index => $item)
                    <tr>
                        <td>{{ $index + 1 }}</td>
                        <td class="text-right">{{ $p($item->item->name ?? '-') }}</td>
                        <td class="text-right">{{ $item->itemUnit && $item->itemUnit->unit ? $p($item->itemUnit->unit->name) : '-' }}</td>
                        <td class="text-right">{{ $p($item->description) }}</td>
                        <td>{{ number_format($item->quantity, 2) }}</td>
                        <td>{{ number_format($item->unit_price, 2) }}</td>
                        <td>{{ number_format($item->discount_percentage ?? 0, 2) }}%</td>
                        <td>{{ number_format($item->tax_rate + ($item->tax_rate_2 ?? 0), 2) }}%</td>
                        <td>{{ number_format($item->withholding_tax_rate, 2) }}%</td>
                        <td style="font-weight: bold;">{{ number_format($item->total ?? 0, 2) }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="9">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('No Items In This Invoice', __('No Items In This Invoice'))) }}</td>
                    </tr>
                @endforelse
            </tbody>
        </table>

        <!-- Totals Section -->
        <div class="totals-section">
            <table class="totals-table">
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Subtotal', __('Subtotal'))) }}:</td>
                    <td>{{ number_format($invoice->subtotal, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Discount', __('Total Discount'))) }}:</td>
                    <td>{{ number_format($invoice->discount_amount, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Tax', __('Total Tax'))) }} ({{ $invoice->subtotal > 0 ? number_format(($invoice->tax_amount / $invoice->subtotal) * 100, 0) : 0 }}%):</td>
                    <td>{{ number_format($invoice->tax_amount, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total WHT', __('إجمالي الخصم والإضافة'))) }}:</td>
                    <td>{{ number_format($invoice->total_withholding_tax, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Amount', __('Total Amount'))) }}:</td>
                    <td>{{ number_format($invoice->total_amount, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
                <tr class="total-row-grand">
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Net Payable', __('صافي المستحق'))) }}:</td>
                    <td>{{ number_format($invoice->net_payable, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                </tr>
            </table>
        </div>

        @if (count($payments) > 0)
            <div style="margin-top: 20px; clear: both;">
                <h3 style="font-size: 10px; margin-bottom: 5px;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Payment Record', __('Payment Record'))) }}</h3>
                <table class="payments-table">
                    <thead>
                        <tr>
                            <th>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Payment Date', __('Payment Date'))) }}</th>
                            <th>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Payment Amount', __('Payment Amount'))) }}</th>
                            <th>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Payment Method', __('Payment Method'))) }}</th>
                            <th>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status'))) }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($payments as $payment)
                            <tr>
                                <td>{{ $payment->created_at->format('Y-m-d') }}</td>
                                <td>{{ number_format($payment->amount, 2) }} {{ $p(\App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR'))) }}</td>
                                <td>{{ $p($payment->payment_method) }}</td>
                                <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Paid', __('Paid'))) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif

        @if ($invoice->notes)
            <div style="margin-top: 15px;">
                <h3 style="font-size: 10px; margin-bottom: 5px;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Notes', __('Notes'))) }}</h3>
                <div style="border: 1px solid #000; padding: 6px; font-size: 9px; min-height: 40px;">
                    {{ $p($invoice->notes) }}
                </div>
            </div>
        @endif

        <div class="footer">
            {{ $p($invoice->company->name) }} - {{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number'))) }}: {{ $invoice->company->tax_number }}
            <br>
            {{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Generated on', __('Generated on'))) }}: {{ now()->format('Y-m-d H:i:s') }}
        </div>
    </div>
</body>

</html>
