<?php
if (!defined('ABSPATH')) exit;

$cur = simpinby_get_currency();
$CURRENCY = $cur["symbol"];

// Variables available:
// $invoice, $items, $pdf_settings

// Logo handling (best compatible)
$logo_url = "";
if (class_exists("SIMPINBY_Logo")) {
  $logo_url = SIMPINBY_Logo::get_pdf_logo_url();
} else {
  $logo_url = $pdf_settings["logo_url"] ?? "";
}
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">

  <style>
    @page {
      margin: 25px 30px 60px 30px; /* bottom space for footer */
    }

    body {
      font-family: DejaVu Sans, sans-serif;
      font-size: 12px;
      color: #111;
      margin: 0;
      padding: 0;
    }

    /* Fixed footer at bottom */
    .footer {
      position: fixed;
      bottom: -45px; /* push inside page margin */
      left: 0;
      right: 0;
      height: 45px;
      font-size: 11px;
      color: #333;
      border-top: 1px solid #ddd;
      padding-top: 10px;
    }

    .footer strong {
      color: #000;
    }

    /* Header */
    .top-header {
      width: 100%;
      margin-bottom: 12px;
    }

    .invoice-title {
      font-size: 18px;
      font-weight: bold;
      margin: 0;
      padding: 0;
    }

    .header-table {
      width: 100%;
      border-collapse: collapse;
      margin-bottom: 12px;
    }

    .header-table td {
      border: none;
      padding: 0;
      vertical-align: top;
    }

    .logo-box {
      text-align: right;
    }

    .logo-box img {
      max-height: 65px;
      max-width: 180px;
      object-fit: contain;
    }

    /* From/Customer section */
    .info-table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 8px;
      margin-bottom: 12px;
    }

    .info-table td {
      border: 1px solid #ddd;
      padding: 10px;
      vertical-align: top;
    }

    .box-title {
      font-weight: bold;
      font-size: 12px;
      margin-bottom: 6px;
    }

    /* Items table */
    table.items {
      width: 100%;
      border-collapse: collapse;
      margin-top: 10px;
    }

    table.items th, table.items td {
      border: 1px solid #ddd;
      padding: 8px;
      text-align: left;
    }

    table.items th {
      background: #f5f5f5;
      font-weight: bold;
    }

    .right { text-align: right; }

    /* Totals */
    table.totals {
      width: 320px;
      border-collapse: collapse;
      margin-left: auto;
      margin-top: 12px;
    }

    table.totals th, table.totals td {
      border: 1px solid #ddd;
      padding: 8px;
    }

    table.totals th {
      background: #f5f5f5;
      text-align: left;
    }

    table.totals td {
      text-align: right;
    }

    .grand-total th,
    .grand-total td {
      font-weight: bold;
      font-size: 13px;
    }

    /* Small text */
    .muted {
      color: #555;
      font-size: 11px;
      margin-top: 3px;
    }
  </style>
</head>

