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

// Fetch order object
if (!isset($order) || !$order instanceof WC_Order) return;

// Fetch general settings
$defaults = [
    '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',
    'invoice_footer'=>'Thank you for your business',
    'invoice_terms_conditions'=>'',
    'style_font_family'=>'Arial, sans-serif',
    'style_font_size'=>'14px',
    'style_text_color'=>'#333333',
    'style_header_color'=>'#0073aa',
    'style_background'=>'#ffffff',
    'logo_placement'=>'left',
    'show_sku'=>1
];

$settings = wp_parse_args(get_option('aipd_general_settings', []), $defaults);
$show_sku = $settings['show_sku'];
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php
$title = sprintf( esc_html__( 'Delivery Note #%s', 'awesome-invoice-delivery-notes-packing-slips' ), esc_html( $order->get_id() ) );
?>
<title><?php echo $title; ?></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:0;
}
.container {
    width:90%; max-width:1000px; margin:20px auto;
    padding:30px; background:#fff;
    box-shadow:0 0 20px rgba(0,0,0,0.05);
    border-radius:10px;
}
header {
    display:flex; justify-content:space-between; align-items:center;
    padding:20px; background:<?php echo esc_attr($settings['style_header_color']); ?>10;
    border-radius:10px 10px 0 0; margin-bottom:30px;
}
header img {
    max-height:130px;
    <?php if($settings['logo_placement'] === 'center') echo 'margin:0 auto; display:block;'; ?>
}
header .company-details { line-height:1.4; }
header h1 {
    font-size:28px; color: <?php echo esc_attr($settings['style_header_color']); ?>;
    margin:0; text-align:right;
}
.section { margin-bottom:25px; }
.section p { margin:5px 0; font-size:16px; }

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

table { width:100%; border-collapse:collapse; margin-top:15px; font-size:16px; }
th, td { border:1px solid #ccc; padding:12px 10px; text-align:left; }
th { background: <?php echo esc_attr($settings['style_header_color']); ?>20; color: <?php echo esc_attr($settings['style_header_color']); ?>; }
tbody tr:nth-child(even) { background:#f9f9f9; }
tfoot td { font-weight:bold; font-size:16px; }

.notes {
    margin-top:20px;
    padding:15px;
    background:#f4f4f4;
    border-left:4px solid <?php echo esc_attr($settings['style_header_color']); ?>;
    border-radius:4px;
}

.footer {
    text-align:center;
    margin-top:50px;
    font-size:14px;
    color: <?php echo esc_attr($settings['style_text_color']); ?>;
    padding:15px;
    background: <?php echo esc_attr($settings['style_text_color']); ?>10;
    border-radius:0 0 10px 10px;
}

.print-button {
    margin:20px auto;
    display:block;
    background: <?php echo esc_attr($settings['style_header_color']); ?>;
    color:#fff;
    padding:10px 25px;
    font-size:16px;
    border:none;
    border-radius:6px;
    cursor:pointer;
}
.print-button:hover { opacity:0.9; }

@media print {
    .print-button { display:none; }
    body { margin:0; padding:0; }
    .container { box-shadow:none; margin:0; border-radius:0; }
    .addresses { flex-direction: column !important; }
    .address-box { width:100% !important; margin-bottom: 15px; }
}
</style>
</head>
<body>

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

<div class="container">
<header>
    <?php if($settings['company_logo']): ?>
        <img src="<?php echo esc_url($settings['company_logo']); ?>" alt="<?php echo esc_attr($settings['company_name']); ?>">
    <?php endif; ?>
    <div class="company-details">
        <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 esc_html_e('Delivery Note','awesome-invoice-delivery-notes-packing-slips'); ?></h1>
</header>

<div class="section order-info">
    <p><strong><?php esc_html_e('Delivery Note #:', 'awesome-invoice-delivery-notes-packing-slips'); ?></strong> <?php echo esc_html($order->get_id()); ?></p>
    <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>

<div class="section addresses">
    <div class="address-box">
        <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="address-box">
        <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>

<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($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($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 $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>

<?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; ?>

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

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

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