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

// Ensure $order is valid
if (!isset($order) || !$order instanceof WC_Order) return;

// Fetch general settings
$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'       => '',
    'footer_text'        => 'Thank you for your order!',
    'show_sku'           => 1,
    'font_family'        => "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
    'font_size'          => '15px',
    'text_color'         => '#333',
    'header_color'       => '#0073aa',
    'bg_color'           => '#f7f7f7',
    'invoice_prefix'     => 'INV-',
    'terms_conditions'   => '',
]);

$invoice_number = $settings['invoice_prefix'] . $order->get_id();
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo esc_html(sprintf(__('Delivery Note #%s','awesome-invoice-delivery-notes-packing-slips'), $invoice_number)); ?></title>
<style>
body {
    font-family: <?php echo esc_attr($settings['font_family']); ?>;
    color: <?php echo esc_attr($settings['text_color']); ?>;
    background: <?php echo esc_attr($settings['bg_color']); ?>;
    margin: 20px;
    font-size: <?php echo esc_attr($settings['font_size']); ?>;
    line-height: 1.5;
}
.delivery-note-container {
    max-width: 950px;
    margin: auto;
    background: #fff;
    padding: 35px;
    border-radius: 12px;
    box-shadow: 0 6px 20px rgba(0,0,0,0.08);
}
.delivery-note-print {
    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;
}
.delivery-note-print:hover { background: <?php echo esc_attr($settings['header_color']); ?>cc; }

/* Header */
.delivery-note-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 35px;
    border-bottom: 3px solid <?php echo esc_attr($settings['header_color']); ?>;
    padding-bottom: 15px;
}
.delivery-note-header img { max-height: 130px; }
.delivery-note-header h1 {
    color: <?php echo esc_attr($settings['header_color']); ?>;
    margin: 0;
    font-size: 30px;
    text-align: right;
}

/* Sections */
.delivery-note-section { margin-bottom: 30px; }
.delivery-note-section strong { color: <?php echo esc_attr($settings['header_color']); ?>; }
.delivery-note-billing-shipping { display: flex; justify-content: space-between; gap: 25px; flex-wrap: wrap; }
.delivery-note-billing, .delivery-note-shipping {
    width: 48%;
    background: #f9f9f9;
    padding: 18px;
    border-radius: 10px;
    box-sizing: border-box;
}
.delivery-note-billing h3, .delivery-note-shipping h3 { margin-top:0; margin-bottom:12px; color: <?php echo esc_attr($settings['header_color']); ?>; font-size:17px; }

/* Table */
.delivery-note-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
    font-size: 15px;
}
.delivery-note-table th, .delivery-note-table td {
    border: 1px solid #ddd;
    padding: 12px 15px;
    text-align: left;
    vertical-align: middle;
}
.delivery-note-table th {
    background: <?php echo esc_attr($settings['header_color']); ?>;
    color: #fff;
    font-weight: bold;
}
.delivery-note-table tbody tr:nth-child(even){ background: #f9f9f9; }
.delivery-note-table tbody tr:hover { background: #e6f7ff; }
.delivery-note-table tfoot td { font-weight: bold; font-size: 16px; background: #f0f8ff; }

/* Notes / Terms */
.delivery-note-notes {
    background: #f0f8ff;
    padding: 18px;
    border-left: 5px solid <?php echo esc_attr($settings['header_color']); ?>;
    border-radius: 6px;
    margin-top: 25px;
    font-size: 15px;
    line-height: 1.6;
}

/* Footer */
.delivery-note-footer {
    text-align:center;
    margin-top: 45px;
    font-size:14px;
    color:#777;
}

/* Responsive */
@media print { 
    .delivery-note-print { display:none; } 
    body { background:#fff; } 
    .delivery-note-billing-shipping { flex-direction: column; } 
    .delivery-note-billing, .delivery-note-shipping { width: 100%; } 
}
</style>
</head>
<body>

<button class="delivery-note-print" onclick="window.print();"><?php esc_html_e('Print Delivery Note','awesome-invoice-delivery-notes-packing-slips'); ?></button>

<div class="delivery-note-container">

    <!-- Header -->
    <div class="delivery-note-header">
        <div class="company-info">
            <?php if($settings['company_logo']): ?>
                <img src="<?php echo esc_url($settings['company_logo']); ?>" alt="<?php echo esc_attr($settings['company_name']); ?>">
            <?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($settings['company_phone']): ?><p><strong><?php esc_html_e('Phone:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($settings['company_phone']); ?></p><?php endif; ?>
            <?php if($settings['company_email']): ?><p><strong><?php esc_html_e('Email:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($settings['company_email']); ?></p><?php endif; ?>
        </div>

        <h1><?php echo esc_html(sprintf(__('Delivery Note #%s','awesome-invoice-delivery-notes-packing-slips'), $invoice_number)); ?></h1>
    </div>

    <!-- Order Info -->
    <div class="delivery-note-section">
        <p><strong><?php esc_html_e('Order Date:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html(wc_format_datetime($order->get_date_created())); ?></p>
        <p><strong><?php 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 esc_html_e('Shipping Method:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_shipping_method()); ?></p>
    </div>

    <!-- Billing & Shipping -->
    <div class="delivery-note-section delivery-note-billing-shipping">
        <div class="delivery-note-billing">
            <h3><?php esc_html_e('Billing Address','awesome-invoice-delivery-notes-packing-slips'); ?></h3>
            <p><?php echo wp_kses_post($order->get_formatted_billing_address()); ?></p>
            <p><strong><?php esc_html_e('Email:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_billing_email()); ?></p>
            <p><strong><?php esc_html_e('Phone:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_billing_phone()); ?></p>
        </div>
        <div class="delivery-note-shipping">
            <h3><?php esc_html_e('Shipping Address','awesome-invoice-delivery-notes-packing-slips'); ?></h3>
            <p><?php echo wp_kses_post($order->get_formatted_shipping_address()); ?></p>
        </div>
    </div>

    <!-- Items Table -->
    <table class="delivery-note-table">
        <thead>
            <tr>
                <th><?php esc_html_e('Qty','awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <th><?php esc_html_e('Product','awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <?php if($settings['show_sku']): ?><th><?php esc_html_e('SKU','awesome-invoice-delivery-notes-packing-slips'); ?></th><?php endif; ?>
                <th><?php esc_html_e('Price','awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <th><?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_quantity()); ?></td>
                <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 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>
                <td colspan="<?php echo $settings['show_sku'] ? 4 : 3; ?>" style="text-align:right;"><?php esc_html_e('Total:','awesome-invoice-delivery-notes-packing-slips'); ?></td>
                <td><?php echo wp_kses_post(wc_price($order->get_total())); ?></td>
            </tr>
        </tfoot>
    </table>

    <!-- Customer Notes -->
    <?php if($order->get_customer_note()): ?>
    <div class="delivery-note-notes">
        <strong><?php esc_html_e('Customer Notes:','awesome-invoice-delivery-notes-packing-slips'); ?></strong>
        <p><?php echo wp_kses_post($order->get_customer_note()); ?></p>
    </div>
    <?php endif; ?>

    <!-- Terms & Conditions -->
    <?php if(!empty($settings['terms_conditions'])): ?>
    <div class="delivery-note-notes">
        <strong><?php esc_html_e('Terms & Conditions:','awesome-invoice-delivery-notes-packing-slips'); ?></strong>
        <p><?php echo wp_kses_post($settings['terms_conditions']); ?></p>
    </div>
    <?php endif; ?>

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

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