<?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="classic-invoice-wrapper <?php echo e(($showFooterImage && $footerImgSrc) ? 'has-footer-image' : ''); ?>">
    <style>
        .classic-invoice-wrapper {
            position: relative;
            max-width: 800px;
            margin: 0 auto;
            padding: 30px 34px 22px 34px;
            font-family: 'Segoe UI', Arial, sans-serif;
            color: #23262f;
            background: #fff;
            min-height: 297mm;
            box-sizing: border-box;
        }

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

        .classic-invoice-wrapper .watermark {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            font-size: 72px;
            font-weight: 800;
            color: rgba(0, 0, 0, 0.08);
            z-index: 0;
            pointer-events: none;
            text-transform: uppercase;
        }

        .classic-invoice-wrapper .watermark-img {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 320px;
            height: auto;
            opacity: 0.08;
            z-index: 0;
            pointer-events: none;
        }

        .classic-invoice-wrapper .header {
            position: relative;
            display: flex;
            justify-content: space-between;
            align-items: center;
            border-bottom: 3px solid #0e2a47;
            padding-bottom: 12px;
            margin-bottom: 20px;
        }

        .classic-invoice-wrapper .header-slant {
            position: absolute;
            bottom: -3px;
            left: 0;
            width: 140px;
            height: 3px;
            background: #1e7a46;
        }

        .classic-invoice-wrapper .brand {
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .classic-invoice-wrapper .brand .logo {
            width: auto;
            height: 52px;
            object-fit: contain;
        }

        .classic-invoice-wrapper .brand-icon {
            width: 44px;
            height: 44px;

            background: #0e2a47;
            display: flex;
            align-items: center;
            justify-content: center;
            color: #fff;
            font-weight: bold;
        }

        .classic-invoice-wrapper .brand-text .name {
            font-size: 16px;
            font-weight: 800;
            color: #0e2a47;
            letter-spacing: -0.3px;
        }

        .classic-invoice-wrapper .brand-text .tag {
            font-size: 8.5px;
            font-weight: 700;
            color: #1e7a46;
            letter-spacing: 1.2px;
            margin-top: 1px;
        }

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

        .classic-invoice-wrapper .title-block h1 {
            font-size: 22px;
            font-weight: 900;
            color: #0e2a47;
            letter-spacing: -0.5px;
            line-height: 1.1;
        }

        .classic-invoice-wrapper .title-block .subtitle {
            font-size: 9px;
            font-weight: 700;
            color: #6b7280;
            letter-spacing: 0.5px;
            margin-top: 2px;
        }

        .classic-invoice-wrapper .title-block .verified {
            font-size: 10px;
            font-weight: 600;
            color: #1e7a46;
            margin-top: 3px;
        }

        .classic-invoice-wrapper .meta-bar {
            display: flex;
            justify-content: space-between;
            background: #f7f8fa;
            border: 1px solid #dfe3e8;

            padding: 8px 14px;
            font-size: 11px;
            margin-bottom: 20px;
        }

        .classic-invoice-wrapper .top-grid {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 20px;
            margin-bottom: 20px;
        }

        .classic-invoice-wrapper .box {
            border: 1px solid #dfe3e8;

            overflow: hidden;
            background: #fff;
        }

        .classic-invoice-wrapper .box-header {
            background: #0e2a47 !important;
            border-bottom: 1px solid #dfe3e8;
            padding: 7px 12px;
            font-size: 10.5px;
            font-weight: 700;
            color: #fff !important;
            letter-spacing: 0.5px;
        }

        .classic-invoice-wrapper .box-body {
            padding: 10px 12px;
        }

        .classic-invoice-wrapper .party-body .party-name {
            font-size: 13px;
            font-weight: 700;
            color: #0e2a47;
            margin-bottom: 4px;
        }

        .classic-invoice-wrapper .party-body p {
            font-size: 11px;
            color: #6b7280;
            line-height: 1.4;
        }

        .classic-invoice-wrapper .party-body .contact-line {
            font-size: 10px;
            color: #98a1ad;
            margin-top: 5px;
        }

        .classic-invoice-wrapper .details-table {
            width: 100%;
            font-size: 11px;
            border-collapse: collapse;
        }

        .classic-invoice-wrapper .details-table td {
            padding: 3px 0;
        }

        .classic-invoice-wrapper .details-table td:nth-child(odd) {
            color: #98a1ad;
            width: 110px;
        }

        .classic-invoice-wrapper .details-table td:nth-child(even) {
            font-weight: 600;
            color: #23262f;
        }

        .classic-invoice-wrapper .items-wrap {
            border: 1px solid #dfe3e8;
            overflow: hidden;
            margin-bottom: 20px;
        }

        .classic-invoice-wrapper .items {
            width: 100%;
            border-collapse: collapse;
            font-size: 10px;
        }

        .classic-invoice-wrapper .items th {
            background: #0e2a47;
            color: #fff;
            font-weight: 700;
            padding: 6px 4px;
            text-align: left;
        }

        .classic-invoice-wrapper .items td {
            padding: 6px 4px;
            border-bottom: 1px solid #dfe3e8;
            word-break: break-word;
        }

        .classic-invoice-wrapper .items tr:last-child td {
            border-bottom: none;
        }

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

        .classic-invoice-wrapper .summary-grid {
            display: grid;
            grid-template-columns: 1.2fr 0.8fr;
            gap: 24px;
            margin-bottom: 20px;
        }

        .classic-invoice-wrapper .tax-summary-table {
            width: 100%;
            font-size: 10px;
            border-collapse: collapse;
            border: 1px solid #dfe3e8;
            overflow: hidden;
        }

        .classic-invoice-wrapper .tax-summary-table th {
            background: #f7f8fa;
            padding: 5px 8px;
            font-weight: 700;
            text-align: left;
            border-bottom: 1px solid #dfe3e8;
        }

        .classic-invoice-wrapper .tax-summary-table td {
            padding: 5px 8px;
            border-bottom: 1px solid #dfe3e8;
        }

        .classic-invoice-wrapper .tax-summary-table tr.total-row td {
            font-weight: 700;
            background: #f7f8fa;
        }

        .classic-invoice-wrapper .tax-summary-table th.num,
        .classic-invoice-wrapper .tax-summary-table td.num {
            text-align: right;
        }

        .classic-invoice-wrapper .words-box {
            margin-top: 10px;
            background: #f7f8fa;
            border: 1px dashed #dfe3e8;
            padding: 8px 12px;
            font-size: 10.5px;
        }

        .classic-invoice-wrapper .totals-box {
            border: 1px solid #dfe3e8;
            padding: 10px 14px;
            background: #f7f8fa;
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .classic-invoice-wrapper .totals-row {
            display: flex;
            justify-content: space-between;
            font-size: 11px;
        }

        .classic-invoice-wrapper .totals-row.net {
            border-top: 1px solid #dfe3e8;
            padding: 7px 14px;
            font-size: 12.5px;
            font-weight: 800;
            color: #fff !important;
            background: #0e2a47;

        }

        .classic-invoice-wrapper .bottom-grid {
            display: grid;
            grid-template-columns: 1.1fr 0.9fr;
            gap: 20px;
            border-top: 1px solid #dfe3e8;
            padding-top: 16px;
            margin-top: 20px;
        }

        .classic-invoice-wrapper .notes-col h4 {
            font-size: 11px;
            font-weight: 700;
            color: #0e2a47;
            margin-bottom: 4px;
        }

        .classic-invoice-wrapper .notes-col ul {
            padding-left: 14px;
            font-size: 10px;
            color: #6b7280;
        }

        .classic-invoice-wrapper .notes-col li {
            margin-bottom: 2px;
        }

        .classic-invoice-wrapper .sign-area {
            margin-top: 20px;
            font-size: 10px;
        }

        .classic-invoice-wrapper .sign-line {
            width: 130px;
            height: 1px;
            background: #dfe3e8;
            margin-bottom: 4px;
        }

        .classic-invoice-wrapper .qr-caption {
            font-size: 8.5px;
            color: #98a1ad;
            margin-top: 3px;
        }

        .classic-invoice-wrapper .thankyou-col {
            text-align: right;
        }

        .classic-invoice-wrapper .thankyou-col .ty-title {
            font-size: 12px;
            font-weight: 700;
            color: #0e2a47;
            margin-bottom: 3px;
        }

        .classic-invoice-wrapper .thankyou-col .ty-line {
            font-size: 10px;
            color: #6b7280;
        }
    </style>

    <?php if($showWatermark): ?>
        <?php if($watermarkType === 'image' && $watermarkImgSrc): ?>
            <img src="<?php echo e($watermarkImgSrc); ?>" class="watermark-img" alt="Watermark Logo" />
        <?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% + 68px); overflow: hidden; margin: -30px -34px 16px -34px;">
            <img src="<?php echo e($letterheadImgSrc); ?>" style="width: 100%; height: auto; display: block;" alt="Letterhead" />
        </div>
    <?php else: ?>
        <div class="header">
            <div class="header-slant"></div>
            <div class="brand">
                <?php if($businessLogoSrc): ?>
                    <img src="<?php echo e($businessLogoSrc); ?>" class="logo" alt="Business Logo" />
                <?php elseif($fbrLogoSrc): ?>
                    <img src="<?php echo e($fbrLogoSrc); ?>" class="logo" alt="FBR Logo" />
                <?php else: ?>
                    <div class="brand-icon"><span>FBR</span></div>
                <?php endif; ?>
                <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>
            </div>
            <div class="title-block">
                <h1><?php echo e($layoutData['titleLabel'] ?? 'TAX INVOICE'); ?></h1>
                <div class="subtitle">FBR DIGITAL INVOICING</div>
                <div class="verified">IRIS Compliant <span
                        style="color: #1e7a46; font-weight: bold; font-size: 11px;">&#10003;</span></div>
            </div>
        </div>
    <?php endif; ?>

    <div class="meta-bar">
        <div>NTN: <b><?php echo e($invoice->business->ntn_cnic); ?></b></div>
        <div>STRN: <b><?php echo e($invoice->business->strn ?? '—'); ?></b></div>
        <div>Invoice Date: <b><?php echo e($layoutData['formatted']['invoice_date']); ?></b></div>
        <div>Time: <b><?php echo e($layoutData['formatted']['invoice_time']); ?></b></div>
    </div>

    <div class="top-grid">
        <div class="box">
            <div class="box-header">SUPPLIER</div>
            <div class="box-body party-body">
                <div class="party-name"><?php echo e($invoice->business->business_name ?? $invoice->business->name); ?></div>
                <p><?php echo e($invoice->business->address ?? '—'); ?></p>
                <div class="contact-line"><?php echo e($invoice->business->phone ?? '—'); ?> &nbsp;·&nbsp;
                    <?php echo e($invoice->business->email ?? '—'); ?>

                </div>
            </div>
        </div>

        <div class="box">
            <div class="box-header">CUSTOMER</div>
            <div class="box-body party-body">
                <div class="party-name"><?php echo e($invoice->customer->name ?? 'Walk-In Customer'); ?></div>
                <p><?php echo e($invoice->customer->address ?? '—'); ?></p>
                <div class="ntn-strn">NTN: <?php echo e($invoice->customer->ntn_cnic ?? '—'); ?> &nbsp;|&nbsp; STRN:
                    <?php echo e($invoice->customer->strn ?? '—'); ?>

                </div>
            </div>
        </div>
    </div>

    <div class="box" style="margin-bottom: 20px;">
        <div class="box-header">INVOICE DETAILS</div>
        <div class="box-body">
            <table class="details-table">
                <tr>
                    <td>Invoice No.</td>
                    <td><?php echo e($invoice->local_invoice_no); ?></td>
                    <td style="width:1px;"></td>
                    <td>Scenario ID</td>
                    <td><?php echo e($invoice->scenario_id ?? 'SN001'); ?></td>
                </tr>
                <tr>
                    <td>FBR Invoice No.</td>
                    <td style="word-break: break-all;"><?php echo e($invoice->fbr_invoice_number ?? 'Not Synced'); ?></td>
                    <td></td>
                    <td>Sync Status</td>
                    <td><?php echo e($invoice->sync_status->label()); ?></td>
                </tr>
            </table>
        </div>
    </div>

    <div class="items-wrap">
        <table class="items">
            <thead>
                <tr>
                    <th>Sr.</th>
                    <th>Description of Goods / Services</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><?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"><?php echo e($item['total_values']); ?></td><?php endif; ?>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
            </tbody>
        </table>
    </div>

    <div class="summary-grid">
        <div>
            <table class="tax-summary-table">
                <tr>
                    <th>Rate</th>
                    <th class="num">Taxable Amount</th>
                    <th class="num">Sales Tax</th>
                </tr>
                <?php $taxGroups = $invoice->items->groupBy('rate'); ?>
                <?php $__currentLoopData = $taxGroups; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $rate => $groupedItems): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                    <tr>
                        <td><?php echo e($rate); ?></td>
                        <td class="num">
                            <?php echo e(number_format($groupedItems->sum('value_sales_excluding_st'), $currencyPrecision)); ?>

                        </td>
                        <td class="num"><?php echo e(number_format($groupedItems->sum('sales_tax_applicable'), $currencyPrecision)); ?>

                        </td>
                    </tr>
                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                <tr class="total-row">
                    <td>Total</td>
                    <td class="num"><?php echo e($layoutData['formatted']['subtotal']); ?></td>
                    <td class="num"><?php echo e($layoutData['formatted']['sales_tax']); ?></td>
                </tr>
            </table>
            <div class="words-box">
                <b>Amount in Words</b><br>
                <i><?php echo e($layoutData['formatted']['amount_in_words']); ?> Rupees Only</i>
            </div>
        </div>
        <div class="totals-box">
            <div class="totals-row"><span>Total Amount (Excl.
                    Tax)</span><span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['subtotal']); ?></span></div>
            <div class="totals-row"><span>Total Sales
                    Tax</span><span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['sales_tax']); ?></span></div>
            <div class="totals-row"><span>Withholding Tax</span><span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['extra_tax']); ?></span></div>
            <div class="totals-row"><span>Further Tax</span><span><?php echo e($currencySymbol); ?><?php echo e($layoutData['formatted']['further_tax']); ?></span></div>
            <div class="totals-row"><span>Rounding</span><span><?php echo e($currencySymbol); ?>0.00</span></div>
            <div class="totals-row net"><span>Net Payable
                    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; gap: 16px; align-items: center; justify-content: flex-end; margin-top: 16px; padding-top: 12px; border-top: 1px solid #dfe3e8;">
            <div style="text-align: center;">
                <div class="qr-box">
                    <img src="https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php echo e(urlencode($qrData)); ?>"
                        style="width: 80px; height: 80px; display:block;" alt="QR Code" />
                </div>
                <div class="qr-caption">Scan to verify</div>
            </div>

            <?php if($fbrLogoSrc): ?>
                <div style="text-align: center;">
                    <div
                        style="padding: 4px; border: 1px solid #dfe3e8;  background: #fff; width: 90px; height: 90px; display: flex; align-items: center; justify-content: center;">
                        <img src="<?php echo e($fbrLogoSrc); ?>" style="width: 80px; height: 80px; object-fit: contain; display: block;"
                            alt="FBR DI Logo" />
                    </div>
                    <div class="qr-caption">IRIS Certified</div>
                </div>
            <?php endif; ?>
        </div>

        <div
            style="position: absolute; bottom: 0; left: 0; right: 0; height: 130px; overflow: hidden; padding: 0 34px 10px 34px;">
            <img src="<?php echo e($footerImgSrc); ?>" style="width: 100%; height: auto; display: block; " alt="Footer Banner" />
        </div>
    <?php else: ?>
        <div class="bottom-grid">
            <div class="notes-col">
                <h4>Important Notes</h4>
                <ul>
                    <li>Goods once sold will not be taken back.</li>
                    <li>Please make payment within the due date.</li>
                    <li>Interest @ 18% p.a. will be charged on overdue amounts.</li>
                </ul>
                <div class="sign-area">
                    <div class="sign-line"></div>
                    <b>Authorised Signature</b><br>
                    <?php echo e($invoice->business->business_name ?? $invoice->business->name); ?>

                </div>
            </div>

            <div style="display: flex; flex-direction: column; justify-content: space-between; align-items: flex-end;">
                <div style="display: flex; gap: 16px; align-items: center;">
                    <div style="text-align: center;">
                        <div class="qr-box">
                            <img src="https://api.qrserver.com/v1/create-qr-code/?size=100x100&data=<?php echo e(urlencode($qrData)); ?>"
                                style="width: 80px; height: 80px; display:block;" alt="QR Code" />
                        </div>
                        <div class="qr-caption">Scan to verify</div>
                    </div>

                    <?php if($fbrLogoSrc): ?>
                        <div style="text-align: center;">
                            <div
                                style="padding: 4px; border: 1px solid #dfe3e8;  background: #fff; width: 90px; height: 90px; display: flex; align-items: center; justify-content: center;">
                                <img src="<?php echo e($fbrLogoSrc); ?>"
                                    style="width: 80px; height: 80px; object-fit: contain; display: block;" alt="FBR DI Logo" />
                            </div>
                            <div class="qr-caption">IRIS Certified</div>
                        </div>
                    <?php endif; ?>
                </div>

                <div class="thankyou-col" style="margin-top: 14px;">
                    <div class="ty-title">Thank you for your business!</div>
                    <div class="ty-line"><?php echo e($invoice->business->phone ?? '—'); ?></div>
                    <div class="ty-line"><?php echo e($invoice->business->email ?? '—'); ?></div>
                </div>
            </div>
        </div>
    <?php endif; ?>
</div><?php /**PATH /www/wwwroot/an.shiningerp.com/resources/views/invoices/pdfs/classic.blade.php ENDPATH**/ ?>