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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{ \App\Helpers\TranslationHelper::transFromDatabase('Print Invoice', __('Print Invoice')) }} -
        {{ $invoice->invoice_number }}</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Arial', 'Segoe UI', sans-serif;
            background-color: #ffffff;
            padding: 20px;
            direction: rtl;
        }

        .invoice-container {
            background-color: white;
            padding: 20px;
            margin: 0 auto;
            max-width: 900px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            display: flex;
            flex-direction: column;
            min-height: 100vh;
        }

        .invoice-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 2px solid #000;
            padding-bottom: 12px;
            margin-bottom: 12px;
            gap: 15px;
        }

        .company-info {
            flex: 1;
        }

        .company-logo {
            width: 130px;
            height: 130px;
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: #ffffff;
            border: 1px solid #000;
            border-radius: 5px;
            flex-shrink: 0;
            margin: 0 auto;
        }

        .company-logo img {
            max-width: 95%;
            max-height: 95%;
            object-fit: contain;
        }

        .qr-code-container {
            flex: 1;
            display: flex;
            flex-direction: column;
            align-items: flex-end;
            text-align: center;
        }

        .qr-code-container img {
            max-width: 120px;
            height: auto;
        }

        .qr-code-container svg {
            max-width: 120px !important;
            height: 120px !important;
            width: 120px !important;
        }

        .qr-code-label {
            font-size: 7px;
            color: #000;
            margin-top: 2px;
            font-weight: 900;
        }

        .invoice-details {
            display: grid;
            grid-template-columns: repeat(5, 1fr);
            gap: 12px;
            margin-bottom: 12px;
            padding: 10px;
            background-color: #ffffff;
            border: 1px solid #000;
            border-radius: 3px;
        }

        .detail-item {
            display: flex;
            flex-direction: column;
        }

        .detail-label {
            font-weight: 900;
            font-size: 8px;
            color: #000;
            margin-bottom: 2px;
            text-transform: uppercase;
        }

        .detail-value {
            font-size: 9px;
            font-weight: 600;
            color: #000;
            line-height: 1.2;
        }

        .simplified-invoice-badge {
            font-size: 8px;
            color: #d9534f;
            font-weight: bold;
            margin-top: 2px;
            padding: 2px;
            background-color: #ffe6e6;
            border-radius: 2px;
            border-left: 2px solid #d9534f;
        }

        .invoice-items-section {
            flex: 1;
            display: flex;
            flex-direction: column;
            overflow: hidden;
            margin-bottom: 12px;
        }

        .invoice-items {
            flex: 1;
            overflow-y: auto;
        }

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

        thead {
            background-color: #000;
            color: white;
        }

        thead th {
            padding: 8px;
            text-align: right;
            font-size: 9px;
            font-weight: 900;
            border: 1px solid #000;
            color: white;
            background-color: #000 !important;
        }

        tbody td {
            padding: 7px 8px;
            font-size: 9px;
            font-weight: 600;
            border: 1px solid #000;
            text-align: right;
            color: #000;
        }

        tbody tr:nth-child(even) {
            background-color: #ffffff;
        }

        tbody tr:hover {
            background-color: #e8e8e8;
        }

        .totals-section {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 12px;
        }

        .totals-box {
            width: 280px;
            background-color: #ffffff;
            padding: 10px;
            border: 2px solid #000;
            border-radius: 3px;
        }

        .total-row {
            display: flex;
            justify-content: space-between;
            padding: 6px 0;
            font-size: 9px;
            font-weight: 600;
            border-bottom: 1px solid #000;
            color: #000;
        }

        .total-row:last-child {
            border-bottom: none;
        }

        .total-row.grand-total {
            font-weight: 900;
            font-size: 11px;
            color: #000;
            padding: 8px 0;
            border-top: 2px solid #000;
            margin-top: 5px;
        }

        .payments-section {
            margin-bottom: 12px;
        }

        .payments-section h3 {
            font-size: 10px;
            font-weight: 900;
            margin-bottom: 6px;
            color: #000;
        }

        .payments-table table {
            margin: 0;
        }

        .status-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 3px;
            font-size: 11px;
            font-weight: 900;
        }

        .status-draft {
            background-color: #f0ad4e;
            color: white;
        }

        .status-sent {
            background-color: #5bc0de;
            color: white;
        }

        .status-paid {
            background-color: #5cb85c;
            color: white;
        }

        .status-partially-paid {
            background-color: #f0ad4e;
            color: white;
        }

        .status-overdue {
            background-color: #d9534f;
            color: white;
        }

        .status-cancelled {
            background-color: #000;
            color: white;
        }

        .notes-section {
            margin-bottom: 12px;
        }

        .notes-section h3 {
            font-size: 10px;
            font-weight: 900;
            margin-bottom: 5px;
            color: #000;
        }

        .notes-content {
            padding: 8px;
            background-color: #ffffff;
            border: 1px solid #000;
            border-radius: 3px;
            font-size: 9px;
            font-weight: 600;
            color: #000;
            max-height: 50px;
            overflow: hidden;
        }

        .footer {
            text-align: center;
            padding-top: 8px;
            border-top: 1px solid #000;
            font-size: 8px;
            font-weight: 600;
            color: #000;
        }

        .print-time {
            display: none;
        }

        .customer-address {
            font-size: 9px;
            font-weight: 600;
            color: #000;
            margin-top: 3px;
            line-height: 1.3;
        }

        .print-button-area {
            text-align: center;
            margin-top: 20px;
            display: none;
        }

        .print-button {
            padding: 10px 20px;
            background-color: #000;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 13px;
        }

        .print-button:hover {
            background-color: #222;
        }

        @page {
            margin: 0;
            padding: 0;
        }

        @media print {
            body {
                background-color: white;
                padding: 0;
                margin: 0;
            }

            .invoice-container {
                box-shadow: none;
                padding: 20px;
            }

            .print-button-area {
                display: none !important;
            }

            * {
                -webkit-print-color-adjust: exact !important;
                print-color-adjust: exact !important;
                color-adjust: exact !important;
            }

            thead th {
                background-color: #000 !important;
                color: white !important;
                font-weight: 900 !important;
            }

            tbody td {
                color: #000 !important;
                font-weight: 600 !important;
            }

            .detail-value {
                color: #000 !important;
                font-weight: 600 !important;
            }

            .detail-label {
                color: #000 !important;
                font-weight: 900 !important;
            }

            .total-row {
                color: #000 !important;
                font-weight: 600 !important;
            }

            .notes-content {
                border: 1px solid #000;
            }

            .company-logo {
                background-color: white;
                border: 1px solid #000;
            }

            .company-logo img {
                max-width: 95%;
                max-height: 95%;
            }
        }

        @media (max-width: 768px) {
            .invoice-details {
                grid-template-columns: 1fr 1fr;
            }

            .totals-box {
                width: 100%;
            }
        }
    </style>
