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

/** @var WC_Order $order */

if (!isset($order) || !$order instanceof WC_Order) {
    return;
}

// Fetch saved settings with defaults
$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_logo'               => '',
    'company_website'            => home_url(),
    'invoice_prefix'             => 'INV-',
    'invoice_start'              => '1001',
    /* translators: Default invoice footer text */
    'invoice_footer'             => esc_html__('Thank you for your business!', 'awesome-invoice-delivery-notes-packing-slips'),
    'invoice_terms_conditions'   => '',
    'style_font_family'          => 'Arial, sans-serif',
    'style_font_size'            => '14px',
    'style_text_color'           => '#333333',
    'style_background'           => '#ffffff',
    'style_header_color'         => '#0073aa',
    'style_footer_color'         => '#333333',
    'logo_placement'             => 'left', // left, center, right
    'show_sku'                   => 1,
    'enable_tax'                 => 0,
    'enable_discount'            => 0,
]);
?>
<!DOCTYPE html>
<html lang="<?php echo esc_attr(get_bloginfo('language')); ?>">
<head>
<meta charset="utf-8">
<title>
<?php
/* translators: %1$s: invoice prefix, %2$s: order ID */
printf(
    esc_html__('%1$s #%2$s', 'awesome-invoice-delivery-notes-packing-slips'),
    esc_html($settings['invoice_prefix']),
    esc_html($order->get_id())
);
?>
</title>
<style>
body {
    font-family: <?php echo esc_attr($settings['style_font_family']); ?>;
    font-size: <?php echo esc_attr($settings['style_font_size']); ?>;
    color: <?php echo esc_attr($settings['style_text_color']); ?>;
    background: <?php echo esc_attr($settings['style_background']); ?>;
    margin: 0;
    padding: 20px;
    line-height: 1.6;
}

.invoice-wrapper {
    max-width: 950px;
    margin: auto;
    background: #fff;
    padding: 35px;
    border-radius: 12px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.08);
}

.print-btn {
    background: <?php echo esc_attr($settings['style_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: <?php
        echo $settings['logo_placement'] === 'center' ? 'center' : ($settings['logo_placement'] === 'right' ? 'flex-end' : 'space-between');
    ?>;
    align-items: center;
    margin-bottom: 35px;
    border-bottom: 3px solid <?php echo esc_attr($settings['style_header_color']); ?>;
    padding-bottom: 15px;
}
.invoice-header img { max-height: 130px; margin-right: 10px; }
.invoice-header h1 {
    color: <?php echo esc_attr($settings['style_header_color']); ?>;
    margin: 0;
    font-size: 30px;
    text-align: right;
}

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

.invoice-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 10px;
    font-size: 15px;
}
.invoice-table caption {
    caption-side: top;
    text-align: left;
    font-weight: bold;
    font-size: 18px;
    margin-bottom: 10px;
}
.invoice-table th,
.invoice-table td {
    border: 1px solid #ddd;
    padding: 12px 15px;
    text-align: left;
    vertical-align: middle;
}
.invoice-table th {
    background: <?php echo esc_attr($settings['style_header_color']); ?>;
    color: #fff;
    font-weight: bold;
}
.invoice-table th[scope="col"] {
    text-align: left;
}
.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['style_header_color']); ?>;
    border-radius: 6px;
    margin-top: 25px;
    font-size: 15px;
    white-space: pre-line;
}

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

/* Print view */
@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" type="button" onclick="window.print();">
    <?php /* translators: Button text to print invoice */ ?>
    <?php esc_html_e('Print Invoice', 'awesome-invoice-delivery-notes-packing-slips'); ?>
</button>

