<?php
/**
 * Invoice PDF Template - Default
 *
 * Available variables:
 * @var array  $settings        Invoice settings (company_name, etc.)
 * @var array  $data            Order data (order_number, billing, shipping, items, etc.)
 * @var string $currency_symbol Currency symbol
 *
 * Note: mPDF only supports traditional CSS, not utility frameworks like Tailwind.
 *
 * @package Storeplugin\PDFInvoice
 */

defined('ABSPATH') || exit;

?>

<!-- Header -->
<table class="invoice-header nxtpdf-header-zone">
    <tr>
        <td width="60%" valign="top">
            <h1 class="invoice-title"><?php echo esc_html($settings['invoice_title'] ?? 'INVOICE'); ?></h1>
            <?php if (!empty($settings['header_text'])): ?>
                <div><?php echo nl2br(esc_html($settings['header_text'])); ?></div>
            <?php endif; ?>
            <div class="company-info">
                <?php if (!empty($settings['show_company_name'])): ?>
                    <div class="company-name"><?php echo esc_html($settings['company_name']); ?></div>
                <?php endif; ?>
                <div class="company-details">
                    <?php if (!empty($settings['show_company_address'])): ?>
                        <?php if (!empty($settings['company_address_1'])): ?>
                            <?php echo esc_html($settings['company_address_1']); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($settings['company_address_2'])): ?>
                            <?php echo esc_html($settings['company_address_2']); ?><br>
                        <?php endif; ?>
                        <?php
                        $nxtpdf_city_line = array_filter([
                            $settings['company_city'] ?? '',
                            $settings['company_state'] ?? '',
                            $settings['company_postcode'] ?? '',
                        ]);
                        if (!empty($nxtpdf_city_line)): ?>
                            <?php echo esc_html(implode(', ', $nxtpdf_city_line)); ?><br>
                        <?php endif; ?>
                        <?php if (!empty($settings['company_country'])): ?>
                            <?php echo esc_html($settings['company_country']); ?><br>
                        <?php endif; ?>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_phone']) && !empty($settings['company_phone'])): ?>
                        <?php echo esc_html($settings['company_phone']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_email']) && !empty($settings['company_email'])): ?>
                        <?php echo esc_html($settings['company_email']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_vat']) && !empty($settings['company_vat'])): ?>
                        <?php esc_html_e('VAT:', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?> <?php echo esc_html($settings['company_vat']); ?><br>
                    <?php endif; ?>
                    <?php if (!empty($settings['show_company_website']) && !empty($settings['company_website'])): ?>
                        <div>Website: <?php echo esc_html($settings['company_website']); ?></div>
                    <?php endif; ?>
                    <?php if (!empty($settings['additional_header_text'])): ?>
                        <div><?php echo nl2br(esc_html($settings['additional_header_text'])); ?></div>
                    <?php endif; ?>
                </div>
            </div>
        </td>
        <td width="40%" valign="top" class="logo-container">
            <?php if (!empty($settings['show_logo']) && !empty($settings['logo_url'])): ?>
                <img class="nxtpdf-logo-image" src="<?php echo esc_url($settings['logo_url']); ?>" alt="Logo">
            <?php endif; ?>
        </td>
    </tr>
</table>

<!-- Invoice Meta -->
<table class="invoice-meta">
    <tr>
        <td width="50%">
            <span class="meta-label"><?php esc_html_e('Invoice Number:', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            <?php echo esc_html($data['invoice_number']); ?><br>
            <span class="meta-label"><?php esc_html_e('Order Reference:', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></span>
            #<?php echo esc_html($data['order_number']); ?>
        </td>
        <td width="50%" class="text-right">
            <span class="meta-label"><?php echo esc_html($data['invoice_date_label'] ?? 'Invoice Date'); ?>:</span>
            <?php echo esc_html($data['invoice_date'] ?? $data['order_date']); ?>
        </td>
    </tr>
    <tr>
        <td width="50%">
            <span class="meta-label"><?php echo esc_html($data['order_date_label'] ?? 'Order Date'); ?>:</span>
            <?php echo esc_html($data['order_date']); ?>
        </td>
        <td width="50%" class="text-right">
            <?php if (!empty($data['payment_method'])): ?>
            <span class="meta-label"><?php echo esc_html($settings['payment_method_label'] ?? 'Payment Method'); ?>:</span>
            <?php echo esc_html($data['payment_method']); ?>
            <?php endif; ?>
        </td>
    </tr>
</table>

<!-- Addresses -->
<table class="addresses-table">
    <tr>
        <td width="48%" valign="top">
            <?php if (!empty($settings['show_billing_address'])): ?>
            <div class="address-box">
                <div class="address-title"><?php echo esc_html($settings['bill_to_label'] ?? 'Bill To'); ?></div>
                <strong><?php echo esc_html($data['billing']['first_name'] . ' ' . $data['billing']['last_name']); ?></strong><br>
                <?php if (!empty($data['billing']['company'])): ?>
                    <?php echo esc_html($data['billing']['company']); ?><br>
                <?php endif; ?>
                <?php echo esc_html($data['billing']['address_1']); ?><br>
                <?php if (!empty($data['billing']['address_2'])): ?>
                    <?php echo esc_html($data['billing']['address_2']); ?><br>
                <?php endif; ?>
                <?php echo esc_html($data['billing']['city'] . ', ' . $data['billing']['state'] . ' ' . $data['billing']['postcode']); ?><br>
                <?php echo esc_html($data['billing']['country']); ?><br>
                <?php if (!empty($settings['show_billing_email']) && !empty($data['billing']['email'])): ?>
                    <?php echo esc_html($data['billing']['email']); ?><br>
                <?php endif; ?>
                <?php if (!empty($settings['show_billing_phone']) && !empty($data['billing']['phone'])): ?>
                    <?php echo esc_html($data['billing']['phone']); ?>
                <?php endif; ?>
            </div>
            <?php endif; ?>
        </td>
        <td width="4%"></td>
        <?php
        $nxtpdf_show_shipping = !empty($settings['show_shipping_address']);
        ?>
        <td width="48%" valign="top">
            <?php if ($nxtpdf_show_shipping): ?>
            <div class="address-box">
                <div class="address-title"><?php echo esc_html($settings['ship_to_label'] ?? 'Ship To'); ?></div>
                <strong><?php echo esc_html($data['shipping']['first_name'] . ' ' . $data['shipping']['last_name']); ?></strong><br>
                <?php if (!empty($data['shipping']['company'])): ?>
                    <?php echo esc_html($data['shipping']['company']); ?><br>
                <?php endif; ?>
                <?php echo esc_html($data['shipping']['address_1']); ?><br>
                <?php if (!empty($data['shipping']['address_2'])): ?>
                    <?php echo esc_html($data['shipping']['address_2']); ?><br>
                <?php endif; ?>
                <?php echo esc_html($data['shipping']['city'] . ', ' . $data['shipping']['state'] . ' ' . $data['shipping']['postcode']); ?><br>
                <?php echo esc_html($data['shipping']['country']); ?>
            </div>
            <?php endif; ?>
        </td>
    </tr>
</table>

<!-- Items -->
<?php
$nxtpdf_show_image = !empty($settings['show_product_image']);
$nxtpdf_show_sku = !empty($settings['show_sku']);
$nxtpdf_show_discount = !empty($settings['show_discount_column']);
$nxtpdf_show_tax = !empty($settings['show_tax_column']);
$nxtpdf_item_col_width = 50;
if ($nxtpdf_show_image) {
    $nxtpdf_item_col_width -= 10;
}
if ($nxtpdf_show_sku) {
    $nxtpdf_item_col_width -= 10;
}
if ($nxtpdf_show_discount) {
    $nxtpdf_item_col_width -= 8;
}
if ($nxtpdf_show_tax) {
    $nxtpdf_item_col_width -= 8;
}
if ($nxtpdf_item_col_width < 25) {
    $nxtpdf_item_col_width = 25;
}
?>
<table class="items-table">
    <thead>
        <tr>
            <?php if ($nxtpdf_show_image): ?>
            <th width="10%"><?php esc_html_e('Image', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <th width="<?php echo esc_attr((string) $nxtpdf_item_col_width . '%'); ?>"><?php echo esc_html($settings['item_label'] ?? 'Item'); ?></th>
            <?php if ($nxtpdf_show_sku): ?>
            <th width="10%"><?php esc_html_e('SKU', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <th width="12%" class="text-right"><?php echo esc_html($settings['qty_label'] ?? 'Qty'); ?></th>
            <th width="15%" class="text-right"><?php echo esc_html($settings['price_label'] ?? 'Price'); ?></th>
            <?php if ($nxtpdf_show_discount): ?>
            <th width="10%" class="text-right"><?php esc_html_e('Discount', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <?php if ($nxtpdf_show_tax): ?>
            <th width="10%" class="text-right"><?php esc_html_e('Tax', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></th>
            <?php endif; ?>
            <th width="15%" class="text-right"><?php echo esc_html($settings['total_label'] ?? 'Total'); ?></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($data['items'] as $nxtpdf_index => $nxtpdf_item):
            $nxtpdf_quantity = (float) ($nxtpdf_item['quantity'] ?? 0);
            $nxtpdf_unit_source = ($settings['unit_price_source'] ?? 'subtotal') === 'subtotal'
                ? ($nxtpdf_item['subtotal'] ?? 0)
                : ($nxtpdf_item['total'] ?? 0);
            $nxtpdf_unit_price = $nxtpdf_quantity > 0 ? ($nxtpdf_unit_source / $nxtpdf_quantity) : 0;
            $nxtpdf_discount = $nxtpdf_item['discount'] ?? 0;
            $nxtpdf_tax = $nxtpdf_item['tax'] ?? 0;
        ?>
        <tr<?php if ($nxtpdf_index % 2 === 1) : ?> class="alt-row"<?php endif; ?>>
            <?php if ($nxtpdf_show_image): ?>
            <td><?php if (!empty($nxtpdf_item['image'])): ?><img src="<?php echo esc_url($nxtpdf_item['image']); ?>" width="40" height="40"><?php endif; ?></td>
            <?php endif; ?>
            <td>
                <?php echo esc_html($nxtpdf_item['name']); ?>
                <?php if (!empty($settings['show_variations']) && !empty($nxtpdf_item['variations'])): ?>
                    <br><small>
                    <?php foreach ($nxtpdf_item['variations'] as $nxtpdf_var): ?>
                        <?php echo esc_html($nxtpdf_var['key']); ?>: <?php echo esc_html($nxtpdf_var['value']); ?><br>
                    <?php endforeach; ?>
                    </small>
                <?php endif; ?>
                <?php if (!empty($settings['show_item_meta']) && !empty($nxtpdf_item['meta'])): ?>
                    <br><small>
                    <?php foreach ($nxtpdf_item['meta'] as $nxtpdf_meta): ?>
                        <?php echo esc_html($nxtpdf_meta['key']); ?>: <?php echo esc_html($nxtpdf_meta['value']); ?><br>
                    <?php endforeach; ?>
                    </small>
                <?php endif; ?>
            </td>
            <?php if ($nxtpdf_show_sku): ?>
            <td><?php echo esc_html($nxtpdf_item['sku'] ?? ''); ?></td>
            <?php endif; ?>
            <td class="text-right"><?php echo esc_html($nxtpdf_item['quantity']); ?></td>
            <td class="text-right"><?php echo esc_html($format_price($nxtpdf_unit_price)); ?></td>
            <?php if ($nxtpdf_show_discount): ?>
            <td class="text-right"><?php echo ($nxtpdf_discount > 0) ? '-' . esc_html($format_price($nxtpdf_discount)) : '-'; ?></td>
            <?php endif; ?>
            <?php if ($nxtpdf_show_tax): ?>
            <td class="text-right"><?php echo esc_html($format_price($nxtpdf_tax)); ?></td>
            <?php endif; ?>
            <td class="text-right"><?php echo esc_html($format_price($nxtpdf_item['total'])); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<!-- Totals -->
<table class="totals-table">
    <tr>
        <td><?php echo esc_html($settings['subtotal_label'] ?? 'Subtotal'); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($data['subtotal'])); ?></td>
    </tr>
    <?php if ($data['shipping_total'] > 0): ?>
    <tr>
        <td><?php echo esc_html($settings['shipping_label'] ?? 'Shipping'); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($data['shipping_total'])); ?></td>
    </tr>
    <?php endif; ?>
    <?php if (!empty($settings['show_discount_total']) && ($data['discount_total'] ?? 0) > 0): ?>
    <tr>
        <td><?php echo esc_html($settings['discount_label'] ?: 'Discount'); ?></td>
        <td class="text-right">-<?php echo esc_html($format_price($data['discount_total'])); ?></td>
    </tr>
    <?php endif; ?>

    <?php if (!empty($settings['show_coupon_details']) && !empty($data['coupons'])): ?>
    <?php foreach ($data['coupons'] as $nxtpdf_coupon): ?>
    <tr>
        <td><?php echo esc_html__('Coupon:', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?> <?php echo esc_html($nxtpdf_coupon['code']); ?></td>
        <td class="text-right">-<?php echo esc_html($format_price($nxtpdf_coupon['discount'])); ?></td>
    </tr>
    <?php endforeach; ?>
    <?php endif; ?>

    <?php if (!empty($settings['show_fees']) && !empty($data['fees'])): ?>
    <?php foreach ($data['fees'] as $nxtpdf_fee): ?>
    <tr>
        <td><?php echo esc_html($nxtpdf_fee['name']); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($nxtpdf_fee['total'])); ?></td>
    </tr>
    <?php endforeach; ?>
    <?php endif; ?>

    <?php if (!empty($settings['show_shipping_tax']) && ($data['shipping_tax'] ?? 0) > 0): ?>
    <tr>
        <td><?php echo esc_html__('Shipping Tax', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($data['shipping_tax'])); ?></td>
    </tr>
    <?php endif; ?>

    <?php if (!empty($settings['show_tax_by_rate']) && !empty($data['tax_rates'])): ?>
        <?php foreach ($data['tax_rates'] as $nxtpdf_tax): ?>
        <tr>
            <td><?php echo esc_html($nxtpdf_tax['label']); ?></td>
            <td class="text-right"><?php echo esc_html($format_price($nxtpdf_tax['amount'])); ?></td>
        </tr>
        <?php endforeach; ?>
    <?php elseif (!empty($data['tax_total']) && $data['tax_total'] > 0): ?>
    <tr>
        <td><?php echo esc_html($settings['tax_label'] ?? 'Tax'); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($data['tax_total'])); ?></td>
    </tr>
    <?php endif; ?>
    <tr class="total-row">
        <td><?php echo esc_html($settings['total_label'] ?? 'Total'); ?></td>
        <td class="text-right"><?php echo esc_html($format_price($data['total'])); ?></td>
    </tr>
</table>

<!-- Notes -->
<?php if (!empty($settings['show_order_notes']) && !empty($data['notes'])): ?>
<div class="notes-section">
    <div class="notes-title"><?php esc_html_e('Order Notes', 'storeinvoice-print-pdf-invoices-and-packing-slips'); ?></div>
    <?php echo esc_html($data['notes']); ?>
</div>
<?php endif; ?>

<?php if (!empty($settings['terms_text']) || !empty($settings['return_policy_text'])): ?>
<div class="terms-section nxtpdf-terms-section">
    <?php if (!empty($settings['terms_text'])): ?>
        <?php if (!empty($settings['terms_title'])): ?>
            <div class="nxtpdf-terms-title">
                <?php echo esc_html($settings['terms_title']); ?>
            </div>
        <?php endif; ?>
        <div class="nxtpdf-terms-block">
            <?php echo nl2br(esc_html($settings['terms_text'])); ?>
        </div>
    <?php endif; ?>

    <?php if (!empty($settings['return_policy_text'])): ?>
        <div>
            <?php if (!empty($settings['return_policy_title'])): ?>
                <strong><?php echo esc_html($settings['return_policy_title']); ?></strong><br>
            <?php endif; ?>
            <?php echo nl2br(esc_html($settings['return_policy_text'])); ?>
        </div>
    <?php endif; ?>
</div>
<?php endif; ?>

<!-- Footer -->
<?php if (!empty($settings['footer_text']) || !empty($settings['show_page_numbers'])): ?>
<div class="invoice-footer nxtpdf-footer-zone">
    <?php if (!empty($settings['footer_text'])): ?>
        <?php echo nl2br(esc_html($settings['footer_text'])); ?>
    <?php endif; ?>
    <?php if (!empty($settings['footer_text']) && !empty($settings['show_page_numbers'])): ?>
        <br>
    <?php endif; ?>
    <?php
        if (!empty($settings['show_page_numbers'])) {
            echo esc_html__('Page {PAGENO} of {nbpg}', 'storeinvoice-print-pdf-invoices-and-packing-slips');
        }
    ?>
</div>
<?php endif; ?>
