   <head>
<?php include __DIR__ . '/../layout/head.php'; ?>
<?php include __DIR__ . '/../layout/head-css.php'; ?>
<script>
document.addEventListener("DOMContentLoaded", function () {
    if (!document.body.classList.contains("no-auto-print")) {
        window.print();
        setTimeout(function () {
            window.close();
        }, 5000);
    }
});
</script>
<style>
    .ribbon {
    width: 200px;
    padding: 10px 0;
    color: white;
    font-weight: bold;
    font-size: 1.5rem;
    text-align: center;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
    pointer-events: none;
  }
</style>
  </head>

    <div class="card">
      <div class="card-body" id="printArea">
<!--         <div class="row">
          <div class="col-md-5 text-left">
            <img src="<?= base_url('uploads/settings/' . ($settings->logo_receipt ?? 'favicon.png')) ?>" style="max-height:55px;">
            <p><?= html_escape($settings->org_address ?? 'Clinic Address') ?><br>
              <?= html_escape($settings->org_phone ?? 'Number') ?>
            </p>
            
          </div>
          <div class="col-md-3">
<?php if (!empty($tokenwithshift)) : ?>
  <?php if ($tokenwithshift->balance > 0 && $tokenwithshift->total_fee != $tokenwithshift->balance) : ?>
    <div class="ribbon bg-warning">Partially Paid</div>
  <?php elseif ($tokenwithshift->total_fee == $tokenwithshift->balance) : ?>
    <div class="ribbon bg-danger">Un-Paid</div>
  <?php endif; ?>
<?php endif; ?>




            
            
          
          </div>
                    
          <div class="col-md-4 text-right" style="text-align: end;">
            <img src="<?= base_url('admin/render/QRcode/' . $print_invoicing->id); ?>" width="100" alt="Invoice QR Code">
          </div>
        </div> -->
        <table class="table table-borderless">
          <tr>
            <td>
              <img src="<?= base_url('uploads/settings/' . ($settings->logo_receipt ?? 'favicon.png')) ?>" style="max-height:55px;">
            <p><?= html_escape($settings->org_address ?? 'Clinic Address') ?><br>
              <?= html_escape($settings->org_phone ?? 'Number') ?>
            </p>
            </td>
            <td>
              <?php if (!empty($tokenwithshift)) : ?>
  <?php if ($tokenwithshift->balance > 0 && $tokenwithshift->total_fee != $tokenwithshift->balance) : ?>
    <div class="ribbon bg-warning">Partially Paid</div>
  <?php elseif ($tokenwithshift->total_fee == $tokenwithshift->balance) : ?>
    <div class="ribbon bg-danger">Un-Paid</div>
  <?php endif; ?>
<?php endif; ?>
            </td>
            <td style="text-align: right;">
              <img src="<?= base_url('admin/render/QRcode/' . $print_invoicing->id); ?>" width="100" alt="Invoice QR Code">
            </td>
          </tr>
          
        </table>
<hr>
        <!-- Header Info -->
        <table class="table table-borderless mb-2">
          <tr class="">
            <td>
              <strong>Date &amp; Time:</strong>
              <small><?= date('d-m-Y - h:i:s A', strtotime($tokenwithshift->created_at)) ?></small><br>
              <strong>Shift:</strong> <?= html_escape($tokenwithshift->shift_name); ?>
            </td>
            <td style="text-align: right;">
              <h1><?= (int)$tokenwithshift->token_number ?></h1>
            </td>
          </tr>
          <tr>
            <td><strong>Patient:</strong> <?= html_escape($tokenwithshift->name); ?></td>
            <td style="text-align: right;"><strong>Sex:</strong> <?= html_escape($tokenwithshift->gender); ?></td>
          </tr>
          <tr>
            <td><strong>Phone:</strong> <?= html_escape($tokenwithshift->phone); ?></td>
            <td style="text-align: right;"><strong>Age:</strong> <?= html_escape(trim($tokenwithshift->age . ' ' . $tokenwithshift->age_unit)); ?></td>
          </tr>
        </table>

        <?php if (!empty($tokenwithshift->funded_by)) : ?>
          <span class="d-block text-center" style="background:#000; color:#fff;">
            Funded By: <?= html_escape($tokenwithshift->funded_by); ?>
          </span>
        <?php endif; ?>

        <!-- Services Table -->
        <table class="table table-bordered">
          <thead>
            <tr>
              <th style="width:50px;">#</th>
              <th>Service</th>
              <th style="width:100px; text-align:right;">Amount</th>
            </tr>
          </thead>
          <tbody>
            <?php
            $computed_total = 0.00;
            if (!empty($services)) {
              foreach ($services as $srv) {
                $computed_total += (float)($srv['fee'] ?? 0.00);
              }
            }

            $currency         = 'PKR';
            $discount_percent = (float)($tokenwithshift->discount_percent ?? 0);
            $discount_amount  = (float)($tokenwithshift->discount_amount ?? 0);
            $balance_amount   = (float)($tokenwithshift->balance ?? 0);


            if ($discount_percent > 0 && ($discount_amount <= 0)) {
              $discount_amount = ($computed_total * $discount_percent) / 100;
            }

            // $final_total = $computed_total - $discount_amount - $balance_amount;

            $paid_amount = $tokenwithshift->total_fee - $balance_amount; 

            ?>

            <?php if (!empty($services)) : ?>
              <?php foreach ($services as $i => $srv) : ?>
                <tr>
                  <td><?= $i + 1 ?></td>

