<!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: 0cm 0cm;
        }

        body {
            font-family: 'DejaVu Sans', sans-serif;
            background-color: #ffffff;
            padding: 30px;
            direction: rtl;
            text-align: right;
            font-size: 10px;
        }

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

        .invoice-header {
            width: 100%;
            border-bottom: 2px solid #000;
            padding-bottom: 10px;
            margin-bottom: 20px;
        }

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

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

        .company-info p {
            margin: 2px 0;
            line-height: 1.4;
        }

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

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

        .invoice-details {
            width: 100%;
            margin-bottom: 20px;
            border: 1px solid #000;
            border-radius: 5px;
        }

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

        .details-table td {
            padding: 8px;
            border-bottom: 1px solid #eee;
        }

        .details-table td:last-child {
            border-bottom: none;
        }

        .label {
            font-weight: bold;
            color: #555;
            width: 150px;
        }

        .value {
            font-weight: bold;
            color: #000;
        }

        .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }

        .items-table th {
            background-color: #000;
            color: #fff;
            padding: 8px;
            text-align: center;
            font-weight: bold;
            border: 1px solid #000;
        }

        .items-table td {
            padding: 8px;
            border: 1px solid #000;
            text-align: center;
        }

        .totals-section {
            width: 100%;
            margin-top: 20px;
        }

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

        .totals-table td {
            padding: 8px;
            border-bottom: 1px solid #000;
        }

        .totals-table td:last-child {
            border-bottom: none;
        }

        .grand-total {
            background-color: #f8f9fa;
            font-weight: bold;
            font-size: 12px;
        }

        .footer {
            margin-top: 50px;
            padding-top: 10px;
            border-top: 1px solid #000;
            text-align: center;
            font-size: 8px;
            color: #777;
            clear: both;
        }

        .status-badge {
            padding: 3px 8px;
            border-radius: 3px;
            color: #fff;
            font-size: 9px;
            font-weight: bold;
        }

        .status-sent { background-color: #5bc0de; }
        .status-paid { background-color: #5cb85c; }
        .status-draft { background-color: #777; }
        .status-overdue { background-color: #d9534f; }
    </style>
</head>

<body>
    <div class="invoice-container">
        <div class="invoice-header">
            <table class="header-table">
                <tr>
                    <td class="company-info">
                        <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>
                    </td>
                    <td class="qr-code-container">
                        @if($qrCode)
                            <img src="data:image/png;base64,{{ $qrCode }}" alt="QR Code">
                        @endif
                    </td>
                </tr>
            </table>
        </div>

        <table class="details-table" style="margin-bottom: 20px; border: 1px solid #000;">
            <tr>
                <td style="width: 50%;">
                    <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Invoice Number', __('Invoice Number'))) }}:</strong> {{ $invoice->invoice_number }}
                </td>
                <td style="width: 50%;">
                    <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Invoice Date', __('Invoice Date'))) }}:</strong> {{ $invoice->invoice_date->format('Y-m-d') }} {{ $invoice->invoice_date_time ? $invoice->invoice_date_time->format('H:i:s') : '' }}
                </td>
            </tr>
            <tr>
                <td>
                    <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Customer Name', __('Customer Name'))) }}:</strong> {{ $p($invoice->customer_name) }}
                </td>
                <td>
                    <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Due Date', __('Due Date'))) }}:</strong> {{ $invoice->due_date->format('Y-m-d') }}
                </td>
            </tr>
            <tr>
                <td>
                    <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Status', __('Status'))) }}:</strong> 
                    <span class="status-badge status-{{ $invoice->status }}">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase(ucfirst($invoice->status), __($invoice->status))) }}</span>
                </td>
                <td>
                    @if($invoice->customer_contract_id)
                        <strong>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Contract Title', __('Contract Title'))) }}:</strong> {{ $invoice->contract->contract_number }}
                    @endif
                </td>
            </tr>
        </table>

        <table class="items-table">
            <thead>
                <tr>
                    <th style="width: 5%;">#</th>
                    <th style="width: 30%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Item Name', __('Item Name'))) }}</th>
                    <th style="width: 10%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Quantity', __('Quantity'))) }}</th>
                    <th style="width: 15%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Unit Price', __('Unit Price'))) }}</th>
                    <th style="width: 10%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Discount %', __('Discount %'))) }}</th>
                    <th style="width: 10%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Tax %', __('Tax %'))) }}</th>
                    <th style="width: 20%;">{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Line Total', __('Line Total'))) }}</th>
                </tr>
            </thead>
            <tbody>
                @foreach($items as $index => $item)
                    <tr>
                        <td>{{ $index + 1 }}</td>
                        <td>{{ $p($item->item->name ?? $item->description) }}</td>
                        <td>{{ number_format($item->quantity, 2) }}</td>
                        <td>{{ number_format($item->unit_price, 2) }}</td>
                        <td>{{ number_format($item->discount_amount, 2) }}%</td>
                        <td>{{ number_format($item->tax_rate + ($item->tax_rate_2 ?? 0), 2) }}%</td>
                        <td>{{ number_format($item->total, 2) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>

        <div class="totals-section">
            <table class="totals-table">
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Subtotal', __('Subtotal'))) }}:</td>
                    <td style="text-align: left;">{{ number_format($invoice->subtotal, 2) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Discount', __('Total Discount'))) }}:</td>
                    <td style="text-align: left;">{{ number_format($invoice->discount_amount, 2) }}</td>
                </tr>
                <tr>
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Tax', __('Total Tax'))) }}:</td>
                    <td style="text-align: left;">{{ number_format($invoice->tax_amount, 2) }}</td>
                </tr>
                <tr class="grand-total">
                    <td>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Total Amount', __('Total Amount'))) }}:</td>
                    <td style="text-align: left;">{{ number_format($invoice->total_amount, 2) }}</td>
                </tr>
            </table>
        </div>

        <div class="footer">
            <p>{{ $p(\App\Helpers\TranslationHelper::transFromDatabase('Generated automatically by the system', __('Generated automatically by the system'))) }} - {{ date('Y-m-d H:i:s') }}</p>
        </div>
    </div>
</body>

</html>
