<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ticket Invoice</title>
    <style>
        @page {
            size: A2 portrait;
            margin: 2cm;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 2cm;
            box-sizing: border-box;
            background: #fff;
            color: #000;
            width: 100%;
        }

        .container {
            max-width: 100%;
            page-break-inside: avoid;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 40px;
        }

        .logo {
            max-height: 100px;
        }

        h1 {
            font-size: 32px;
            margin: 0;
        }

        h3.section-title {
            background-color: #f2f2f2;
            padding: 7px;
            font-size: 15px;
            margin-top: 10px;
            border-left: 5px solid #333;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 10px;
        }

        th, td {
            border: 1px solid #333;
            padding: 5px;
            font-size: 14px;
        }

        th {
            background-color: #e0e0e0;
            text-align: left;
        }

        .print-button {
            position: fixed;
            top: 20px;
            right: 40px;
            padding: 12px 24px;
            font-size: 16px;
            background-color: #4CAF50;
            color: #fff;
            border: none;
            cursor: pointer;
            z-index: 1000;
        }

        @media print {
            .print-button {
                display: none;
            }

            body {
                padding: 0;
                margin: 0;
            }

            .container {
                padding: 0;
                margin: 0;
            }
            hr {
                display: none;
            }
        }

        .break_page {
            break-after: page;
        }


    </style>
<body>

<button class="print-button" onclick="window.print()">🖨️ طباعة</button>