</head>

<body>
    <div class="invoice-container">
        <div class="invoice-header">
            <div class="company-info">
                <h1>{{ $invoice->company->name }}</h1>
                <p><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number')) }}:</strong>
                    {{ $invoice->company->tax_number }}</p>
                <p><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Commercial Register', __('Commercial Register')) }}:</strong>
                    {{ $invoice->company->commercial_record }}</p>
                <p><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Address', __('Address')) }}:</strong>
                    {{ $invoice->company->address }}</p>
                <p><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Phone', __('Phone')) }}:</strong>
                    {{ $invoice->company->phone }}</p>
                <p><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Email', __('Email')) }}:</strong>
                    {{ $invoice->company->email }}</p>
            </div>

            {{-- عرض لوجو الشركة إن وجد --}}
            @if ($invoice->company->logo)
                <div class="company-logo">
                    <img src="{{ asset('storage/' . $invoice->company->logo) }}" alt="{{ $invoice->company->name }}"
                        onerror="this.style.display='none'">
                </div>
            @else
                <div style="flex: 1;"></div> {{-- Placeholder if no logo to maintain centering --}}
            @endif

            <div class="qr-code-container">
                @if (isset($qrCode) && !empty($qrCode) && $compliance['valid'])
                    {!! $qrCode !!}
                    <div class="qr-code-label" style="color: green; font-weight: bold;">
                        ✅
                        {{ \App\Helpers\TranslationHelper::transFromDatabase('ZATCA Compliant', __('ZATCA Compliant')) }}
                        @if ($invoice->total_amount < 1000 && empty($invoice->customer->tax_number))
                            <br><small style="color: #000; font-weight: bold;">📋 فاتورة مبسطة</small>
                        @else
                            <br><small style="color: #000; font-weight: bold;">📋 فاتورة ضريبية</small>
                        @endif
                    </div>
                @elseif(!empty($compliance['errors']))
                    <div
                        style="padding: 15px; background-color: #ffe6e6; border: 2px solid #d9534f; border-radius: 5px; text-align: center;">
                        <p style="color: #d9534f; font-weight: bold;">❌ بيانات ناقصة:</p>
                        @foreach ($compliance['errors'] as $error)
                            <p style="color: #d9534f; font-size: 10px;">• {{ $error }}</p>
                        @endforeach
                    </div>
                @else
                    <div style="padding: 10px; background-color: #ffffff; border: 2px solid #000; border-radius: 5px;">
                        <p style="color: #000; font-size: 10px; font-weight: bold;">
                            {{ \App\Helpers\TranslationHelper::transFromDatabase('QR Code', __('QR Code')) }}</p>
                    </div>
                @endif
            </div>

        </div>

        <div class="invoice-details">
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number')) }}</span>
                <span class="detail-value">{{ $invoice->invoice_number }}</span>
            </div>
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Reference Number', __('الرقم المرجعي')) }}</span>
                <span class="detail-value">{{ $invoice->reference_number ?? '-' }}</span>
            </div>
            <div class="detail-item">
                <span
                    class="detail-label">{{ \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>
            </div>
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date')) }}</span>
                <span
                    class="detail-value">{{ $invoice->due_date ? $invoice->due_date->format('Y-m-d') : \App\Helpers\TranslationHelper::transFromDatabase('Not Set', __('Not Set')) }}</span>
            </div>
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status')) }}</span>
                <span class="detail-value">
                    <span class="status-badge status-{{ $invoice->status }}">
                        @switch($invoice->status)
                            @case('draft')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Draft', __('Draft')) }}
                            @break

                            @case('sent')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Sent', __('Sent')) }}
                            @break

                            @case('paid')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Paid', __('Paid')) }}
                            @break

                            @case('partially_paid')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Partially Paid', __('Partially Paid')) }}
                            @break

                            @case('overdue')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Overdue', __('Overdue')) }}
                            @break

                            @case('cancelled')
                                {{ \App\Helpers\TranslationHelper::transFromDatabase('Cancelled', __('Cancelled')) }}
                            @break
                        @endswitch
                    </span>
                </span>
            </div>
        </div>

        <div class="invoice-details" style="background-color: #eff8ff; margin-bottom: 20px;">
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Customer Information', __('Customer Information')) }}</span>
                <span class="detail-value" style="color: #000;"><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Customer Name', __('Customer Name')) }}:</strong> {{ $invoice->customer_name }}</span>
                @if($invoice->diverse_customer_name)
                <span class="detail-value" style="color: #000;"><strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Diverse Customer Name', __('اسم العميل المتنوع')) }}:</strong> {{ $invoice->diverse_customer_name }}</span>
                @endif
                <span class="detail-value"
                    style="font-size: 10px; margin-top: 3px; color: #000;">{{ $invoice->customer->email }}</span>

                @if (!empty($invoice->customer->tax_number))
                    <span class="detail-value" style="font-size: 10px; margin-top: 3px; color: #000; font-weight: bold;">
                        <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Tax Number', __('Tax Number')) }}:</strong>
                        {{ $invoice->customer->tax_number }}
                    </span>
                @endif

                @if (!empty($invoice->customer->address))
                    <span class="customer-address">
                        <strong>{{ \App\Helpers\TranslationHelper::transFromDatabase('Address', __('Address')) }}:</strong>
                        {{ $invoice->customer->address }}
                    </span>
                @endif
            </div>
            <div class="detail-item">
                <span
                    class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Contact Number', __('Contact Number')) }}</span>
                <span class="detail-value">{{ $invoice->customer->phone }}</span>
            </div>
            @if ($invoice->customer_contract_id)
                <div class="detail-item">
                    <span
                        class="detail-label">{{ \App\Helpers\TranslationHelper::transFromDatabase('Contract Title', __('Contract Title')) }}</span>
                    <span class="detail-value">{{ $invoice->contract->contract_number }}</span>
                </div>
            @endif
        </div>

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

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

        @if (count($payments) > 0)
            <div class="payments-section">
                <h3>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Record', __('Payment Record')) }}
                </h3>
                <table>
                    <thead>
                        <tr>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Date', __('Payment Date')) }}
                            </th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Amount', __('Payment Amount')) }}
                            </th>
                            <th>{{ \App\Helpers\TranslationHelper::transFromDatabase('Payment Method', __('Payment Method')) }}
                            </th>
                            <th>{{ \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 style="text-align: center;">{{ number_format($payment->amount, 2) }}
                                    {{ \App\Helpers\TranslationHelper::transFromDatabase($invoice->company->currency ?? 'SAR', $invoice->company->currency ?? __('SAR')) }}</td>
                                <td>{{ $payment->payment_method }}</td>
                                <td style="text-align: center;">
                                    <span
                                        class="status-badge status-paid">{{ \App\Helpers\TranslationHelper::transFromDatabase('Paid', __('Paid')) }}</span>
                                </td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        @endif

        @if ($invoice->notes)
            <div class="notes-section">
                <h3>{{ \App\Helpers\TranslationHelper::transFromDatabase('Notes', __('Notes')) }}</h3>
                <div class="notes-content">
                    {{ $invoice->notes }}
                </div>
            </div>
        @endif

        <div class="footer">
            <p>{{ \App\Helpers\TranslationHelper::transFromDatabase('This is a computer-generated invoice. No signature required.', __('This is a computer-generated invoice. No signature required.')) }}</p>
            <p class="print-time">{{ now()->format('Y-m-d H:i:s') }}</p>
        </div>
    </div>

    <script>
        window.addEventListener('load', function() {
            window.print();
        });
    </script>
</body>

</html>
