<?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
/**
 * Modern Receipt Template — UAE FTA VAT Compliant
 * Full A4 layout matching the invoice template style.
 */
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">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php echo esc_html( $label ?? 'Receipt' ); ?> <?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; }
  .inv-page { max-width: 794px; margin: 0 auto; padding: 40px; }

  /* Header */
  .inv-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 32px; border-bottom: 3px solid <?php echo esc_attr( $primary ); ?>; padding-bottom: 20px; }
  .inv-logo img { max-height: 70px; max-width: 200px; object-fit: contain; }
  .inv-logo-placeholder { font-size: 20px; font-weight: 700; color: <?php echo esc_attr( $primary ); ?>; }
  .inv-doc-title { text-align: right; }
  .inv-doc-title h1 { font-size: 24px; font-weight: 700; color: <?php echo esc_attr( $primary ); ?>; letter-spacing: -0.5px; }
  .inv-doc-title .inv-type-badge { display: inline-block; background: <?php echo esc_attr( $accent ); ?>; color: #fff; font-size: 10px; font-weight: 700; padding: 2px 8px; border-radius: 12px; margin-top: 4px; letter-spacing: 0.5px; text-transform: uppercase; }
  .inv-doc-title .inv-number { font-size: 14px; color: #64748b; margin-top: 4px; }
  .inv-doc-title .inv-date { font-size: 11px; color: #94a3b8; }

  /* Accent band */
  .inv-accent-band { background: <?php echo esc_attr( $primary ); ?>; color: #fff; padding: 10px 40px; margin: 0 -40px; display: flex; justify-content: space-between; font-size: 11px; }
  .inv-accent-band strong { font-size: 12px; }

  /* Party section */
  .inv-parties { display: flex; gap: 40px; margin: 24px 0; }
  .inv-party { flex: 1; }
  .inv-party__title { font-size: 10px; font-weight: 700; text-transform: uppercase; color: <?php echo esc_attr( $primary ); ?>; letter-spacing: 1px; border-bottom: 1.5px solid <?php echo esc_attr( $accent ); ?>; padding-bottom: 4px; margin-bottom: 8px; }
  .inv-party__name { font-size: 13px; font-weight: 700; color: #1e293b; margin-bottom: 4px; }
  .inv-party__line { font-size: 11px; color: #475569; margin-bottom: 2px; }
  .inv-party__trn { font-size: 11px; color: <?php echo esc_attr( $primary ); ?>; font-weight: 600; margin-top: 6px; background: #f0f6ff; padding: 4px 8px; border-radius: 4px; display: inline-block; }
  .inv-icon { color: #64748b; font-size: 10px; font-weight: 600; }

  /* Paid stamp */
  .inv-paid-stamp { display: inline-block; border: 3px solid #16a34a; color: #16a34a; font-size: 22px; font-weight: 900; letter-spacing: 3px; padding: 4px 16px; border-radius: 6px; transform: rotate(-8deg); margin-bottom: 16px; text-transform: uppercase; }

  /* Items table */
  .inv-items { width: 100%; border-collapse: collapse; margin: 20px 0; font-size: 11px; }
  .inv-items thead tr { background: <?php echo esc_attr( $primary ); ?>; color: #fff; }
  .inv-items thead th { padding: 9px 10px; text-align: left; font-weight: 600; font-size: 10px; text-transform: uppercase; letter-spacing: 0.5px; }
  .inv-items tbody tr:nth-child(even) { background: #f8fafc; }
  .inv-items tbody td { padding: 8px 10px; border-bottom: 1px solid #e2e8f0; }
  .col-num    { width: 4%; text-align: center; }
  .col-desc   { width: 42%; }
  .col-qty    { width: 8%; text-align: center; }
  .col-price  { width: 16%; text-align: right; }
  .col-total  { width: 16%; text-align: right; font-weight: 600; }

  /* Totals */
  .inv-totals { display: flex; justify-content: flex-end; margin: 0 0 24px; }
  .inv-totals table { min-width: 280px; }
  .inv-totals td { padding: 5px 10px; font-size: 12px; }
  .inv-totals .label { color: #475569; text-align: right; }
  .inv-totals .amount { text-align: right; font-weight: 600; }
  .inv-totals .subtotal-row td { border-top: 1px solid #e2e8f0; }
  .inv-totals .total-row td { background: <?php echo esc_attr( $primary ); ?>; color: #fff; font-size: 14px; font-weight: 700; padding: 10px 12px; }
  .inv-totals .vat-row td { background: <?php echo esc_attr( $accent ); ?>18; }
  .inv-totals .vat-row .label, .inv-totals .vat-row .amount { color: <?php echo esc_attr( $accent ); ?>; }

  /* VAT summary */
  .inv-vat-summary { background: #f0f6ff; border: 1px solid #bfdbfe; border-radius: 6px; padding: 12px 16px; margin-bottom: 20px; font-size: 11px; }
  .inv-vat-summary__title { font-weight: 700; color: <?php echo esc_attr( $primary ); ?>; margin-bottom: 6px; }
  .inv-vat-aed { font-size: 14px; font-weight: 700; color: <?php echo esc_attr( $primary ); ?>; }

  /* Footer */
  .inv-footer { display: flex; justify-content: space-between; align-items: flex-end; border-top: 1.5px solid <?php echo esc_attr( $accent ); ?>; padding-top: 16px; }
  .inv-notes { font-size: 10px; color: #64748b; max-width: 420px; }
  .inv-bank { font-size: 10px; color: #475569; }
  .inv-bank strong { display: block; color: <?php echo esc_attr( $primary ); ?>; font-size: 11px; margin-bottom: 4px; }

  @media print {
    body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
    .inv-page { padding: 20px; }
    .no-print { display: none !important; }
  }
<?php pbuaiv_style_tag_close(); ?>
</head>
<body>
<div class="inv-page">

  <!-- Header -->
  <div class="inv-header">
    <div class="inv-logo">
      <?php if ( ! empty( $company['logo_url'] ) ) : ?>
        <img src="<?php echo esc_url( $company['logo_url'] ); ?>" alt="<?php echo esc_attr( $company['name'] ); ?>" />
      <?php else : ?>
        <div class="inv-logo-placeholder"><?php echo esc_html( $company['name'] ); ?></div>
      <?php endif; ?>
    </div>
    <div class="inv-doc-title">
      <h1><?php echo esc_html( $label ?? __( 'Payment Receipt', 'pb-invoicing-system' ) ); ?></h1>
      <div class="inv-type-badge"><?php esc_html_e( 'Payment Received', 'pb-invoicing-system' ); ?></div>
      <?php if ( ! empty( $doc_number ) ) : ?>
        <div class="inv-number"><?php echo esc_html( $doc_number ); ?></div>
      <?php else : ?>
        <div class="inv-number" style="color:#f59e0b;">DRAFT</div>
      <?php endif; ?>
      <div class="inv-date">
        <?php esc_html_e( 'Date:', 'pb-invoicing-system' ); ?> <?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $doc_date ?? wp_date( 'Y-m-d' ) ) ) ); ?><br>
        <?php esc_html_e( 'Order:', 'pb-invoicing-system' ); ?> #<?php echo esc_html( $order_number ?? '' ); ?>
      </div>
    </div>
  </div>

  <!-- Accent band -->
  <div class="inv-accent-band">
    <span>
      <strong><?php esc_html_e( 'Payment Method:', 'pb-invoicing-system' ); ?></strong>
      <?php echo esc_html( $payment_method ?? '' ); ?>
    </span>
    <span>
      <strong><?php esc_html_e( 'Order Status:', 'pb-invoicing-system' ); ?></strong>
      <?php echo esc_html( $order_status ?? '' ); ?>
    </span>
    <span>
      <strong><?php esc_html_e( 'Currency:', 'pb-invoicing-system' ); ?></strong>
      <?php echo esc_html( $cur ); ?>
    </span>
  </div>

  <!-- Parties -->
  <div class="inv-parties">
    <!-- Seller -->
    <div class="inv-party">
      <div class="inv-party__title"><?php esc_html_e( 'From — Seller', 'pb-invoicing-system' ); ?></div>
      <div class="inv-party__name"><?php echo esc_html( $company['name'] ?? '' ); ?></div>
      <?php if ( ! empty( $company['address'] ) ) : ?><div class="inv-party__line"><?php echo esc_html( $company['address'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $company['city'] ) ) : ?><div class="inv-party__line"><?php echo esc_html( $company['city'] ); ?><?php if ( ! empty( $company['emirate'] ) ) : ?>, <?php echo esc_html( $company['emirate'] ); ?><?php endif; ?></div><?php endif; ?>
      <div class="inv-party__line"><?php echo esc_html( $company['country'] ?? 'UAE' ); ?></div>
      <?php if ( ! empty( $company['phone'] ) ) : ?><div class="inv-party__line"><span class="inv-icon">Tel:</span> <?php echo esc_html( $company['phone'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $company['email'] ) ) : ?><div class="inv-party__line"><span class="inv-icon">Email:</span> <?php echo esc_html( $company['email'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $company['trn'] ) ) : ?><div class="inv-party__trn">TRN: <?php echo esc_html( $company['trn'] ); ?></div><?php endif; ?>
    </div>

    <!-- Buyer -->
    <div class="inv-party">
      <div class="inv-party__title"><?php esc_html_e( 'To — Buyer', 'pb-invoicing-system' ); ?></div>
      <div class="inv-party__name"><?php echo esc_html( $customer['name'] ?? '' ); ?></div>
      <?php if ( ! empty( $customer['company'] ) ) : ?><div class="inv-party__line"><?php echo esc_html( $customer['company'] ); ?></div><?php endif; ?>
      <div class="inv-party__line"><?php echo wp_kses_post( nl2br( $customer['address'] ?? '' ) ); ?></div>
      <?php if ( ! empty( $customer['phone'] ) ) : ?><div class="inv-party__line"><span class="inv-icon">Tel:</span> <?php echo esc_html( $customer['phone'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $customer['email'] ) ) : ?><div class="inv-party__line"><span class="inv-icon">Email:</span> <?php echo esc_html( $customer['email'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $customer['trn'] ) ) : ?><div class="inv-party__trn">TRN: <?php echo esc_html( $customer['trn'] ); ?></div><?php endif; ?>
    </div>
  </div>

  <!-- Paid stamp -->
  <div style="text-align:center;margin:8px 0 20px;">
    <span class="inv-paid-stamp"><?php esc_html_e( 'PAID', 'pb-invoicing-system' ); ?></span>
  </div>

  <!-- Line items -->
  <table class="inv-items">
    <thead>
      <tr>
        <th class="col-num">#</th>
        <th class="col-desc"><?php esc_html_e( 'Description', 'pb-invoicing-system' ); ?></th>
        <th class="col-qty"><?php esc_html_e( 'Qty', 'pb-invoicing-system' ); ?></th>
        <th class="col-price"><?php esc_html_e( 'Unit Price', 'pb-invoicing-system' ); ?></th>
        <th class="col-total"><?php esc_html_e( 'Total', 'pb-invoicing-system' ); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php $i = 1; foreach ( $items ?? [] as $item ) : ?>
        <tr>
          <td class="col-num"><?php echo (int) $i++; ?></td>
          <td class="col-desc">
            <strong><?php echo esc_html( $item['name'] ); ?></strong>
            <?php if ( ! empty( $item['sku'] ) ) : ?><br><small style="color:#94a3b8;">SKU: <?php echo esc_html( $item['sku'] ); ?></small><?php endif; ?>
          </td>
          <td class="col-qty" style="text-align:center;"><?php echo esc_html( $item['qty'] ); ?></td>
          <td class="col-price"><?php echo esc_html( number_format( $item['price'], 2 ) ); ?> <?php echo esc_html( $cur ); ?></td>
          <td class="col-total"><?php echo esc_html( number_format( $item['total'], 2 ) ); ?> <?php echo esc_html( $cur ); ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <!-- Totals -->
  <div class="inv-totals">
    <table>
      <tr class="subtotal-row">
        <td class="label"><?php esc_html_e( 'Subtotal (ex. VAT)', 'pb-invoicing-system' ); ?></td>
        <td class="amount"><?php echo esc_html( number_format( $subtotal ?? 0, 2 ) ); ?> <?php echo esc_html( $cur ); ?></td>
      </tr>
      <tr class="vat-row">
        <td class="label"><?php esc_html_e( 'VAT (5%)', 'pb-invoicing-system' ); ?></td>
        <td class="amount"><?php echo esc_html( number_format( $vat_amount ?? 0, 2 ) ); ?> <?php echo esc_html( $cur ); ?></td>
      </tr>
      <?php if ( ( $currency ?? 'AED' ) !== 'AED' ) : ?>
      <tr>
        <td class="label" style="font-size:10px;color:#94a3b8;"><?php esc_html_e( 'VAT in AED (FTA)', 'pb-invoicing-system' ); ?></td>
        <td class="amount" style="font-size:10px;color:#244b8e;">AED <?php echo esc_html( number_format( $vat_amount_aed ?? 0, 2 ) ); ?></td>
      </tr>
      <?php endif; ?>
      <tr class="total-row">
        <td class="label" style="color:#fff;font-size:13px;"><?php esc_html_e( 'TOTAL PAID', 'pb-invoicing-system' ); ?></td>
        <td class="amount" style="color:#fff;font-size:13px;"><?php echo esc_html( number_format( $total ?? 0, 2 ) ); ?> <?php echo esc_html( $cur ); ?></td>
      </tr>
    </table>
  </div>

  <!-- VAT summary -->
  <div class="inv-vat-summary">
    <div class="inv-vat-summary__title"><?php esc_html_e( 'UAE VAT Summary (FTA Compliant)', 'pb-invoicing-system' ); ?></div>
    <table style="width:100%;font-size:11px;">
      <tr>
        <td><?php esc_html_e( 'VAT Rate Applied:', 'pb-invoicing-system' ); ?></td>
        <td><?php echo esc_html( get_option( 'pbuaiv_vat_rate', '5' ) ); ?>% <?php esc_html_e( '(UAE Standard Rate)', 'pb-invoicing-system' ); ?></td>
      </tr>
      <tr>
        <td><?php esc_html_e( 'Total VAT Amount (AED):', 'pb-invoicing-system' ); ?></td>
        <td class="inv-vat-aed">AED <?php echo esc_html( number_format( $vat_amount_aed ?? $vat_amount ?? 0, 2 ) ); ?></td>
      </tr>
      <tr>
        <td><?php esc_html_e( 'Supplier TRN:', 'pb-invoicing-system' ); ?></td>
        <td><strong><?php echo esc_html( $company['trn'] ?? '—' ); ?></strong></td>
      </tr>
    </table>
  </div>

  <!-- Footer: notes + bank details -->
  <div class="inv-footer">
    <div class="inv-notes">
      <?php if ( ! empty( $custom_notes ) ) : ?><p><?php echo esc_html( $custom_notes ); ?></p><?php endif; ?>
      <?php if ( ! empty( $notes ) ) : ?><p style="margin-top:6px;font-style:italic;"><?php echo esc_html( $notes ); ?></p><?php endif; ?>
      <p style="margin-top:8px;font-weight:600;color:#16a34a;"><?php esc_html_e( 'Thank you for your business!', 'pb-invoicing-system' ); ?></p>
    </div>
    <?php if ( ! empty( $bank['name'] ) ) : ?>
    <div class="inv-bank">
      <strong><?php esc_html_e( 'Bank Details', 'pb-invoicing-system' ); ?></strong>
      <?php if ( ! empty( $bank['name'] ) ) : ?><div><?php echo esc_html( $bank['name'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $bank['iban'] ) ) : ?><div>IBAN: <?php echo esc_html( $bank['iban'] ); ?></div><?php endif; ?>
      <?php if ( ! empty( $bank['swift'] ) ) : ?><div>SWIFT: <?php echo esc_html( $bank['swift'] ); ?></div><?php endif; ?>
    </div>
    <?php endif; ?>
  </div>

  <!-- Branding footer -->
  <div style="margin-top:24px;text-align:center;font-size:9px;color:#cbd5e1;">
    <?php echo esc_html( $company['name'] ?? '' ); ?> &nbsp;|&nbsp;
    <?php if ( ! empty( $company['website'] ) ) : ?><?php echo esc_html( $company['website'] ); ?> &nbsp;|&nbsp; <?php endif; ?>
    <?php echo esc_html( get_option( 'pbuaiv_company_name', '' ) ); ?>
  </div>

</div><!-- .inv-page -->
</body>
</html>