<div class="invoice-wrapper">

    <div class="invoice-header">
        <div class="company-info">
            <?php if (!empty($settings['company_logo'])) : ?>
                <img src="<?php echo esc_url($settings['company_logo']); ?>" alt="<?php echo esc_attr($settings['company_name'] ?: 'Company Logo'); ?>">
            <?php endif; ?>
            <p><strong><?php echo esc_html($settings['company_name']); ?></strong></p>
            <p><?php echo wp_kses_post($settings['company_address']); ?></p>
            <?php if (!empty($settings['company_phone'])) : ?>
                <p><strong><?php /* translators: Company phone label */ esc_html_e('Phone:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($settings['company_phone']); ?></p>
            <?php endif; ?>
            <?php if (!empty($settings['company_email'])) : ?>
                <p><strong><?php /* translators: Company email label */ esc_html_e('Email:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($settings['company_email']); ?></p>
            <?php endif; ?>
            <?php if (!empty($settings['company_website'])) : ?>
                <p><strong><?php /* translators: Company website label */ esc_html_e('Website:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <a href="<?php echo esc_url($settings['company_website']); ?>"><?php echo esc_url($settings['company_website']); ?></a></p>
            <?php endif; ?>
        </div>
        <h1>
            <?php
            /* translators: %1$s: invoice prefix, %2$s: order ID */
            printf(
                esc_html__('%1$s #%2$s', 'awesome-invoice-delivery-notes-packing-slips'),
                esc_html($settings['invoice_prefix']),
                esc_html($order->get_id())
            );
            ?>
        </h1>
    </div>

    <div class="addresses">
        <div class="address-box">
            <h3><?php /* translators: Billing address title */ esc_html_e('Billing Address', 'awesome-invoice-delivery-notes-packing-slips'); ?></h3>
            <address><?php echo wp_kses_post($order->get_formatted_billing_address()); ?></address>
            <p><strong><?php /* translators: Billing email label */ esc_html_e('Email:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_billing_email()); ?></p>
            <p><strong><?php /* translators: Billing phone label */ esc_html_e('Phone:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_billing_phone()); ?></p>
        </div>
        <div class="address-box">
            <h3><?php /* translators: Shipping address title */ esc_html_e('Shipping Address', 'awesome-invoice-delivery-notes-packing-slips'); ?></h3>
            <address><?php echo wp_kses_post($order->get_formatted_shipping_address()); ?></address>
        </div>
    </div>

    <div class="invoice-info">
        <p><strong><?php /* translators: Invoice date label */ esc_html_e('Invoice Date:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html(wc_format_datetime($order->get_date_created(), get_option('date_format'))); ?></p>
        <p><strong><?php /* translators: Payment method label */ esc_html_e('Payment Method:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_payment_method_title()); ?></p>
        <p><strong><?php /* translators: Shipping method label */ esc_html_e('Shipping Method:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_shipping_method()); ?></p>
    </div>

    <table class="invoice-table">
        <caption><?php /* translators: Order items table caption */ esc_html_e('Order Items', 'awesome-invoice-delivery-notes-packing-slips'); ?></caption>
        <thead>
            <tr>
                <th scope="col"><?php esc_html_e('Product', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <?php if ($settings['show_sku']) : ?>
                    <th scope="col"><?php esc_html_e('SKU', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <?php endif; ?>
                <th scope="col"><?php esc_html_e('Qty', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <th scope="col"><?php esc_html_e('Unit Price', 'awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <th scope="col"><?php esc_html_e('Subtotal', 'awesome-invoice-delivery-notes-packing-slips'); ?></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; ?>"><?php esc_html_e('Subtotal:', 'awesome-invoice-delivery-notes-packing-slips'); ?></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; ?>"><?php esc_html_e('Tax:', 'awesome-invoice-delivery-notes-packing-slips'); ?></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; ?>"><?php esc_html_e('Discount:', 'awesome-invoice-delivery-notes-packing-slips'); ?></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; ?>"><?php esc_html_e('Total:', 'awesome-invoice-delivery-notes-packing-slips'); ?></td>
                <td><?php echo wp_kses_post($order->get_formatted_order_total()); ?></td>
            </tr>
        </tfoot>
    </table>

    <?php if ($order->get_customer_note() || !empty($settings['invoice_terms_conditions'])) : ?>
    <div class="invoice-notes">
        <?php if ($order->get_customer_note()) : ?>
            <strong><?php /* translators: Customer note label */ esc_html_e('Customer Notes:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong>
            <p><?php echo wp_kses_post($order->get_customer_note()); ?></p>
        <?php endif; ?>
        <?php if (!empty($settings['invoice_terms_conditions'])) : ?>
            <strong><?php /* translators: Terms and conditions label */ esc_html_e('Terms & Conditions:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong>
            <p><?php echo wp_kses_post($settings['invoice_terms_conditions']); ?></p>
        <?php endif; ?>
    </div>
    <?php endif; ?>

    <div class="invoice-footer">
        <?php echo esc_html($settings['invoice_footer']); ?><br>
        <?php
        /* translators: %s: site name */
        printf(
            esc_html__('Generated by %s.', 'awesome-invoice-delivery-notes-packing-slips'),
            esc_html(get_bloginfo('name'))
        );
        ?>
    </div>

</div>
</body>
</html>
