<?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="elegant-invoice-wrapper <?php echo e(($showFooterImage && $footerImgSrc) ? 'has-footer-image' : ''); ?>">
    <style>
        .elegant-invoice-wrapper {
            position: relative;
            max-width: 800px;
            margin: 0 auto;
            padding: 40px 45px;
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #2b2d38;
            background: #fff;
        }

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

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

        .elegant-invoice-wrapper .watermark {
            position: absolute;
            top: 55%;
            left: 50%;
            transform: translate(-50%, -50%) rotate(-35deg);
            font-size: 80px;
            font-weight: 300;
            color: rgba(0, 0, 0, 0.05);
            z-index: 0;
            letter-spacing: 4px;
            text-transform: uppercase;
            pointer-events: none;
        }

        .elegant-invoice-wrapper .header {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            margin-bottom: 35px;
            border-bottom: 1px solid #e2e8f0;
            padding-bottom: 25px;
        }

        .elegant-invoice-wrapper .brand {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .elegant-invoice-wrapper .brand .logo {
            max-height: 48px;
            width: auto;
            object-fit: contain;
        }

        .elegant-invoice-wrapper .brand-text .name {
            font-size: 20px;
            font-weight: 300;
            color: #1a202c;
            letter-spacing: -0.5px;
        }

        .elegant-invoice-wrapper .brand-text .tag {
            font-size: 9px;
            font-weight: 600;
            color: #4a5568;
            letter-spacing: 2px;
            text-transform: uppercase;
        }

        .elegant-invoice-wrapper .title-block {
            text-align: right;
        }

        .elegant-invoice-wrapper .title-block h1 {
            font-size: 26px;
            font-weight: 300;
            letter-spacing: 1px;
            color: #1a202c;
            text-transform: uppercase;
        }

        .elegant-invoice-wrapper .title-block .subtitle {
            font-size: 10px;
            color: #718096;
            letter-spacing: 0.5px;
        }

        .elegant-invoice-wrapper .party-info {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 40px;
            margin-bottom: 35px;
        }

        .elegant-invoice-wrapper .party-box {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .elegant-invoice-wrapper .party-label {
            font-size: 9px;
            font-weight: 700;
            background-color: #c3dcaf !important;
            padding: 8px;
            color: #2b2d38 !important;
            letter-spacing: 1px;
            text-transform: uppercase;
        }

        .elegant-invoice-wrapper .party-details {
            font-size: 11.5px;
            line-height: 1.5;
            color: #4a5568;
        }

        .elegant-invoice-wrapper .party-name {
            font-size: 14px;
            font-weight: 600;
            color: #1a202c;
            margin-bottom: 2px;
        }

        .elegant-invoice-wrapper .invoice-details-bar {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 20px;
            background: #f7fafc;
            border-radius: 8px;
            padding: 15px 20px;
            margin-bottom: 35px;
        }

        .elegant-invoice-wrapper .detail-item {
            display: flex;
            flex-direction: column;
            gap: 3px;
        }

        .elegant-invoice-wrapper .detail-label {
            font-size: 9px;
            font-weight: 700;
            color: #a0aec0;
            text-transform: uppercase;
        }

        .elegant-invoice-wrapper .detail-val {
            font-size: 11px;
            font-weight: 600;
            color: #2d3748;
        }

        .elegant-invoice-wrapper .items-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 30px;
        }

        .elegant-invoice-wrapper .items-table th {
            background-color: #c3dcaf !important;
            border-bottom: 2px solid #e2e8f0;
            font-size: 9px;
            font-weight: 700;
            text-transform: uppercase;
            color: #1a202c !important;
            padding: 12px 10px;
            text-align: left;
        }

        .elegant-invoice-wrapper .items-table td {
            border-bottom: 1px solid #edf2f7;
            font-size: 11px;
            padding: 12px 10px;
            color: #4a5568;
        }

        .elegant-invoice-wrapper .items-table th.num,
        .elegant-invoice-wrapper .items-table td.num {
            text-align: right;
        }

        .elegant-invoice-wrapper .summary-section {
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            gap: 40px;
            margin-bottom: 30px;
        }

        .elegant-invoice-wrapper .words-box {
            max-width: 340px;
            font-size: 10.5px;
            color: #718096;
            line-height: 1.5;
        }

        .elegant-invoice-wrapper .totals-box {
            width: 280px;
            display: flex;
            flex-direction: column;
            gap: 8px;
        }

        .elegant-invoice-wrapper .totals-row {
            display: flex;
            justify-content: space-between;
            font-size: 11px;
            color: #4a5568;
        }

        .elegant-invoice-wrapper .totals-row.grand-total {
            background-color: #c3dcaf !important;
            border-top: 1px solid #e2e8f0;
            padding-top: 10px;
            font-size: 14px;
            font-weight: 700;
            color: #1a202c;
        }

        .elegant-invoice-wrapper .footer-section {
            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            border-top: 1px solid #e2e8f0;
            padding-top: 25px;
            margin-top: 30px;
        }

        .elegant-invoice-wrapper .notes-box {
            font-size: 10px;
            color: #a0aec0;
            max-width: 320px;
        }

        .elegant-invoice-wrapper .sign-area {
            text-align: center;
            font-size: 10.5px;
        }

        .elegant-invoice-wrapper .sign-line {
            width: 140px;
            height: 1px;
            background: #cbd5e0;
            margin: 0 auto 6px auto;
        }
    </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.09; 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% + 90px); overflow: hidden; margin: -40px -45px 25px -45px;">
            <img src="<?php echo e($letterheadImgSrc); ?>" style="width: 100%; height: auto; display: block;" alt="Letterhead" />
        </div>
    <?php else: ?>
        <div class="header">
            <div class="brand">
                <?php if($businessLogoSrc): ?>
                    <img src="<?php echo e($businessLogoSrc); ?>" class="logo" alt="Logo" />
                <?php else: ?>
                    <div class="brand-text">
                        <div class="name"><?php echo e($invoice->business->business_name ?? $invoice->business->name); ?></div>
                        <div class="tag">Digital Invoicing</div>
                    </div>
                <?php endif; ?>
            </div>
            <div class="title-block">
                <h1><?php echo e($layoutData['titleLabel'] ?? 'Tax Invoice'); ?></h1>
                <div class="subtitle">IRIS Compliant Complied</div>
            </div>
        </div>
    <?php endif; ?>

    <div class="party-info">
        <div class="party-box">
            <span class="party-label">Supplier</span>
            <div class="party-details">
                <div class="party-name"><?php echo e($invoice->business->business_name ?? $invoice->business->name); ?></div>
                <div><?php echo e($invoice->business->address); ?></div>
                <div>NTN: <?php echo e($invoice->business->ntn_cnic); ?></div>
                <div>STRN: <?php echo e($invoice->business->strn ?? '—'); ?></div>
            </div>
        </div>
        <div class="party-box">
            <span class="party-label">Customer</span>
            <div class="party-details">
                <div class="party-name"><?php echo e($invoice->customer->name ?? 'Walk-In Customer'); ?></div>
                <div><?php echo e($invoice->customer->address); ?></div>
                <div>NTN: <?php echo e($invoice->customer->ntn_cnic ?? '—'); ?></div>
                <div>STRN: <?php echo e($invoice->customer->strn ?? '—'); ?></div>
            </div>
        </div>
    </div>

    <div class="invoice-details-bar">
        <div class="detail-item">
            <span class="detail-label">Invoice Number</span>
            <span class="detail-val"><?php echo e($invoice->local_invoice_no); ?></span>
        </div>
        <div class="detail-item">
            <span class="detail-label">FBR Ref No</span>
            <span class="detail-val"
                style="word-break: break-all;"><?php echo e($invoice->fbr_invoice_number ?? 'Not Synced'); ?></span>
        </div>
        <div class="detail-item">
            <span class="detail-label">Date</span>
            <span class="detail-val"><?php echo e($layoutData['formatted']['invoice_date']); ?></span>
        </div>
        <div class="detail-item">
            <span class="detail-label">Time</span>
            <span class="detail-val"><?php echo e($layoutData['formatted']['invoice_time']); ?></span>
        </div>
    </div>

    <table class="items-table">
        <thead>
            <tr>
                <th style="width: 40px;">#</th>
                <th>Description</th>
                <?php if($showHs): ?>
                <th><?php echo e($labelHs); ?></th><?php endif; ?>
                <?php if($showQty): ?>
                <th class="num"><?php echo e($labelQty); ?></th><?php endif; ?>
                <?php if($showUom): ?>
                <th><?php echo e($labelUom); ?></th><?php endif; ?>
                <?php if($showRate): ?>
                <th class="num"><?php echo e($labelRate); ?></th><?php endif; ?>
                <th class="num">Unit Price </th>
                <?php if($showTaxable): ?>
                <th class="num"><?php echo e($labelTaxable); ?></th><?php endif; ?>
                <?php if($showTax): ?>
                <th class="num"><?php echo e($labelTax); ?></th><?php endif; ?>
                <?php if($showTotal): ?>
                <th class="num"><?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(); ?>
                <tr>
                    <td><?php echo e($loop->iteration); ?></td>
                    <td style="font-weight: 500;"><?php echo e($item['product_description']); ?></td>
                    <?php if($showHs): ?>
                    <td><?php echo e($item['hs_code']); ?></td><?php endif; ?>
                    <?php if($showQty): ?>
                    <td class="num"><?php echo e($item['quantity']); ?></td><?php endif; ?>
                    <?php if($showUom): ?>
                    <td><?php echo e($item['uom']); ?></td><?php endif; ?>
                    <?php if($showRate): ?>
                    <td class="num"><?php echo e($item['rate']); ?></td><?php endif; ?>
                    <td class="num"><?php echo e($currencySymbol); ?><?php echo e($item['unit_price']); ?></td>
                    <?php if($showTaxable): ?>
                    <td class="num"><?php echo e($item['value_sales_excluding_st']); ?></td><?php endif; ?>
                    <?php if($showTax): ?>
                    <td class="num"><?php echo e($item['sales_tax_applicable']); ?></td><?php endif; ?>
                    <?php if($showTotal): ?>
                    <td class="num" style="font-weight: 600;"><?php echo e($item['total_values']); ?></td><?php endif; ?>
                </tr>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </tbody>
    </table>

    <div class="summary-section">
        <div class="words-box">
            <p
                style="font-weight: 700; margin-bottom: 4px; text-transform: uppercase; font-size: 9px; letter-spacing: 0.5px;">
                Amount in Words</p>
            <p style="font-style: italic;"><?php echo e($layoutData['formatted']['amount_in_words']); ?> Rupees Only</p>
        </div>
        <div class="totals-box">
            <div class="totals-row">
                <span>Subtotal</span>
                <span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['subtotal']); ?></span>
            </div>
            <div class="totals-row">
                <span>Sales Tax</span>
                <span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['sales_tax']); ?></span>
            </div>
            <div class="totals-row grand-total">
                <span>Total Amount</span>
                <span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['total_amount']); ?></span>
            </div>
        </div>
    </div>

    <?php if($showFooterImage && $footerImgSrc): ?>
        <div
            style="display: flex; justify-content: space-between; align-items: flex-end; border-top: 1px solid #e2e8f0; padding-top: 25px; margin-top: 30px;">
            <div style="display: flex; gap: 20px; align-items: center;">
                <div style="text-align: center;">
                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php echo e(urlencode($qrData)); ?>"
                        style="width: 70px; height: 70px; display:block;" alt="QR" />
                    <span style="font-size: 8px; color: #a0aec0; margin-top: 2px; display: block;">Verification QR</span>
                </div>
                <?php if($fbrLogoSrc): ?>
                    <div style="text-align: center;">
                        <img src="<?php echo e($fbrLogoSrc); ?>" style="width: 70px; height: 70px; object-fit: contain; display: block;"
                            alt="FBR" />
                        <span style="font-size: 8px; color: #a0aec0; margin-top: 2px; display: block;">IRIS Certified</span>
                    </div>
                <?php endif; ?>
            </div>

            <div class="sign-area">
                <div class="sign-line"></div>
                <p style="font-weight: 600; color: #2d3748;">Authorized Signature</p>
            </div>
        </div>

        <div
            style="position: absolute; bottom: 0; left: 0; right: 0; height: 130px; overflow: hidden; padding: 0 45px 10px 45px;">
            <img src="<?php echo e($footerImgSrc); ?>" style="width: 100%; height: auto; display: block; border-radius: 4px;"
                alt="Footer Banner" />
        </div>
    <?php else: ?>
        <div class="footer-section">
            <div class="notes-box">
                <p style="font-weight: 600; margin-bottom: 2px;">Terms & Conditions</p>
                <p>This is an IRIS FBR Digital Invoicing compliant receipt. Verified via secure QR signature.</p>
            </div>

            <div style="display: flex; gap: 20px; align-items: center;">
                <div style="text-align: center;">
                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php echo e(urlencode($qrData)); ?>"
                        style="width: 70px; height: 70px; display:block;" alt="QR" />
                    <span style="font-size: 8px; color: #a0aec0; margin-top: 2px; display: block;">Verification QR</span>
                </div>
                <?php if($fbrLogoSrc): ?>
                    <div style="text-align: center;">
                        <img src="<?php echo e($fbrLogoSrc); ?>" style="width: 70px; height: 70px; object-fit: contain; display: block;"
                            alt="FBR" />
                        <span style="font-size: 8px; color: #a0aec0; margin-top: 2px; display: block;">IRIS Certified</span>
                    </div>
                <?php endif; ?>
            </div>

            <div class="sign-area">
                <div class="sign-line"></div>
                <p style="font-weight: 600; color: #2d3748;">Authorized Signature</p>
            </div>
        </div>
    <?php endif; ?>
</div><?php /**PATH /www/wwwroot/an.shiningerp.com/resources/views/invoices/pdfs/elegant.blade.php ENDPATH**/ ?>