<?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 lang="<?php echo esc_attr($language); ?>">
<head>
<meta charset="UTF-8">
<title><?php echo esc_html($doc_label . ' ' . $doc_number); ?></title>
<style><?php include __DIR__ . '/style.css'; ?></style>
</head>
<body>

<div class="ff-doc">

  <header class="ff-header">
    <div class="ff-shop">
      <?php if (!empty($shop['logo_url'])): ?>
        <img class="ff-logo" src="<?php echo esc_url($shop['logo_url']); ?>" alt="">
      <?php endif; ?>
      <div class="ff-shop-name"><?php echo esc_html($shop['name']); ?></div>
      <div class="ff-shop-addr">
        <?php echo esc_html(trim($shop['address_1'] . ' ' . $shop['address_2'])); ?><br>
        <?php echo esc_html(trim($shop['postcode'] . ' ' . $shop['city'] . ', ' . $shop['country'])); ?>
        <?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>
    </div>
    <div class="ff-doc-meta">
      <h1><?php echo esc_html(strtoupper(__('Proforma', 'ai-eshop-optimizer'))); ?></h1>
      <table>
        <tr><th><?php esc_html_e('Number', 'ai-eshop-optimizer'); ?></th><td><?php echo esc_html($doc_number); ?></td></tr>
        <tr><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>
      </table>
    </div>
  </header>

  <section class="ff-addresses">
    <div class="ff-addr">
      <h3><?php esc_html_e('Bill to', 'ai-eshop-optimizer'); ?></h3>
      <?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_phone()) { echo '<br>' . esc_html($order->get_billing_phone()); }
      if ($order->get_billing_email()) { echo '<br>' . esc_html($order->get_billing_email()); }
      ?>
    </div>
    <?php if ($order->get_formatted_shipping_address() && $order->get_formatted_shipping_address() !== $order->get_formatted_billing_address()): ?>
    <div class="ff-addr">
      <h3><?php esc_html_e('Ship to', 'ai-eshop-optimizer'); ?></h3>
      <?php echo wp_kses_post($order->get_formatted_shipping_address()); ?>
    </div>
    <?php endif; ?>
  </section>

  <table class="ff-lines">
    <thead>
      <tr>
        <th class="col-desc"><?php esc_html_e('Description', 'ai-eshop-optimizer'); ?></th>
        <th class="col-qty"><?php esc_html_e('Qty', 'ai-eshop-optimizer'); ?></th>
        <th class="col-price"><?php esc_html_e('Price', 'ai-eshop-optimizer'); ?></th>
        <th class="col-total"><?php esc_html_e('Total', 'ai-eshop-optimizer'); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($lines as $line): ?>
        <tr>
          <td class="col-desc">
            <strong><?php echo esc_html($line['name']); ?></strong>
            <?php if ($line['sku']): ?><br><small>SKU: <?php echo esc_html($line['sku']); ?></small><?php endif; ?>
            <?php if ($line['gtin']): ?><br><small>GTIN: <?php echo esc_html($line['gtin']); ?></small><?php endif; ?>
          </td>
          <td class="col-qty"><?php echo esc_html((string) $line['qty']); ?></td>
          <td class="col-price"><?php echo esc_html($money($line['qty'] > 0 ? $line['subtotal'] / $line['qty'] : $line['subtotal'])); ?></td>
          <td class="col-total"><?php echo esc_html($money($line['total'])); ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <table class="ff-totals">
    <tr><th><?php esc_html_e('Subtotal', 'ai-eshop-optimizer'); ?></th><td><?php echo esc_html($money($totals['subtotal'])); ?></td></tr>
    <?php if ($totals['discount'] > 0): ?>
    <tr><th><?php esc_html_e('Discount', 'ai-eshop-optimizer'); ?></th><td>−<?php echo esc_html($money($totals['discount'])); ?></td></tr>
    <?php endif; ?>
    <?php if ($totals['shipping'] > 0): ?>
    <tr><th><?php esc_html_e('Shipping', 'ai-eshop-optimizer'); ?></th><td><?php echo esc_html($money($totals['shipping'])); ?></td></tr>
    <?php endif; ?>
    <?php if ($totals['tax'] > 0): ?>
    <tr><th><?php esc_html_e('VAT', 'ai-eshop-optimizer'); ?></th><td><?php echo esc_html($money($totals['tax'])); ?></td></tr>
    <?php endif; ?>
    <tr class="ff-grand-total"><th><?php esc_html_e('Total', 'ai-eshop-optimizer'); ?></th><td><?php echo esc_html($money($totals['total'])); ?></td></tr>
  </table>

  <?php if ($order->get_customer_note()): ?>
  <section class="ff-notes">
    <h3><?php esc_html_e('Customer notes', 'ai-eshop-optimizer'); ?></h3>
    <p><?php echo esc_html($order->get_customer_note()); ?></p>
  </section>
  <?php endif; ?>

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

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