<?php
    $currencyPrecision = $layoutData['currencyPrecision'] ?? 2;
    $quantityPrecision = $layoutData['quantityPrecision'] ?? 2;
    $currencySymbol = $layoutData['currencySymbol'] ?? 'Rs. ';
    $headerType = $layoutData['headerType'] ?? 'standard';
    $showLetterhead = $layoutData['showLetterhead'] ?? false;
    $showFooterImage = $layoutData['showFooterImage'] ?? false;
    $timezone = $layoutData['timezone'] ?? 'Asia/Karachi';
    $showWatermark = $layoutData['showWatermark'] ?? false;
    $watermarkText = $layoutData['watermarkText'] ?? '';
    $watermarkType = $layoutData['watermarkType'] ?? 'text';
    $watermarkImgSrc = $layoutData['watermarkImgSrc'] ?? null;

    $businessLogoSrc = $layoutData['businessLogoSrc'] ?? null;
    $letterheadImgSrc = $layoutData['letterheadImgSrc'] ?? null;
    $footerImgSrc = $layoutData['footerImgSrc'] ?? null;
    $fbrLogoSrc = $layoutData['fbrLogoSrc'] ?? null;
    $qrData = $layoutData['qrData'] ?? '';

    // Columns
    $showHs = $layoutData['columns']['hs']['show'] ?? true;
    $labelHs = $layoutData['columns']['hs']['label'] ?? 'HS Code';

    $showQty = $layoutData['columns']['qty']['show'] ?? true;
    $labelQty = $layoutData['columns']['qty']['label'] ?? 'Qty';

    $showUom = $layoutData['columns']['uom']['show'] ?? true;
    $labelUom = $layoutData['columns']['uom']['label'] ?? 'UOM';

    $showRate = $layoutData['columns']['rate']['show'] ?? true;
    $labelRate = $layoutData['columns']['rate']['label'] ?? 'Rate';

    $showTaxable = $layoutData['columns']['taxable']['show'] ?? true;
    $labelTaxable = $layoutData['columns']['taxable']['label'] ?? 'Taxable Value';

    $showTax = $layoutData['columns']['tax']['show'] ?? true;
    $labelTax = $layoutData['columns']['tax']['label'] ?? 'Sales Tax';

    $showTotal = $layoutData['columns']['total']['show'] ?? true;
    $labelTotal = $layoutData['columns']['total']['label'] ?? 'Total Value';
    
    $layout = $layoutData['layout'] ?? 'navy';
?>