<?php
if (!function_exists('formatServiceName')) {
    function formatServiceName($deptName, $subServiceName, $consultantName) {
        $deptName = strtolower(trim($deptName));
        $subServiceName = trim($subServiceName);
        $consultantName = trim($consultantName);

        if ($deptName === 'opd') {
            return "OPD {$subServiceName} By {$consultantName}";
        } else if ($deptName === 'lab') {
            return "{$subServiceName} By {$consultantName} Lab";
        } else if ($deptName === 'ultrasound') {
            return "{$subServiceName} Ultrasound by {$consultantName}";
        } else if ($deptName === 'physiotherapy') {
            return "Physiotherapy {$subServiceName} by {$consultantName}";
        } else if ($deptName === 'clinical services') {
            return $subServiceName;
        } else {
            return "{$subServiceName} by {$consultantName} {$deptName}";
        }
    }
}
?>

<td>
    <?= html_escape(
        formatServiceName(
            $srv['department_name'] ?? '',        // Make sure this is available, else '' 
            $srv['sub_service_name'] ?? '',
            $srv['service_name'] ?? ''
        )
    ) ?>
</td>

                  <!-- <td><?= html_escape (!empty($srv['sub_service_name']) ? '' . $srv['sub_service_name'] : '') ?></td> -->
                  <td class="text-right"><?= amount_format_pakistani((float)$srv['fee']) ?></td>
                </tr>
              <?php endforeach; ?>
            <?php else : ?>
              <tr>
                <td colspan="3" class="text-center">No services added.</td>
              </tr>
            <?php endif; ?>

            <tr>
              <td colspan="2" class="text-right"><strong>Net Total:</strong></td>
              <td class="text-right"><strong><?= amount_format_pakistani($tokenwithshift->total_fee) ?></strong></td>
            </tr>


            <?php if ($discount_amount > 0 || $discount_percent > 0): ?>
              <tr>
                <td colspan="2"><strong>Discount Applied: <strong><?= number_format($discount_percent) ?>%</strong></strong></td>
                <!-- <td class="text-right"><strong><?= amount_format_pakistani($discount_percent) ?>%</strong></td> -->
                <!-- <td colspan=""><strong>Discount Amount:</strong></td> -->
                <td class="text-right"><strong><?= amount_format_pakistani($discount_amount) ?></strong></td>
              </tr>
              <!-- <tr>
                <td colspan="2"><strong>Discount Amount:</strong></td>
                <td class="text-right"><?= amount_format_pakistani($discount_amount) ?></td>
              </tr> -->
            <?php endif; ?>
<!-- <pre>
<?php print_r($tokenwithshift); ?>
</pre> -->
            


            <?php if (!empty($tokenwithshift) && $tokenwithshift->balance > 0 ): ?>
              <tr>
                <td colspan="2"><strong>Paid Amount:</strong></td>
                <td class="text-right"><strong><?= amount_format_pakistani($paid_amount) ?></strong></td>
              </tr>
              <tr>
                <td colspan="2"><strong>Balance:</strong></td>
                <td class="text-right"><strong><?= amount_format_pakistani($tokenwithshift->balance) ?></strong></td>
              </tr>
            <?php endif; ?>
          </tbody>
        </table>
      </div>
    </div>