<?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
/**
 * Classic Invoice Template — UAE FTA VAT Compliant
 * Traditional black-and-white friendly layout.
 */
if ( ! defined( 'ABSPATH' ) ) exit;
$primary = esc_attr( $brand_primary ?? '#244b8e' );
$is_std  = ( $invoice_type ?? 'standard' ) === 'standard';
$cur     = $currency ?? 'AED';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title><?php echo esc_html($label??'Invoice'); ?> <?php echo esc_html($doc_number??''); ?></title>
<?php pbuaiv_style_tag_open(); ?>
  * { box-sizing:border-box; }
  body { font-family: 'Times New Roman', Times, serif; font-size: 11px; color: #000; background: #fff; }
  .inv { max-width: 794px; margin: 0 auto; padding: 40px; }
  h1 { font-size: 22px; text-align: center; text-transform: uppercase; letter-spacing: 2px; margin-bottom: 4px; color: <?php echo esc_attr( $primary ); ?>; }
  .inv-type { text-align: center; font-size: 11px; color: #444; margin-bottom: 24px; }
  .inv-meta { border: 2px solid <?php echo esc_attr( $primary ); ?>; margin-bottom: 20px; }
  .inv-meta table { width: 100%; border-collapse: collapse; }
  .inv-meta td { padding: 6px 12px; border-bottom: 1px solid #e2e8f0; font-size: 11px; }
  .inv-meta .key { color: #475569; width: 130px; }
  .inv-meta .val { font-weight: 600; }
  .inv-parties { display: flex; gap: 20px; margin-bottom: 20px; }
  .inv-party { flex: 1; border: 1px solid #000; padding: 10px; }
  .inv-party h3 { font-size: 11px; text-transform: uppercase; border-bottom: 1px solid #000; padding-bottom: 4px; margin-bottom: 6px; }
  .inv-items { width: 100%; border-collapse: collapse; margin-bottom: 16px; font-size: 11px; }
  .inv-items th { border: 1px solid #000; padding: 6px 8px; background: #000; color: #fff; font-size: 10px; }
  .inv-items td { border: 1px solid #ccc; padding: 6px 8px; }
  .inv-items tfoot td { border: 1px solid #ccc; padding: 5px 8px; font-weight: 600; }
  .inv-totals { float: right; font-size: 11px; border: 1px solid #000; }
  .inv-totals td { padding: 5px 12px; }
  .inv-total-row td { background: #000; color: #fff; font-weight: 700; font-size: 13px; }
  .inv-vat-box { border: 2px solid <?php echo esc_attr( $primary ); ?>; padding: 10px; margin-top: 20px; clear: both; font-size: 11px; }
  .inv-vat-box h4 { margin-bottom: 6px; color: <?php echo esc_attr( $primary ); ?>; }
  .inv-footer-line { border-top: 2px solid #000; margin-top: 30px; padding-top: 10px; text-align: center; font-size: 10px; color: #666; }
<?php pbuaiv_style_tag_close(); ?>
</head>
<body>
<div class="inv">
  <h1><?php echo esc_html($label??'Tax Invoice'); ?></h1>
  <div class="inv-type"><?php echo esc_html( $is_std ? 'Standard Tax Invoice' : 'Simplified Tax Invoice' ); ?></div>

  <div class="inv-meta">
    <table>
      <tr><td class="key">Invoice No.</td><td class="val"><?php echo esc_html($doc_number??'DRAFT'); ?></td><td class="key">Date</td><td class="val"><?php echo esc_html(date_i18n(get_option('date_format'),strtotime($doc_date??wp_date('Y-m-d')))); ?></td></tr>
      <tr><td class="key">Order No.</td><td class="val">#<?php echo esc_html($order_number??''); ?></td><td class="key">Supply Date</td><td class="val"><?php echo esc_html(date_i18n(get_option('date_format'),strtotime($supply_date??$doc_date??wp_date('Y-m-d')))); ?></td></tr>
      <tr><td class="key">Payment</td><td class="val"><?php echo esc_html($payment_method??''); ?></td><td class="key">Currency</td><td class="val"><?php echo esc_html($cur); ?></td></tr>
    </table>
  </div>

  <div class="inv-parties">
    <div class="inv-party">
      <h3>Seller</h3>
      <strong><?php echo esc_html($company['name']??''); ?></strong><br>
      <?php echo esc_html($company['address']??''); ?><br>
      <?php echo esc_html($company['emirate']??''); ?>, UAE<br>
      <?php if(!empty($company['trn'])): ?>TRN: <strong><?php echo esc_html($company['trn']); ?></strong><?php endif; ?>
    </div>
    <div class="inv-party">
      <h3>Buyer</h3>
      <strong><?php echo esc_html($customer['name']??''); ?></strong><br>
      <?php echo wp_kses_post(nl2br($customer['address']??'')); ?><br>
      <?php if(!empty($customer['trn'])): ?>TRN: <strong><?php echo esc_html($customer['trn']); ?></strong><?php endif; ?>
    </div>
  </div>

  <table class="inv-items">
    <thead><tr>
      <th>#</th><th style="text-align:left;">Description</th><th>Qty</th>
      <th>Unit Price (<?php echo esc_html($cur); ?>)</th><th>VAT%</th>
      <th>VAT Amt (<?php echo esc_html($cur); ?>)</th><th>Total (<?php echo esc_html($cur); ?>)</th>
    </tr></thead>
    <tbody>
      <?php $i=1; foreach($items??[] as $item): ?>
      <tr>
        <td style="text-align:center;"><?php echo (int) $i++; ?></td>
        <td><?php echo esc_html($item['name']); ?><?php if(!empty($item['sku'])): ?> <small>(<?php echo esc_html($item['sku']); ?>)</small><?php endif; ?></td>
        <td style="text-align:center;"><?php echo esc_html($item['qty']); ?></td>
        <td style="text-align:right;"><?php echo number_format($item['price'],2); ?></td>
        <td style="text-align:center;"><?php echo esc_html($item['vat_rate']); ?>%</td>
        <td style="text-align:right;"><?php echo number_format($item['vat'],2); ?></td>
        <td style="text-align:right;font-weight:600;"><?php echo number_format($item['total'],2); ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <div class="inv-totals">
    <table>
      <tr><td>Subtotal</td><td style="text-align:right;"><?php echo number_format($subtotal??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr><td>Discount</td><td style="text-align:right;">-<?php echo number_format($discount??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr><td>VAT (<?php echo esc_html(get_option('pbuaiv_vat_rate','5')); ?>%)</td><td style="text-align:right;"><?php echo number_format($vat_amount??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr class="inv-total-row"><td>TOTAL</td><td style="text-align:right;"><?php echo number_format($total??0,2); ?> <?php echo esc_html($cur); ?></td></tr>
    </table>
  </div>

  <div class="inv-vat-box">
    <h4>UAE FTA VAT Summary</h4>
    Supplier TRN: <strong><?php echo esc_html($company['trn']??'—'); ?></strong> &nbsp;|&nbsp;
    VAT Amount (AED): <strong>AED <?php echo number_format($vat_amount_aed??$vat_amount??0,2); ?></strong> &nbsp;|&nbsp;
    VAT Rate: <strong><?php echo esc_html(get_option('pbuaiv_vat_rate','5')); ?>%</strong>
  </div>

  <div class="inv-footer-line">
    <?php echo esc_html($company['name']??''); ?> &mdash; <?php echo esc_html($company['website']??''); ?>

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