<div class="fbr-gov-invoice-wrapper <?php echo e(($showFooterImage && $footerImgSrc) ? 'has-footer-image' : ''); ?>">
    <style>
        .fbr-gov-invoice-wrapper {
            position: relative;
            max-width: 850px;
            margin: 0 auto;
            padding: 20px 30px;
            font-family: Arial, sans-serif;
            color: #000;
            background: #fff;
        }

        .fbr-gov-invoice-wrapper.has-footer-image {
            min-height: 1080px;
            padding-bottom: 160px;
        }

        .fbr-gov-invoice-wrapper * {
            box-sizing: border-box;
        }

        .fbr-gov-invoice-wrapper .watermark {
            position: absolute;
            top: 55%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-30deg);
            font-size: 80px;
            font-weight: bold;
            color: rgba(0, 0, 0, 0.03);
            z-index: 0;
            text-transform: uppercase;
            pointer-events: none;
        }

        .fbr-gov-invoice-wrapper .top-company-header {
            text-align: center;
            margin-bottom: 15px;
        }

        .fbr-gov-invoice-wrapper .company-name {
            font-size: 20px;
            font-weight: bold;
            text-transform: capitalize;
            margin: 0;
        }

        .fbr-gov-invoice-wrapper .company-address {
            font-size: 11px;
            margin: 3px 0;
        }

        .fbr-gov-invoice-wrapper .header-info-grid {
            display: grid;
            grid-template-columns: 1.2fr 0.8fr 1fr;
            gap: 15px;
            font-size: 11px;
            line-height: 1.4;
            margin-bottom: 15px;
        }

        .fbr-gov-invoice-wrapper .header-info-col {
            display: flex;
            flex-direction: column;
        }

        .fbr-gov-invoice-wrapper .invoice-type-title {
            font-size: 14px;
            font-weight: bold;
            text-align: center;
            margin-top: 10px;
            text-transform: uppercase;
            text-decoration: underline;
        }

        .fbr-gov-invoice-wrapper .grid-table-container {
            width: 100%;
            overflow-x: auto;
            margin-bottom: 15px;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 10px;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table th {
            border: 1px solid #000;
            background: #fff;
            padding: 6px 4px;
            font-weight: bold;
            text-align: center;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table td {
            border: 1px solid #000;
            padding: 6px 4px;
            text-align: center;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table th.left, 
        .fbr-gov-invoice-wrapper .fbr-grid-table td.left {
            text-align: left;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table th.right, 
        .fbr-gov-invoice-wrapper .fbr-grid-table td.right {
            text-align: right;
        }

        .fbr-gov-invoice-wrapper .fbr-grid-table .total-row td {
            font-weight: bold;
            border-top: 2px double #000;
        }

        .fbr-gov-invoice-wrapper .bottom-flex {
            display: grid;
            grid-template-columns: 1.3fr 0.7fr;
            gap: 20px;
            align-items: start;
            margin-top: 10px;
        }

        .fbr-gov-invoice-wrapper .bottom-left-panel {
            display: flex;
            flex-direction: column;
            gap: 15px;
            font-size: 11px;
        }

        .fbr-gov-invoice-wrapper .payment-method-row {
            font-weight: bold;
        }

        .fbr-gov-invoice-wrapper .logo-and-qr-box {
            display: flex;
            align-items: center;
            gap: 15px;
            margin-top: 10px;
        }

        .fbr-gov-invoice-wrapper .qr-img {
            width: 75px;
            height: 75px;
            display: block;
            border: 1px solid #000;
            padding: 2px;
        }

        .fbr-gov-invoice-wrapper .digital-logo-text {
            width: 75px;
            height: 75px;
            background: #1e3a8a;
            color: #fff;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            text-align: center;
            font-size: 9px;
            font-weight: bold;
            border-radius: 4px;
            line-height: 1.1;
        }

        .fbr-gov-invoice-wrapper .digital-logo-text span {
            font-size: 11px;
            letter-spacing: 0.5px;
        }

        .fbr-gov-invoice-wrapper .fbr-pk-logo {
            height: 75px;
            width: auto;
            object-fit: contain;
        }

        .fbr-gov-invoice-wrapper .footer-caption {
            font-size: 9.5px;
            font-weight: bold;
            color: #000;
            margin-top: 10px;
            text-transform: uppercase;
        }

        .fbr-gov-invoice-wrapper .footer-note-text {
            font-size: 9px;
            color: #444;
            font-style: italic;
        }

        .fbr-gov-invoice-wrapper .bottom-right-panel {
            width: 100%;
        }

        .fbr-gov-invoice-wrapper .totals-summary-table {
            width: 100%;
            border-collapse: collapse;
            font-size: 11px;
        }

        .fbr-gov-invoice-wrapper .totals-summary-table td {
            border: 1px solid #000;
            padding: 6px 8px;
        }

        .fbr-gov-invoice-wrapper .totals-summary-table td.val {
            text-align: right;
            font-weight: bold;
        }
    </style>

    <?php if($showWatermark): ?>
        <?php if($watermarkType === 'image' && $watermarkImgSrc): ?>
            <img src="<?php echo e($watermarkImgSrc); ?>" style="position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 280px; opacity: 0.05; pointer-events: none;" alt="Watermark" />
        <?php elseif($watermarkType === 'text' && $watermarkText): ?>
            <div class="watermark"><?php echo e($watermarkText); ?></div>
        <?php endif; ?>
    <?php endif; ?>

    <?php if($headerType === 'letterhead_image' && $letterheadImgSrc): ?>
        <div style="width: calc(100% + 60px); overflow: hidden; margin: -20px -30px 15px -30px;">
            <img src="<?php echo e($letterheadImgSrc); ?>" style="width: 100%; height: auto; display: block;" alt="Letterhead" />
        </div>
    <?php else: ?>
        <div class="top-company-header" style="display: flex; flex-direction: column; align-items: center; gap: 8px;">
            <?php if($businessLogoSrc): ?>
                <img src="<?php echo e($businessLogoSrc); ?>" style="max-height: 48px; width: auto; object-fit: contain;" alt="Logo" />
            <?php endif; ?>
            <div>
                <h1 class="company-name" style="margin: 0;"><?php echo e($invoice->business->business_name ?? $invoice->business->name); ?></h1>
                <p class="company-address" style="margin: 3px 0 0 0;"><?php echo e($invoice->business->address); ?></p>
            </div>
        </div>
    <?php endif; ?>

    <div class="header-info-grid">
        <div class="header-info-col">
            <div><b>Buyer's Name:</b> <?php echo e($invoice->customer->name ?? 'Walk-In Customer'); ?></div>
            <div><b>Buyer's Address:</b> <?php echo e($invoice->customer->address ?? '—'); ?></div>
            <div><b>NTN / CNIC #:</b> <?php echo e($invoice->customer->ntn_cnic ?? '—'); ?></div>
        </div>
        <div class="header-info-col" style="text-align: center;">
            <div><b>Phone:</b> <?php echo e($invoice->business->phone ?? '—'); ?></div>
            <div><b>NTN Number:</b> <?php echo e($invoice->business->ntn_cnic); ?></div>
            <div class="invoice-type-title"><?php echo e($layoutData['titleLabel'] ?? 'Sales Tax Invoice'); ?></div>
        </div>
        <div class="header-info-col" style="text-align: right;">
            <div><b>Invoice #:</b> <?php echo e($invoice->local_invoice_no); ?></div>
            <div><b>Date:</b> <?php echo e($layoutData['formatted']['invoice_date']); ?></div>
            <div><b>OGP#:</b> <?php echo e($invoice->id + 1000); ?></div>
        </div>
    </div>

    <div class="grid-table-container">
        <table class="fbr-grid-table">
            <thead>
                <tr>
                    <th style="width: 30px;">Sr #</th>
                    <th>Item SKU</th>
                    <th class="left">Item Name</th>
                    <?php if($showHs): ?><th><?php echo e($labelHs); ?></th><?php endif; ?>
                    <?php if($showUom): ?><th><?php echo e($labelUom); ?></th><?php endif; ?>
                    <?php if($showRate): ?><th class="right"><?php echo e($labelRate); ?></th><?php endif; ?>
                    <?php if($showQty): ?><th><?php echo e($labelQty); ?></th><?php endif; ?>
                    <th>Disc %</th>
                    <th>GST %</th>
                    <th>FED %</th>
                    <th>WH Tax%</th>
                    <th>F. Tax%</th>
                    <?php if($showTaxable): ?><th class="right"><?php echo e($labelTaxable); ?></th><?php endif; ?>
                    <?php if($showTotal): ?><th class="right"><?php echo e($labelTotal); ?></th><?php endif; ?>
                </tr>
            </thead>
            <tbody>
                <?php $__currentLoopData = $layoutData['formatted']['items']; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $item): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <?php
                        // Group rate as GST percentage
                        $rawItem = $invoice->items->firstWhere('id', $item['id']);
                        $gstPercentage = $rawItem ? (int) $rawItem->rate : 18;
                    ?>
                    <tr>
                        <td><?php echo e($loop->iteration); ?></td>
                        <td><?php echo e($item['sku']); ?></td>
                        <td class="left" style="font-weight: bold;"><?php echo e($item['product_description']); ?></td>
                        <?php if($showHs): ?><td><?php echo e($item['hs_code'] ?: '6004.9000'); ?></td><?php endif; ?>
                        <?php if($showUom): ?><td><?php echo e($item['uom'] ?: 'KG'); ?></td><?php endif; ?>
                        <?php if($showRate): ?><td class="right"><?php echo e($item['rate']); ?></td><?php endif; ?>
                        <?php if($showQty): ?><td><?php echo e($item['quantity']); ?></td><?php endif; ?>
                        <td>0</td>
                        <td><?php echo e($gstPercentage); ?></td>
                        <td>0</td>
                        <td>0</td>
                        <td>0</td>
                        <?php if($showTaxable): ?><td class="right"><?php echo e($item['value_sales_excluding_st']); ?></td><?php endif; ?>
                        <?php if($showTotal): ?><td class="right"><?php echo e($item['total_values']); ?></td><?php endif; ?>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <tr class="total-row">
                    <td colspan="3" class="right">Total</td>
                    <?php if($showHs): ?><td></td><?php endif; ?>
                    <?php if($showUom): ?><td></td><?php endif; ?>
                    <?php if($showRate): ?><td></td><?php endif; ?>
                    <?php if($showQty): ?><td><?php echo e(number_format($invoice->items->sum('quantity'), $quantityPrecision)); ?></td><?php endif; ?>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <?php if($showTaxable): ?><td class="right"><?php echo e($layoutData['formatted']['subtotal']); ?></td><?php endif; ?>
                    <?php if($showTotal): ?><td class="right"><?php echo e($layoutData['formatted']['total_amount']); ?></td><?php endif; ?>
                </tr>
            </tbody>
        </table>
    </div>

    <div class="bottom-flex" style="<?php echo e(($showFooterImage && $footerImgSrc) ? 'margin-bottom: 140px;' : ''); ?>">
        <div class="bottom-left-panel">
            <div class="payment-method-row">Payment method: Credit</div>
            <div><b>FBR Invoice No:</b> <?php echo e($invoice->fbr_invoice_number ?? 'Not Synced'); ?></div>
            
            <div class="logo-and-qr-box">
                <img src="https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php echo e(urlencode($qrData)); ?>" class="qr-img" alt="QR" />

                <?php if($fbrLogoSrc): ?>
                    <img src="<?php echo e($fbrLogoSrc); ?>" class="fbr-pk-logo" alt="FBR Pakistan Logo" />
                <?php endif; ?>
            </div>

            <div>
                <div class="footer-caption">FBR DIGITAL INVOICING SYSTEM</div>
                <div class="footer-note-text">This is a system-generated invoice, no signature required.</div>
            </div>
        </div>

        <div class="bottom-right-panel">
            <table class="totals-summary-table">
                <tr>
                    <td>Total (Excl. tax)</td>
                    <td class="val"><?php echo e($layoutData['formatted']['subtotal']); ?></td>
                </tr>
                <tr>
                    <td>Total Discount</td>
                    <td class="val">0.00</td>
                </tr>
                <tr>
                    <td>Total GST</td>
                    <td class="val"><?php echo e($layoutData['formatted']['sales_tax']); ?></td>
                </tr>
                <tr>
                    <td>FED Payable</td>
                    <td class="val">0.00</td>
                </tr>
                <tr>
                    <td>WH Income Tax</td>
                    <td class="val">0.00</td>
                </tr>
                <tr>
                    <td>Further Tax</td>
                    <td class="val">0.00</td>
                </tr>
                <tr>
                    <td><b>Total Tax</b></td>
                    <td class="val"><?php echo e($layoutData['formatted']['sales_tax']); ?></td>
                </tr>
                <tr style="background: #f8fafc;">
                    <td><b>Total (incl. Tax)</b></td>
                    <td class="val" style="font-size: 12px; color: #000;"><?php echo e($layoutData['formatted']['total_amount']); ?></td>
                </tr>
            </table>
        </div>
    </div>

    <?php if($showFooterImage && $footerImgSrc): ?>
        <div style="position: absolute; bottom: 0; left: 0; right: 0; height: 130px; overflow: hidden; padding: 0 30px 10px 30px;">
            <img src="<?php echo e($footerImgSrc); ?>" style="width: 100%; height: auto; display: block; border-radius: 4px;" alt="Footer Banner" />
        </div>
    <?php endif; ?>
</div><?php /**PATH /www/wwwroot/lifeelements.shiningerp.com/resources/views/invoices/pdfs/slim.blade.php ENDPATH**/ ?>