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

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

// Fetch 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,
    'logo_placement'  => 'space-between', // left, center, right, or space-between
    'font_family'     => "'Segoe UI', Tahoma, Geneva, Verdana, sans-serif",
    'font_size'       => '14px',
    'text_color'      => '#333',
    'header_color'    => '#0073aa',
    'bg_color'        => '#f7f7f7',
]);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php
$title = sprintf( esc_html__( 'Package #%s', 'awesome-invoice-delivery-notes-packing-slips' ), esc_html( $order->get_id() ) );
?>
<title><?php echo esc_html($title); ?></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: 0;
    padding: 20px;
    font-size: <?php echo esc_attr($settings['font_size']); ?>;
    line-height: 1.5;
}
.container {
    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['header_color']); ?>;
    color: #fff;
    border: none;
    padding: 12px 25px;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 25px;
    font-size: 16px;
}
.print-btn:hover { background: <?php echo esc_attr($settings['header_color']); ?>cc; }

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

/* Customer Info */
.customer-info {
    display: flex;
    justify-content: space-between;
    gap: 20px;
    margin-bottom: 25px;
    flex-wrap: wrap;
}
.customer-info .billing, .customer-info .shipping {
    width: 48%;
    background: #f9f9f9;
    padding: 18px;
    border-radius: 10px;
    box-sizing: border-box;
}
.customer-info h3 { margin-top:0; margin-bottom:12px; color: <?php echo esc_attr($settings['header_color']); ?>; }

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

/* Notes */
.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;
    white-space: pre-line;
}

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

/* Responsive */
@media print {
    .print-btn { display:none; }
    .customer-info { flex-direction: column; }
    .customer-info .billing, .customer-info .shipping { width: 100%; }
}
</style>
</head>
<body>

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

<div class="container">

    <!-- Header -->
    <div class="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']); ?>">
            <?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 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 esc_html_e('Email:','awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($settings['company_email']); ?></p><?php endif; ?>
        </div>
        <h1><?php printf( esc_html__('Package #%s','awesome-invoice-delivery-notes-packing-slips'), esc_html($order->get_id()) ); ?></h1>
    </div>

    <!-- Customer Info -->
    <div class="customer-info">
        <div class="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="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>
            <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>
    </div>

    <!-- Items Table -->
    <table class="table">
        <thead>
            <tr>
                <th><?php esc_html_e('Product','awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <?php if(!empty($settings['show_sku'])): ?><th><?php esc_html_e('SKU','awesome-invoice-delivery-notes-packing-slips'); ?></th><?php endif; ?>
                <th><?php esc_html_e('Qty','awesome-invoice-delivery-notes-packing-slips'); ?></th>
                <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() : '';
                $variation = '';
                if($product && $product->is_type('variation')) {
                    $variation = wc_get_formatted_variation($product, true, false);
                }
            ?>
            <tr>
                <td><?php echo esc_html($item->get_name() . ' ' . $variation); ?></td>
                <?php if(!empty($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>
            <?php foreach($order->get_order_item_totals() as $label => $total_row): ?>
            <tr>
                <td colspan="<?php echo !empty($settings['show_sku']) ? 4 : 3; ?>" style="text-align:right;"><?php echo esc_html($total_row['label']); ?></td>
                <td><?php echo wp_kses_post($total_row['value']); ?></td>
            </tr>
            <?php endforeach; ?>
        </tfoot>
    </table>

    <!-- Customer Notes -->
    <?php if($order->get_customer_note()): ?>
    <div class="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; ?>

    <!-- Footer -->
    <div class="footer">
        <?php if(!empty($settings['footer_text'])) 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>
