<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/** @var WC_Order $order */

// Load settings from general settings page
$settings = wp_parse_args( get_option('aipd_general_settings', []), [
    'company_name'       => get_bloginfo('name'),
    'company_email'      => get_option('admin_email'),
    'company_phone'      => '',
    'company_address'    => '',
    'company_website'    => home_url(),
    'company_logo'       => '',
    'header_color'       => '#0073aa',
    'footer_color'       => '#777',
    'footer_text'        => 'Thank you for your order!',
    'font_family'        => 'Helvetica, Arial, sans-serif',
    'font_size'          => '14px',
    'text_color'         => '#333',
    'background_color'   => '#f7f7f7',
    'show_sku'           => 1,
    'enable_tax'         => 1,
    'enable_discount'    => 1,
]);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Invoice #<?php echo esc_html( $order->get_id() ); ?></title>
<style>
body {
    font-family: <?php echo esc_attr($settings['font_family']); ?>;
    font-size: <?php echo esc_attr($settings['font_size']); ?>;
    color: <?php echo esc_attr($settings['text_color']); ?>;
    background: <?php echo esc_attr($settings['background_color']); ?>;
    margin:0; padding:20px;
}
.invoice-wrapper {
    max-width:950px;
    margin:auto;
    background:#fff;
    padding:35px;
    border-radius:12px;
    box-shadow:0 6px 25px rgba(0,0,0,0.1);
}
.print-btn {
    background: <?php echo esc_attr($settings['header_color']); ?>;
    color:#fff;
    border:none;
    padding:12px 25px;
    border-radius:6px;
    cursor:pointer;
    margin-bottom:25px;
    font-size:16px;
}
.print-btn:hover { opacity:0.9; }

.invoice-header {
    display:flex;
    justify-content:space-between;
    align-items:center;
    margin-bottom:35px;
}
.invoice-header img { max-height:130px; }
.invoice-header h1 { color: <?php echo esc_attr($settings['header_color']); ?>; font-size:32px; margin:0; }

