<?php

if (!defined('ABSPATH')) { exit; }

$currency_sym = function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol($totals['currency']) : '€';
$money = static function ($n) use ($currency_sym) {
    return number_format((float) $n, 2, ',', '.') . ' ' . $currency_sym;
};
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title><?php echo esc_html($doc_label . ' ' . $doc_number); ?></title>
<style type="text/css"><?php include __DIR__ . '/style.css'; ?></style>
</head>
<body class="<?php echo esc_attr($doc_type); ?>">

<table class="head container">
    <tr>
        <td class="header">
            <?php if (!empty($shop['logo_url'])): ?>
                <img src="<?php echo esc_url($shop['logo_url']); ?>" alt="">
            <?php else: ?>
                <?php echo esc_html(strtoupper(__('Proforma', 'ai-eshop-optimizer'))); ?>
            <?php endif; ?>
        </td>
        <td class="shop-info">
            <div class="shop-name"><h3><?php echo esc_html($shop['name']); ?></h3></div>
            <div class="shop-address">
                <?php echo esc_html(trim($shop['address_1'] . ' ' . $shop['address_2'])); ?><br>
                <?php echo esc_html(trim($shop['postcode'] . ' ' . $shop['city'])); ?>
                <?php if ($shop['country']): ?>, <?php echo esc_html($shop['country']); ?><?php endif; ?>
                <?php if ($shop['phone']): ?><br><?php echo esc_html(__('Tel.', 'ai-eshop-optimizer') . ' ' . $shop['phone']); ?><?php endif; ?>
                <?php if ($shop['email']): ?><br><?php echo esc_html($shop['email']); ?><?php endif; ?>
                <?php if ($shop['vat']): ?><br><?php echo esc_html(__('VAT:', 'ai-eshop-optimizer') . ' ' . $shop['vat']); ?><?php endif; ?>
            </div>
        </td>
    </tr>
</table>

<h1 class="document-type-label">
    <?php if (!empty($shop['logo_url'])) echo esc_html(strtoupper(__('Proforma', 'ai-eshop-optimizer'))); ?>
</h1>

<table class="order-data-addresses">
    <tr>
        <td class="address billing-address">
            <?php
            $billing = $order->get_formatted_billing_address();
            echo wp_kses_post($billing ?: ($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()));
            if ($order->get_billing_email()) { echo '<div class="billing-email">' . esc_html($order->get_billing_email()) . '</div>'; }
            if ($order->get_billing_phone()) { echo '<div class="billing-phone">' . esc_html($order->get_billing_phone()) . '</div>'; }
            ?>
        </td>
        <td class="address shipping-address">
            <?php $shipping = $order->get_formatted_shipping_address(); if ($shipping && $shipping !== $order->get_formatted_billing_address()): ?>
                <h3><?php esc_html_e('Ship To:', 'ai-eshop-optimizer'); ?></h3>
                <?php echo wp_kses_post($shipping); ?>
            <?php endif; ?>
        </td>
        <td class="order-data">
            <table>
                <tr class="order-number">
                    <th><?php esc_html_e('Document #:', 'ai-eshop-optimizer'); ?></th>
                    <td><?php echo esc_html($doc_number); ?></td>
                </tr>
                <tr class="order-date">
                    <th><?php esc_html_e('Date:', 'ai-eshop-optimizer'); ?></th>
                    <td><?php echo esc_html(mysql2date(get_option('date_format'), $doc_date)); ?></td>
                </tr>
                <tr>
                    <th><?php esc_html_e('Order:', 'ai-eshop-optimizer'); ?></th>
                    <td>#<?php echo esc_html((string) $order->get_order_number()); ?></td>
                </tr>
                <?php if ($order->get_payment_method_title()): ?>
                <tr class="payment-method">
                    <th><?php esc_html_e('Payment:', 'ai-eshop-optimizer'); ?></th>
                    <td><?php echo esc_html($order->get_payment_method_title()); ?></td>
                </tr>
                <?php endif; ?>
            </table>
        </td>
    </tr>
</table>

<table class="order-details">
    <thead>
        <tr>
            <th class="product"><span><?php esc_html_e('Description', 'ai-eshop-optimizer'); ?></span></th>
            <th class="quantity"><span><?php esc_html_e('Qty', 'ai-eshop-optimizer'); ?></span></th>
            <th class="price"><span><?php esc_html_e('Price', 'ai-eshop-optimizer'); ?></span></th>
            <th class="price"><span><?php esc_html_e('Total', 'ai-eshop-optimizer'); ?></span></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($lines as $line): ?>
        <tr>
            <td class="product">
                <span class="item-name"><?php echo esc_html($line['name']); ?></span>
                <dl class="meta">
                    <?php if ($line['sku']): ?><dt class="sku">SKU:</dt><dd class="sku"><?php echo esc_html($line['sku']); ?></dd><?php endif; ?>
                </dl>
                <?php if ($line['gtin']): ?><span class="gtin-line">GTIN: <?php echo esc_html($line['gtin']); ?></span><?php endif; ?>
            </td>
            <td class="quantity"><span><?php echo esc_html((string) $line['qty']); ?></span></td>
            <td class="price"><span><?php echo esc_html($money($line['price'])); ?></span></td>
            <td class="price"><span><?php echo esc_html($money($line['total'])); ?></span></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
    <tfoot>
        <tr class="no-borders">
            <td class="no-borders" colspan="2">
                <?php if ($order->get_customer_note()): ?>
                <div class="customer-notes">
                    <h3><?php esc_html_e('Notes', 'ai-eshop-optimizer'); ?></h3>
                    <p><?php echo esc_html($order->get_customer_note()); ?></p>
                </div>
                <?php endif; ?>
            </td>
            <td class="no-borders" colspan="2">
                <table class="totals">
                    <tfoot>
                        <tr><th class="description"><?php esc_html_e('Subtotal', 'ai-eshop-optimizer'); ?></th><td class="price"><?php echo esc_html($money($totals['subtotal'])); ?></td></tr>
                        <?php if ($totals['discount'] > 0): ?>
                        <tr><th class="description"><?php esc_html_e('Discount', 'ai-eshop-optimizer'); ?></th><td class="price">−<?php echo esc_html($money($totals['discount'])); ?></td></tr>
                        <?php endif; ?>
                        <?php if ($totals['shipping'] > 0): ?>
                        <tr><th class="description"><?php esc_html_e('Shipping', 'ai-eshop-optimizer'); ?></th><td class="price"><?php echo esc_html($money($totals['shipping'])); ?></td></tr>
                        <?php endif; ?>
                        <?php if ($totals['tax'] > 0): ?>
                        <tr><th class="description"><?php esc_html_e('VAT', 'ai-eshop-optimizer'); ?></th><td class="price"><?php echo esc_html($money($totals['tax'])); ?></td></tr>
                        <?php endif; ?>
                        <tr class="order_total"><th class="description"><?php esc_html_e('Total', 'ai-eshop-optimizer'); ?></th><td class="price"><?php echo esc_html($money($totals['total'])); ?></td></tr>
                    </tfoot>
                </table>
            </td>
        </tr>
    </tfoot>
</table>

<div class="bottom-spacer"></div>

<div id="footer">
    <small><?php
        
        echo esc_html(sprintf(__('Issued by %s', 'ai-eshop-optimizer'), $shop['name']));
    ?></small>
</div>

</body>
</html>
