<?php
/**
 * WooCommerce Invoice Template 1
 * Modern Layout - Optimized for Dompdf with Bangla Unicode Support
 */

if (!defined('ABSPATH')) exit;

// $order and $settings are expected to be passed to this template
if (!isset($order) || !$order) {
    echo esc_html( 'No order data provided.' );
    return;
}

$active_order = $order;
$template_settings = $settings;

// Helper to parse placeholders
if (!function_exists('dac_parse_template')) {
    function dac_parse_template($text, $data) {
        if (!$text || !$data) return $text;
        $placeholders = [
            '{{order_no}}' => $data['order_no'] ?? $data['id'],
            '{{customer_name}}' => $data['contact_name'] ?? '',
            '{{date}}' => $data['issued_at'] ?? '',
            '{{total}}' => $data['amount'] ?? '0.00',
        ];
        return str_replace(array_keys($placeholders), array_values($placeholders), $text);
    }
}

$header_text = dac_parse_template($template_settings['wc_invoice_header'] ?? 'INVOICE', $active_order);
$footer_text = dac_parse_template($template_settings['wc_invoice_footer'] ?? 'Thank you for your business!', $active_order);
$accent_color = $template_settings['wc_invoice_color'] ?? '#3b82f6';

// Shop Info
$shop_name = $template_settings['wc_invoice_shop_name'] ?? get_bloginfo('name');
$shop_address_1 = $template_settings['wc_invoice_shop_address_1'] ?? '';
$shop_address_2 = $template_settings['wc_invoice_shop_address_2'] ?? '';
$shop_city = $template_settings['wc_invoice_shop_city'] ?? '';
$shop_postcode = $template_settings['wc_invoice_shop_postcode'] ?? '';
$shop_country = $template_settings['wc_invoice_shop_country'] ?? '';
$shop_phone = $template_settings['wc_invoice_shop_phone'] ?? '';
$shop_email = $template_settings['wc_invoice_shop_email'] ?? '';
$shop_vat = $template_settings['wc_invoice_shop_vat'] ?? '';