<div class="container">
    <div class="header">
        <img src="<?php echo e(asset('logo.png')); ?>" alt="Logo" class="logo" width="100">
        <h1>تذكرة سفر / Ticket Invoice</h1>
    </div>

    <?php
        $companyKeys = [
            'company_name_en',
            'company_address_en',
            'tax_number',
            'commercial_register',
            'tourism_license',
        ];
        $company = \App\Models\Setting::whereIn('key', $companyKeys)->pluck('value', 'key');
    ?>

    <h3 class="section-title">Company Details</h3>
    <table>
        <tr>
            <th>Company Name</th>
            <td><?php echo e($company['company_name_en'] ?? ''); ?></td>
            <th>Address</th>
            <td><?php echo e($company['company_address_en'] ?? ''); ?></td>
        </tr>
        <tr>
            <th>Tax Number</th>
            <td><?php echo e($company['tax_number'] ?? ''); ?></td>
            <th>Commercial Register</th>
            <td><?php echo e($company['commercial_register'] ?? ''); ?></td>
        </tr>
        <tr>
            <th>Tourism License</th>
            <td colspan="3"><?php echo e($company['tourism_license'] ?? ''); ?></td>
        </tr>
    </table>

    <?php $__currentLoopData = $invoice->tickets; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $ticket): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
        <?php if(!$loop->first): ?>
            <hr>
        <?php endif; ?>

        <h3 class="section-title">Ticket Details</h3>
        <table>
            <tr>
                <th>Airline Name</th>
                <td><?php echo e($ticket->airline->name ?? $ticket->airline_name); ?> (<?php echo e($ticket->airline->iata_code ?? ''); ?>)</td>

                <th>Airline Code</th>
                <td><?php echo e($ticket->airline->iata_prefix ?? $ticket->validating_carrier_code ?? ''); ?></td>
            </tr>
            <tr>
                <th>Ticket Number</th>
                <td><?php echo e($ticket->ticket_number_full ?? ''); ?></td>

                <th>Issue Date</th>
                <td><?php echo e($ticket->issue_date ? $ticket->issue_date->format('Y-m-d') : ''); ?></td>
            </tr>
            <tr>
                <th>Itinerary</th>
                <td colspan="3">
                    <?php if($ticket->segments->count() > 0): ?>
                        <?php echo e($ticket->segments->pluck('origin.iata')->join(' → ')); ?>

                        →
                        <?php echo e($ticket->segments->last()->destination->iata); ?>

                    <?php else: ?>
                        <?php echo e($ticket->itinerary_string ?? 'N/A'); ?>

                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th>Beneficiary</th>
                <td><?php echo e($ticket->client->name ?? 'N/A'); ?></td>

                <th>PNR</th>
                <td><?php echo e($ticket->pnr ?? ''); ?></td>
            </tr>
        </table>

        <?php if($ticket->segments->count() > 0): ?>
            <?php $__currentLoopData = $ticket->segments; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $segment): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <h3 class="section-title">Flight Details</h3>
                <table>
                    <tr>
                        <th>Flight Number</th>
                        <td><?php echo e($segment->flight_number ?? ''); ?></td>
                        <th>Ticket Number</th>
                        <td><?php echo e($ticket->ticket_number_core ?? ''); ?></td>
                    </tr>
                    <tr>
                        <th>From</th>
                        <td><?php echo e($segment->origin->city ?? $segment->origin->iata ?? ''); ?></td>
                        <th>To</th>
                        <td><?php echo e($segment->destination->city ?? $segment->destination->iata ?? ''); ?></td>
                    </tr>
                    <tr>
                        <th>Departure Date</th>
                        <td><?php echo e($segment->departure_at ? $segment->departure_at->format('Y-m-d') : ''); ?></td>
                        <th>Departure Time</th>
                        <td><?php echo e($segment->departure_at ? $segment->departure_at->format('H:i') : ''); ?></td>
                    </tr>
                    <tr>
                        <th>Arrival Date</th>
                        <td><?php echo e($segment->arrival_at ? $segment->arrival_at->format('Y-m-d') : ''); ?></td>
                        <th>Arrival Time</th>
                        <td><?php echo e($segment->arrival_at ? $segment->arrival_at->format('H:i') : ''); ?></td>
                    </tr>
                    <tr>
                        <th>Flight Number</th>
                        <td><?php echo e($segment->flight_number ?? ''); ?></td>
                    </tr>
                </table>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <?php endif; ?>

        <h3 class="section-title">Passenger Details</h3>
        <?php if($ticket->passengers->count() > 0): ?>
            <?php $__currentLoopData = $ticket->passengers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $passenger): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <table>
                    <tr>
                        <th>Passenger Name</th>
                        <td><?php echo e(strtoupper(trim(($passenger->first_name ?? '') . ' ' . ($passenger->last_name ?? '')))); ?></td>
                        <th>Nationality</th>
                        <td><?php echo e($passenger->nationality ?? ''); ?></td>
                    </tr>
                    <tr>
                        <th>Passport Number</th>
                        <td><?php echo e($passenger->passport_number ?? ''); ?></td>
                        <th>Passport Expiry</th>
                        <td><?php echo e($passenger->passport_expiry_date ?? ''); ?></td>
                    </tr>
                    <tr>
                        <th>Date of Birth</th>
                        <td><?php echo e($passenger->date_of_birth ?? ''); ?></td>
                        <th>Phone</th>
                        <td><?php echo e($passenger->phone ?? ''); ?></td>
                    </tr>
                </table>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        <?php endif; ?>

        <h3 class="section-title">Price Details</h3>
        <table>
            <tr>
                <th>Base Price</th>
                <td><?php echo e(number_format($ticket->cost_base_amount ?? 0, 0)); ?> <?php echo e($ticket->currency->symbol ?? 'SAR'); ?></td>
                <th>Taxes</th>
                <td><?php echo e(number_format(($ticket->cost_tax_amount ?? 0) + ($ticket->extra_tax_amount ?? 0), 0)); ?> <?php echo e($ticket->currency->symbol ?? 'SAR'); ?></td>
                <th>Total Price</th>
                <td><?php echo e(number_format($ticket->sale_total_amount ?? 0, 2)); ?> <?php echo e($ticket->currency->symbol ?? 'SAR'); ?></td>
            </tr>
        </table>

        <h3 class="section-title">Baggage & Restrictions</h3>
        <table>
            <tr>
                <th>Baggage Allowance</th>
                <td>N/A</td>
            </tr>
            <tr>
                <th>Restrictions</th>
                <td>N/A</td>
            </tr>
        </table>

        <h3 class="section-title">Other Information</h3>
        <table>
            <tr>
                <th>Booking Date</th>
                <td><?php echo e($ticket->booking_date ? $ticket->booking_date->format('Y-m-d') : ($ticket->issue_date ? $ticket->issue_date->format('Y-m-d') : '')); ?></td>
                <th>Payment Method</th>
                <td>CASH</td>
                <th>Supplier</th>
                <td><?php echo e($ticket->supplier->name ?? 'N/A'); ?></td>
            </tr>
        </table>
        <div class="break_page"></div>
    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>

    <h3 class="section-title">Invoice Details</h3>
    <table>
        <tr>
            <th>Invoice Number</th>
            <td><?php echo e($invoice->invoice_number ?? ''); ?></td>
            <th>Invoice Date</th>
            <td><?php echo e($invoice->created_at ? $invoice->created_at->format('Y-m-d') : ''); ?></td>
            <th>Due Date</th>
            <td><?php echo e($invoice->due_date ? $invoice->due_date/*->format('Y-m-d')*/ : ''); ?></td>
        </tr>
        <tr>
            <th>Total Taxes</th>
            <td colspan="2"><?php echo e(number_format($invoice->total_taxes ?? 0, 2)); ?></td>
            <th>Total Amount</th>
            <td colspan="2"><?php echo e(number_format($invoice->total_amount ?? 0, 2)); ?></td>
        </tr>
    </table>

</div>

</body>
</html>
<?php /**PATH C:\abdPro\flyaramfilament\resources\views/invoices/invoice.blade.php ENDPATH**/ ?>