<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.DirectDatabaseQuery.SchemaChange
/**
 * Minimal Invoice Template — UAE FTA VAT Compliant
 * Ultra-clean, no borders, maximum whitespace.
 */
if ( ! defined( 'ABSPATH' ) ) exit;
$primary = esc_attr( $brand_primary ?? '#244b8e' );
$accent  = esc_attr( $brand_secondary ?? '#f59e0b' );
$cur     = $currency ?? 'AED';
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<title><?php echo esc_html($label??'Invoice'); ?></title>
<?php pbuaiv_style_tag_open(); ?>
* { box-sizing:border-box; margin:0; padding:0; }
body { font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size:11px; color:#334155; background:#fff; }
.pg { max-width:794px; margin:0 auto; padding:50px; }
.hd { margin-bottom:40px; }
.hd h1 { font-size:10px; font-weight:600; text-transform:uppercase; letter-spacing:3px; color:<?php echo esc_attr( $accent ); ?>; margin-bottom:8px; }
.hd .num { font-size:28px; font-weight:200; color:<?php echo esc_attr( $primary ); ?>; }
.hd .meta { font-size:10px; color:#94a3b8; margin-top:4px; }
.party { margin-bottom:30px; }
.party-row { display:flex; gap:60px; }
.party h3 { font-size:9px; font-weight:700; text-transform:uppercase; letter-spacing:2px; color:<?php echo esc_attr( $accent ); ?>; margin-bottom:10px; }
.party p { font-size:11px; line-height:1.7; color:#475569; }
.party .name { font-size:13px; font-weight:700; color:#0f172a; margin-bottom:4px; }
.party .trn { font-size:10px; color:<?php echo esc_attr( $primary ); ?>; font-weight:600; margin-top:6px; }
table.items { width:100%; border-collapse:collapse; margin:30px 0; }
table.items th { font-size:9px; font-weight:700; text-transform:uppercase; letter-spacing:1px; color:#94a3b8; padding:0 0 8px; border-bottom:1px solid #e2e8f0; }
table.items td { padding:10px 0; border-bottom:1px solid #f1f5f9; font-size:11px; color:#334155; }
.r { text-align:right; }
.c { text-align:center; }
.totals { margin-left:auto; max-width:260px; margin-top:10px; }
.totals td { padding:4px 0; font-size:11px; }
.totals .tl { color:#94a3b8; }
.totals .final td { font-size:16px; font-weight:700; color:<?php echo esc_attr( $primary ); ?>; padding-top:10px; border-top:2px solid <?php echo esc_attr( $primary ); ?>; }
.vat-block { background:#f8fafc; border-radius:8px; padding:16px; margin:20px 0; font-size:10px; color:#64748b; }
.vat-block strong { color:<?php echo esc_attr( $primary ); ?>; }
.foot { margin-top:50px; padding-top:20px; border-top:1px solid #e2e8f0; font-size:9px; color:#cbd5e1; text-align:center; }
<?php pbuaiv_style_tag_close(); ?></head><body>
<div class="pg">
  <div class="hd">
    <?php if(!empty($company['logo_url'])): ?><img src="<?php echo esc_url($company['logo_url']); ?>" style="max-height:50px;margin-bottom:16px;" /><br><?php endif; ?>
    <h1><?php echo esc_html($label??'Tax Invoice'); ?></h1>
    <div class="num"><?php echo esc_html($doc_number??'DRAFT'); ?></div>
    <div class="meta">
      <?php echo esc_html(date_i18n(get_option('date_format'),strtotime($doc_date??wp_date('Y-m-d')))); ?>
      &nbsp;·&nbsp; Order #<?php echo esc_html($order_number??''); ?>
      &nbsp;·&nbsp; <?php echo esc_html($payment_method??''); ?>
    </div>
  </div>

  <div class="party-row">
    <div class="party">
      <h3>From</h3>
      <div class="name"><?php echo esc_html($company['name']??''); ?></div>
      <p><?php echo esc_html(implode(', ', array_filter([$company['address']??'',$company['emirate']??'','UAE']))); ?></p>
      <?php if(!empty($company['trn'])): ?><div class="trn">TRN <?php echo esc_html($company['trn']); ?></div><?php endif; ?>
    </div>
    <div class="party">
      <h3>To</h3>
      <div class="name"><?php echo esc_html($customer['name']??''); ?></div>
      <p><?php echo wp_kses_post(nl2br($customer['address']??'')); ?></p>
      <?php if(!empty($customer['trn'])): ?><div class="trn">TRN <?php echo esc_html($customer['trn']); ?></div><?php endif; ?>
    </div>
  </div>

  <table class="items">
    <thead><tr>
      <th>Item</th><th class="c">Qty</th><th class="r">Price (<?php echo esc_html($cur); ?>)</th>
      <th class="r">VAT</th><th class="r">Total (<?php echo esc_html($cur); ?>)</th>
    </tr></thead>
    <tbody>
      <?php foreach($items??[] as $item): ?>
      <tr>
        <td><?php echo esc_html($item['name']); ?></td>
        <td class="c"><?php echo esc_html($item['qty']); ?></td>
        <td class="r"><?php echo number_format($item['price'],2); ?></td>
        <td class="r"><?php echo number_format($item['vat'],2); ?></td>
        <td class="r" style="font-weight:600;"><?php echo number_format($item['total'],2); ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <div class="totals">
    <table width="100%">
      <tr><td class="tl">Subtotal</td><td class="r"><?php echo number_format($subtotal??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr><td class="tl">VAT <?php echo esc_html(get_option('pbuaiv_vat_rate','5')); ?>%</td><td class="r"><?php echo number_format($vat_amount??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr class="final"><td>Total</td><td class="r"><?php echo number_format($total??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
    </table>
  </div>

  <div class="vat-block" style="margin-top:30px;">
    UAE FTA: TRN <strong><?php echo esc_html($company['trn']??'—'); ?></strong> · VAT AED <strong><?php echo number_format($vat_amount_aed??$vat_amount??0,2); ?></strong>
  </div>

  <div class="foot"><?php echo esc_html($company['name']??''); ?></div>
</div>
</body></html>