// Currency Symbol
$currency_symbol = '৳'; 
$items = is_string($active_order['item_data']) ? json_decode($active_order['item_data'], true) : $active_order['item_data'];
$items = is_array($items) ? $items : [];
?>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title><?php echo esc_html($header_text); ?> - <?php echo esc_html($active_order['order_no'] ?? $active_order['id']); ?></title>
    <style>
        @font-face {
            font-family: 'Hind Siliguri';
            font-style: normal;
            font-weight: 400;
            src: url(https://fonts.gstatic.com/s/hindsiliguri/v12/ijwaNjc6uy9eOTvAmZcjTmajGS6TjS0.ttf) format('truetype');
        }
        @font-face {
            font-family: 'Hind Siliguri';
            font-style: normal;
            font-weight: 700;
            src: url(https://fonts.gstatic.com/s/hindsiliguri/v12/ijwbNjc6uy9eOTvAmZcjTmajGf6fuzS_mQ.ttf) format('truetype');
        }
        @page {
            margin: 15mm 10mm;
        }
        * {
            box-sizing: border-box;
        }
        body { 
            font-family: 'Hind Siliguri', 'DejaVu Sans', sans-serif;
            font-size: 12px;
            color: #374151; 
            margin: 0; 
            padding: 0;
            line-height: 1.4;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .container {
            padding: 0;
            max-width: 100%;
        }
        .accent-bar {
            height: 6px;
            background-color: <?php echo esc_attr($accent_color); ?>;
            width: 100%;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            table-layout: fixed;
        }
        td, th {
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .header-table {
            margin-bottom: 30px;
            table-layout: fixed;
        }
        .shop-name {
            font-size: 20px;
            font-weight: bold;
            color: <?php echo esc_attr($accent_color); ?>;
            text-transform: uppercase;
            letter-spacing: 1px;
            margin-bottom: 2px;
        }
        .shop-info {
            font-size: 10px;
            color: #6b7280;
            line-height: 1.3;
        }
        .document-type {
            font-size: 28px;
            font-weight: 300;
            color: #d1d5db;
            text-align: right;
            text-transform: uppercase;
            vertical-align: middle;
        }
        .info-grid {
            margin-bottom: 30px;
            border-top: 1px solid #f3f4f6;
            border-bottom: 1px solid #f3f4f6;
            padding: 15px 0;
            table-layout: fixed;
        }
        .info-block {
            vertical-align: top;
            padding: 0 8px;
        }
        .label {
            font-size: 9px;
            font-weight: bold;
            text-transform: uppercase;
            color: #9ca3af;
            letter-spacing: 1px;
            margin-bottom: 8px;
            display: block;
        }
        .value {
            font-size: 12px;
            color: #111827;
        }
        .items-table {
            width: 100%;
            margin-top: 10px;
            table-layout: fixed;
        }
        .items-table th {
            text-align: left;
            padding: 8px 10px;
            font-size: 10px;
            font-weight: bold;
            background: #f9fafb;
            color: #6b7280;
            border-bottom: 2px solid #e5e7eb;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .items-table td {
            padding: 10px;
            border-bottom: 1px solid #f3f4f6;
            vertical-align: top;
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        .summary-table {
            width: 100%;
            margin-top: 10px;
        }
        .summary-table td {
            padding: 5px 8px;
            font-size: 12px;
        }
        .grand-total {
            color: <?php echo esc_attr($accent_color); ?>;
            font-weight: bold;
            font-size: 14px;
        }
        .footer {
            margin-top: 40px;
            padding-top: 15px;
            font-size: 10px;
            color: #9ca3af;
            text-align: center;
        }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        .text-end { float: right; }
        .font-bold { font-weight: bold; }
    </style>
</head>
<body>
    <div class="accent-bar"></div>
    
    <div class="container">
        <!-- Header -->
        <table class="header-table">
            <tr>
                <td width="65%">
                    <div class="shop-name"><?php echo esc_html($shop_name); ?></div>
                    <div class="shop-info">
                        <?php echo esc_html($shop_address_1); ?><?php echo $shop_address_2 ? ', ' . esc_html($shop_address_2) : ''; ?><br>
                        <?php echo esc_html($shop_city); ?><?php echo $shop_postcode ? ', ' . esc_html($shop_postcode) : ''; ?><br>
                        <?php if ($shop_phone) echo 'Tel: ' . esc_html($shop_phone) . ' | '; ?><?php if ($shop_email) echo esc_html($shop_email); ?>
                        <?php if ($shop_vat): ?><br>VAT: <?php echo esc_html($shop_vat); ?><?php endif; ?>
                    </div>
                </td>
                <td width="35%" class="document-type">
                    <?php echo esc_html($header_text); ?>
                </td>
            </tr>
        </table>

        <!-- Info Grid -->
        <table class="info-grid">
            <tr>
                <td width="40%" class="info-block" style="padding-left: 0;">
                    <span class="label">Billed To</span>
                    <div class="value font-bold" style="font-size: 13px; ">
                        <?php echo esc_html($active_order['contact_name']); ?>
                    </div>
                    <div class="value" style="line-height: 1; color: #4b5563;">
                        <?php echo nl2br(esc_html($active_order['contact_address'])); ?>
                    </div>
                </td>
                <td width="30%" class="info-block">
                    <span class="label">Invoice Details</span>
                    <div class="value">
                        <span style="color: #9ca3af;">Number:</span> #<?php echo esc_html($active_order['order_no'] ?? $active_order['id']); ?>
                    </div>
                    <div class="value">
                        <span style="color: #9ca3af;">Date:</span> <?php echo esc_html( date('M d, Y', strtotime($active_order['issued_at'])) ); ?>
                    </div>
                </td>
                <td width="30%" class="info-block" style="padding-right: 0;">
                    <span class="label">Payment</span>
                    <div class="value">
                        <?php echo esc_html($active_order['payment_method'] ?? 'Standard Payment'); ?>
                    </div>
                </td>
            </tr>
        </table>

        <!-- Items Table -->
        <table class="items-table">
            <thead>
                <tr>
                    <th width="8%">#</th>
                    <th width="52%">Item Description</th>
                    <th width="15%"  style="text-align: right">Qty</th>
                    <th width="25%"  style="text-align: right;">Total</th>
                </tr>
            </thead>
            <tbody>
                <?php $i = 1; foreach ($items as $item): ?>
                <tr>
                    <td style="color: #d1d5db; font-weight: bold;"><?php echo esc_html( str_pad($i++, 2, '0', STR_PAD_LEFT) ); ?></td>
                    <td>
                        <div class="font-bold" style="font-size: 12px; color: #111827;"><?php echo esc_html($item['name']); ?></div>
                        <?php if (!empty($item['sku'])): ?>
                        <div style="font-size: 11px; color: #9ca3af; margin-top: 2px;">SKU: <?php echo esc_html($item['sku']); ?></div>
                        <?php endif; ?>
                        <?php if (!empty($item['meta'])): ?>
                        <div style="font-size: 11px; color: <?php echo esc_attr($accent_color); ?>; margin-top: 4px; font-style: italic;">
                            <?php echo esc_html($item['meta']); ?>
                        </div>
                        <?php endif; ?>
                    </td>
                    <td style="text-align: right;"><?php echo esc_html($item['quantity']); ?></td>
                    <td style="text-align: right;"><?php echo esc_html($currency_symbol); ?> <?php echo number_format($item['total'], 2); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>

        <!-- Summary -->
        <table class="summary-table">
            <tr>
                <td width="55%"></td>
                <td width="45%" style="padding-right: 0;">
                    <table>
                        <tr>
                            <td class="text-right" style="color: #6b7280; font-size: 11px; text-transform: uppercase; letter-spacing: 1px;">Subtotal</td>
                            <td width="120" class="text-right"><?php echo esc_html($currency_symbol); ?> <?php echo number_format($active_order['subtotal'], 2); ?></td>
                        </tr>
                        <?php if ($active_order['discount'] > 0): ?>
                        <tr>
                            <td class="text-right" style="color: #6b7280; font-size: 11px; text-transform: uppercase; letter-spacing: 1px;">Discount</td>
                            <td class="text-right" style="color: #ef4444;">- <?php echo esc_html($currency_symbol); ?> <?php echo number_format($active_order['discount'], 2); ?></td>
                        </tr>
                        <?php endif; ?>
                        <tr>
                            <td class="text-right" style="color: #6b7280; font-size: 11px; text-transform: uppercase; letter-spacing: 1px;">Shipping</td>
                            <td class="text-right"><?php echo esc_html($currency_symbol); ?> <?php echo number_format($active_order['shipping_total'], 2); ?></td>
                        </tr>
                        <tr class="grand-total">
                            <td class="text-right">Grand Total</td>
                            <td class="text-right"><?php echo esc_html($currency_symbol); ?> <?php echo number_format($active_order['amount'], 2); ?></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>

        <!-- Extra Info -->
        <?php if (!empty($template_settings['wc_invoice_extra_1']) || !empty($template_settings['wc_invoice_extra_2'])): ?>
        <table style="margin-top: 45px; border-top: 1px solid #f3f4f6; padding-top: 20px;">
            <tr>
                <?php if (!empty($template_settings['wc_invoice_extra_1'])): ?>
                <td width="50%" class="info-block" style="padding-left: 0;">
                    <span class="label">Terms & Conditions</span>
                    <div class="value" style="font-size: 11px; color: #6b7280;">
                        <?php echo nl2br(esc_html($template_settings['wc_invoice_extra_1'])); ?>
                    </div>
                </td>
                <?php endif; ?>
                <?php if (!empty($template_settings['wc_invoice_extra_2'])): ?>
                <td width="50%" class="info-block" style="padding-right: 0;">
                    <span class="label">Payment Instructions</span>
                    <div class="value" style="font-size: 11px; color: #6b7280;">
                        <?php echo nl2br(esc_html($template_settings['wc_invoice_extra_2'])); ?>
                    </div>
                </td>
                <?php endif; ?>
            </tr>
        </table>
        <?php endif; ?>

        <!-- Footer -->
        <div class="footer">
            <p style="font-style: italic; margin-bottom: 12px;"><?php echo esc_html($footer_text); ?></p>
            <p style="font-weight: bold; color: #6b7280; font-size: 13px;">
                Thank you for choosing <?php echo esc_html($shop_name); ?>!
            </p>
        </div>
    </div>
</body>
</html>