<?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
/**
 * Credit Note Template — UAE FTA VAT Compliant
 */
if ( ! defined( 'ABSPATH' ) ) exit;
$primary = esc_attr( $brand_primary ?? '#244b8e' );
$cur     = $currency ?? 'AED';
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<title>Credit Note <?php echo esc_html($doc_number??''); ?></title>
<?php pbuaiv_style_tag_open(); ?>
* { box-sizing:border-box; margin:0; padding:0; }
body { font-family:'Segoe UI',Arial,sans-serif; font-size:12px; color:#1e293b; background:#fff; }
.pg { max-width:794px; margin:0 auto; padding:40px; }
.hdr { display:flex; justify-content:space-between; align-items:flex-start; margin-bottom:28px; padding-bottom:16px; border-bottom:3px solid #ef4444; }
.hdr h1 { font-size:22px; color:#ef4444; font-weight:700; }
.hdr .num { font-size:13px; color:#64748b; }
.ref { background:#fef2f2; border:1px solid #fca5a5; border-radius:6px; padding:10px 14px; margin-bottom:20px; font-size:11px; }
.ref strong { color:#b91c1c; }
.parties { display:flex; gap:40px; margin-bottom:20px; }
.party { flex:1; background:#f8fafc; border-radius:6px; padding:12px; }
.party h3 { font-size:10px; text-transform:uppercase; letter-spacing:1px; color:#ef4444; margin-bottom:8px; }
table.items { width:100%; border-collapse:collapse; margin:16px 0; }
table.items th { background:#ef4444; color:#fff; padding:8px; font-size:10px; }
table.items td { border-bottom:1px solid #fee2e2; padding:8px; }
.totals { float:right; }
.totals td { padding:5px 12px; }
.totals .grand td { background:#ef4444; color:#fff; font-weight:700; font-size:14px; padding:10px 12px; }
.vat-note { clear:both; margin-top:20px; background:#fef2f2; border:1px solid #fca5a5; border-radius:6px; padding:10px; font-size:11px; }
<?php pbuaiv_style_tag_close(); ?></head><body>
<div class="pg">
  <div class="hdr">
    <div>
      <?php if(!empty($company['logo_url'])): ?><img src="<?php echo esc_url($company['logo_url']); ?>" style="max-height:50px;margin-bottom:8px;" /><br><?php endif; ?>
      <div style="font-size:13px;font-weight:600;"><?php echo esc_html($company['name']??''); ?></div>
      <?php if(!empty($company['trn'])): ?><div style="font-size:11px;color:#64748b;">TRN: <?php echo esc_html($company['trn']); ?></div><?php endif; ?>
    </div>
    <div style="text-align:right;">
      <h1>Credit Note</h1>
      <div class="num"><?php echo esc_html($doc_number??'DRAFT'); ?></div>
      <div style="font-size:11px;color:#94a3b8;"><?php echo esc_html(date_i18n(get_option('date_format'),strtotime($doc_date??wp_date('Y-m-d')))); ?></div>
    </div>
  </div>

  <?php if(!empty($referenced_invoice)): ?>
  <div class="ref">↩ <?php esc_html_e('Credit Note against Tax Invoice','pb-invoicing-system'); ?>: <strong><?php echo esc_html($referenced_invoice); ?></strong></div>
  <?php endif; ?>

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

  <table class="items">
    <thead><tr><th style="text-align:left;"><?php esc_html_e('Item','pb-invoicing-system'); ?></th><th><?php esc_html_e('Qty','pb-invoicing-system'); ?></th><th style="text-align:right;"><?php esc_html_e('Amount','pb-invoicing-system'); ?></th><th style="text-align:right;"><?php esc_html_e('VAT','pb-invoicing-system'); ?></th><th style="text-align:right;"><?php esc_html_e('Total','pb-invoicing-system'); ?></th></tr></thead>
    <tbody>
      <?php foreach($items??[] as $item): ?>
      <tr>
        <td><?php echo esc_html($item['name']); ?></td>
        <td style="text-align:center;"><?php echo esc_html($item['qty']); ?></td>
        <td style="text-align:right;color:#ef4444;">-<?php echo number_format(abs($item['subtotal']),2); ?> <?php echo esc_html($cur); ?></td>
        <td style="text-align:right;color:#ef4444;">-<?php echo number_format(abs($item['vat']),2); ?> <?php echo esc_html($cur); ?></td>
        <td style="text-align:right;font-weight:600;color:#ef4444;">-<?php echo number_format(abs($item['total']),2); ?> <?php echo esc_html($cur); ?></td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <div class="totals">
    <table>
      <tr><td><?php esc_html_e('VAT Refunded','pb-invoicing-system'); ?></td><td style="text-align:right;color:#ef4444;">-<?php echo number_format(abs($vat_amount??0),2); ?> <?php echo esc_html($cur); ?></td></tr>
      <tr class="grand"><td><?php esc_html_e('TOTAL CREDIT','pb-invoicing-system'); ?></td><td style="text-align:right;">-<?php echo number_format(abs($total??0),2); ?> <?php echo esc_html($cur); ?></td></tr>
    </table>
  </div>

  <div class="vat-note">
    <?php esc_html_e('UAE FTA: VAT refunded AED','pb-invoicing-system'); ?> <strong><?php echo number_format(abs($vat_amount_aed??$vat_amount??0),2); ?></strong> · TRN: <?php echo esc_html($company['trn']??'—'); ?>
  </div>
</div></body></html>
