<?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
/**
 * Delivery Note Template — UAE FTA VAT Compliant
 */
if ( ! defined( 'ABSPATH' ) ) exit;
$primary = esc_attr( $brand_primary ?? '#244b8e' );
?>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8">
<title>Delivery Note #<?php echo esc_html($order_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; }
.pg { max-width:794px; margin:0 auto; padding:32px; }
.hdr { display:flex; justify-content:space-between; align-items:flex-start; border-bottom:3px solid <?php echo esc_attr( $primary ); ?>; padding-bottom:14px; margin-bottom:20px; }
h1 { font-size:20px; color:<?php echo esc_attr( $primary ); ?>; font-weight:700; }
.meta { font-size:11px; color:#64748b; text-align:right; }
.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:<?php echo esc_attr( $primary ); ?>; margin-bottom:8px; }
table.items { width:100%; border-collapse:collapse; }
table.items th { background:<?php echo esc_attr( $primary ); ?>; color:#fff; padding:8px 10px; font-size:10px; text-align:left; }
table.items td { padding:8px 10px; border-bottom:1px solid #f1f5f9; font-size:11px; }
table.items tr:last-child td { border-bottom:none; }
.sig-row { display:flex; gap:40px; margin-top:32px; }
.sig-box { flex:1; border-top:1px solid #cbd5e1; padding-top:8px; font-size:10px; color:#94a3b8; text-align:center; }
.notes { background:#f8fafc; border-radius:6px; padding:10px; margin-top:16px; font-size:11px; color:#64748b; }
.footer { margin-top:20px; font-size:10px; color:#94a3b8; text-align:center; }
<?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:48px;margin-bottom:8px;" /><br><?php endif; ?>
      <h1>Delivery Note</h1>
      <div style="font-size:11px;color:#64748b;"><?php echo esc_html($company['name']??''); ?></div>
    </div>
    <div class="meta">
      <strong style="font-size:13px;color:#1e293b;"><?php echo esc_html($doc_number??'DRAFT'); ?></strong><br>
      <?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??''); ?><br>
      <?php if(!empty($company['trn'])): ?>TRN: <?php echo esc_html($company['trn']); ?><?php endif; ?>
    </div>
  </div>

  <div class="parties">
    <div class="party">
      <h3><?php esc_html_e('From','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']))); ?>
      <?php if(!empty($company['phone'])): ?><br>Tel: <?php echo esc_html($company['phone']); ?><?php endif; ?>
    </div>
    <div class="party">
      <h3><?php esc_html_e('Deliver To','pb-invoicing-system'); ?></h3>
      <strong><?php echo esc_html($customer['name']??''); ?></strong><br>
      <?php if(!empty($customer['company'])): ?><?php echo esc_html($customer['company']); ?><br><?php endif; ?>
      <?php echo wp_kses_post(nl2br($customer['shipping_address']??$customer['address']??'')); ?>
      <?php if(!empty($customer['phone'])): ?><br>Tel: <?php echo esc_html($customer['phone']); ?><?php endif; ?>
    </div>
  </div>

  <table class="items">
    <thead>
      <tr>
        <th><?php esc_html_e('Item','pb-invoicing-system'); ?></th>
        <th><?php esc_html_e('SKU','pb-invoicing-system'); ?></th>
        <th style="text-align:center;"><?php esc_html_e('Qty Ordered','pb-invoicing-system'); ?></th>
        <th style="text-align:center;"><?php esc_html_e('Qty Delivered','pb-invoicing-system'); ?></th>
      </tr>
    </thead>
    <tbody>
      <?php foreach($items??[] as $item): ?>
      <tr>
        <td><?php echo esc_html($item['name']); ?></td>
        <td style="font-size:10px;color:#94a3b8;"><?php echo esc_html($item['sku']??'—'); ?></td>
        <td style="text-align:center;"><?php echo esc_html($item['qty']); ?></td>
        <td style="text-align:center;">___</td>
      </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <?php if(!empty($custom_notes)||!empty($notes)): ?>
  <div class="notes">
    <strong><?php esc_html_e('Notes:','pb-invoicing-system'); ?></strong>
    <?php echo esc_html($custom_notes??$notes??''); ?>
  </div>
  <?php endif; ?>

  <div class="sig-row">
    <div class="sig-box"><?php esc_html_e('Dispatched by / Date','pb-invoicing-system'); ?></div>
    <div class="sig-box"><?php esc_html_e('Received by / Date','pb-invoicing-system'); ?></div>
    <div class="sig-box"><?php esc_html_e('Customer Signature','pb-invoicing-system'); ?></div>
  </div>

  <div class="footer"><?php echo esc_html( get_option( 'pbuaiv_company_name', '' ) ); ?></div>
</div></body></html>