.addresses { display:flex; justify-content:space-between; gap:20px; flex-wrap:wrap; margin-bottom:30px; }
.address-box { width:48%; background:#f9f9f9; padding:20px; border-radius:10px; box-sizing:border-box; }
.address-box h3 { margin-top:0; margin-bottom:12px; color: <?php echo esc_attr($settings['header_color']); ?>; font-size:17px; }

.invoice-info p { margin:6px 0; font-size:15px; }
.table-wrapper { overflow-x:auto; }
.invoice-table { width:100%; border-collapse:collapse; margin-top:15px; font-size:15px; }
.invoice-table th, .invoice-table td { border:1px solid #ddd; padding:12px; text-align:left; vertical-align:middle; }
.invoice-table th { background: <?php echo esc_attr($settings['header_color']); ?>; color:#fff; font-weight:bold; }
.invoice-table tbody tr:nth-child(even){ background:#f9f9f9; }
.invoice-table tbody tr:hover { background:#e6f7ff; }
.total-row td { font-weight:bold; text-align:right; background:#f0f8ff; }

.invoice-notes {
    background:#f0f8ff;
    padding:18px;
    border-left:5px solid <?php echo esc_attr($settings['header_color']); ?>;
    border-radius:6px;
    margin-top:25px;
    white-space:pre-line;
    font-size:15px;
}

.invoice-footer {
    text-align:center;
    margin-top:45px;
    font-size:13px;
    color: <?php echo esc_attr($settings['footer_color']); ?>;
}

@media print {
    .print-btn { display:none; }
    body { background:#fff; }
    .addresses { flex-direction:column !important; }
    .address-box { width:100% !important; margin-bottom:15px; }
}
</style>
</head>
<body>

<button class="print-btn" onclick="window.print();">Print Invoice</button>

<div class="invoice-wrapper">

    <div class="invoice-header">
        <div class="company-info">
            <?php if($settings['company_logo']): ?>
                <img src="<?php echo esc_url($settings['company_logo']); ?>" alt="Logo">
            <?php endif; ?>
            <p><strong><?php echo esc_html($settings['company_name']); ?></strong></p>
            <p><?php echo esc_html($settings['company_address']); ?></p>
            <?php if($settings['company_phone']): ?><p>Phone: <?php echo esc_html($settings['company_phone']); ?></p><?php endif; ?>
            <?php if($settings['company_email']): ?><p>Email: <?php echo esc_html($settings['company_email']); ?></p><?php endif; ?>
            <?php if($settings['company_website']): ?><p>Website: <a href="<?php echo esc_url($settings['company_website']); ?>" target="_blank"><?php echo esc_html($settings['company_website']); ?></a></p><?php endif; ?>
        </div>
        <h1>Invoice #<?php echo esc_html($order->get_id()); ?></h1>
    </div>

    <div class="addresses">
        <div class="address-box">
            <h3>Billing Address</h3>
            <p><?php echo wp_kses_post($order->get_formatted_billing_address()); ?></p>
            <p><strong>Email:</strong> <?php echo esc_html($order->get_billing_email()); ?></p>
            <p><strong>Phone:</strong> <?php echo esc_html($order->get_billing_phone()); ?></p>
        </div>
        <div class="address-box">
            <h3>Shipping Address</h3>
            <p><?php echo wp_kses_post($order->get_formatted_shipping_address()); ?></p>
        </div>
    </div>

    <div class="invoice-info">
        <p><strong>Invoice Date:</strong> <?php echo esc_html(wc_format_datetime($order->get_date_created())); ?></p>
        <p><strong>Payment Method:</strong> <?php echo esc_html($order->get_payment_method_title()); ?></p>
        <p><strong>Shipping Method:</strong> <?php echo esc_html($order->get_shipping_method()); ?></p>
    </div>

    <div class="table-wrapper">
        <table class="invoice-table">
            <thead>
                <tr>
                    <th>Product</th>
                    <?php if($settings['show_sku']): ?><th>SKU</th><?php endif; ?>
                    <th>Qty</th>
                    <th>Unit Price</th>
                    <th>Subtotal</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach($order->get_items() as $item):
                    $product = $item->get_product();
                    $sku = $product ? $product->get_sku() : '';
                ?>
                <tr>
                    <td><?php echo esc_html($item->get_name()); ?></td>
                    <?php if($settings['show_sku']): ?><td><?php echo esc_html($sku); ?></td><?php endif; ?>
                    <td><?php echo esc_html($item->get_quantity()); ?></td>
                    <td><?php echo wp_kses_post(wc_price($order->get_item_total($item,true))); ?></td>
                    <td><?php echo wp_kses_post(wc_price($item->get_total())); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr class="total-row">
                    <td colspan="<?php echo $settings['show_sku'] ? 4 : 3; ?>">Subtotal:</td>
                    <td><?php echo wp_kses_post(wc_price($order->get_subtotal())); ?></td>
                </tr>
                <?php if($settings['enable_tax'] && $order->get_total_tax() > 0): ?>
                <tr class="total-row">
                    <td colspan="<?php echo $settings['show_sku'] ? 4 : 3; ?>">Tax:</td>
                    <td><?php echo wp_kses_post(wc_price($order->get_total_tax())); ?></td>
                </tr>
                <?php endif; ?>
                <?php if($settings['enable_discount'] && $order->get_discount_total() > 0): ?>
                <tr class="total-row">
                    <td colspan="<?php echo $settings['show_sku'] ? 4 : 3; ?>">Discount:</td>
                    <td>- <?php echo wp_kses_post(wc_price($order->get_discount_total())); ?></td>
                </tr>
                <?php endif; ?>
                <tr class="total-row">
                    <td colspan="<?php echo $settings['show_sku'] ? 4 : 3; ?>">Total:</td>
                    <td><?php echo wp_kses_post($order->get_formatted_order_total()); ?></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <?php if($order->get_customer_note()): ?>
    <div class="invoice-notes">
        <strong>Customer Notes:</strong>
        <p><?php echo esc_html($order->get_customer_note()); ?></p>
    </div>
    <?php endif; ?>

    <div class="invoice-footer">
        <?php echo esc_html($settings['footer_text']); ?><br>
        Generated by <?php bloginfo('name'); ?>.
    </div>

</div>

</body>
</html>