<body>

  <!-- HEADER -->
  <table class="header-table">
    <tr>
      <td>
        <p class="invoice-title">INVOICE</p>
        <div class="muted">
          Invoice #: <b><?php echo esc_html($invoice["invoice_number"]); ?></b><br>
          <?php
          $formatted_date = wp_date(
              get_option('date_format'),
              strtotime($invoice["created_at"])
          );
          ?>

          Date: <?php echo esc_html($formatted_date); ?>
        </div>
      </td>

      <td class="logo-box">
        <?php if (!empty($logo_url)): ?>
          <img src="<?php echo esc_url($logo_url); ?>" />
        <?php endif; ?>
      </td>
    </tr>
  </table>

  <!-- FROM + CUSTOMER -->
  <table class="info-table">
    <tr>
      <td style="width:50%;">
        <div class="box-title">From</div>
        <b><?php echo esc_html($pdf_settings["company_name"] ?? ""); ?></b><br>

        <?php if (!empty($pdf_settings["company_address"])): ?>
          <?php echo nl2br(esc_html($pdf_settings["company_address"])); ?><br>
        <?php endif; ?>

        <?php if (!empty($pdf_settings["company_phone"])): ?>
          Phone: <?php echo esc_html($pdf_settings["company_phone"]); ?><br>
        <?php endif; ?>

        <?php if (!empty($pdf_settings["company_email"])): ?>
          Email: <?php echo esc_html($pdf_settings["company_email"]); ?><br>
        <?php endif; ?>

        <?php if (!empty($pdf_settings["company_website"])): ?>
          Web: <?php echo esc_html($pdf_settings["company_website"]); ?><br>
        <?php endif; ?>
      </td>

      <td style="width:50%;">
        <div class="box-title">Bill To</div>
        <b><?php echo esc_html($invoice["customer_name"]); ?></b><br>

        <?php if (!empty($invoice["customer_phone"])): ?>
          Phone: <?php echo esc_html($invoice["customer_phone"]); ?><br>
        <?php endif; ?>

        <?php if (!empty($invoice["customer_address"])): ?>
          <?php echo nl2br(esc_html($invoice["customer_address"])); ?>
        <?php endif; ?>
      </td>
    </tr>
  </table>

  <!-- ITEMS -->
  <table class="items">
    <thead>
      <tr>
        <th>Product</th>
        <th class="right" style="width:70px;">Qty</th>
        <th class="right" style="width:110px;">Price</th>
        <th class="right" style="width:120px;">Total</th>
      </tr>
    </thead>

    <tbody>
      <?php foreach ($items as $it): ?>
        <tr>
          <td><?php echo esc_html($it["product_name"]); ?></td>
          <td class="right"><?php echo esc_html($it["qty"]); ?></td>
          <td class="right"><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$it["price"], 2)); ?></td>
          <td class="right"><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$it["row_total"], 2)); ?></td>
        </tr>
      <?php endforeach; ?>
    </tbody>
  </table>

  <!-- TOTALS -->
  <table class="totals">
    <tr>
      <th>Subtotal</th>
      <td><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$invoice["subtotal"], 2)); ?></td>
    </tr>

    <tr>
      <th>Discount</th>
      <td><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$invoice["discount"], 2)); ?></td>
    </tr>

    <tr>
        <th>
            <?php if (!empty($invoice["tax_name"])): ?>
            <?php echo esc_html($invoice["tax_name"]); ?> (<?php echo esc_html($invoice["tax_percent"]); ?>%)
            <?php else: ?>
            Tax
            <?php endif; ?>
        </th>
        <td><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$invoice["total_tax"], 2)); ?></td>
    </tr>


    <tr class="grand-total">
      <th>Total</th>
      <td><?php echo esc_html($CURRENCY); ?> <?php echo esc_html(number_format((float)$invoice["total_amount"], 2)); ?></td>
    </tr>
  </table>

  <?php if (!empty($pdf_settings["footer_text"])): ?>
    <div style="margin-top:15px; font-size:12px; color:#333;">
      <?php echo esc_html($pdf_settings["footer_text"]); ?>
    </div>
  <?php endif; ?>

  <!-- FIXED FOOTER -->
  <div class="footer">
    <strong><?php echo esc_html($pdf_settings["company_name"] ?? ""); ?></strong><br>

    <?php if (!empty($pdf_settings["company_website"])): ?>
      Web: <?php echo esc_html($pdf_settings["company_website"]); ?>
    <?php endif; ?>

    <?php if (!empty($pdf_settings["company_phone"])): ?>
      &nbsp; | &nbsp; Call Us: <?php echo esc_html($pdf_settings["company_phone"]); ?>
    <?php endif; ?>

    <?php if (!empty($pdf_settings["company_email"])): ?>
      &nbsp; | &nbsp; E-Mail: <?php echo esc_html($pdf_settings["company_email"]); ?>
    <?php endif; ?>

  </div>

</body>
</html>
