    <div class="card">
      <div class="card-header">
<div class="row align-items-center">
    <!-- Icon + Text -->
    <div class="col-md-9 d-flex align-items-center">
<i class="material-icons-two-tone text-primary f-46 me-3">confirmation_number</i>


        <div>
            <h5 class="mb-0">
                List of all Token / Patiens with actions
            </h5>
            <small>You can Edit or Delete (depending on rights)</small>
        </div>
    </div>

    <!-- Button -->
    <div class="col-md-3 text-end">
        <button class="btn btn-primary btn-sm btn-round"
                data-bs-toggle="modal"
                data-bs-target="#addTokenModal">
            Create New Token
        </button>
    </div>
<!--     <div class="col-md-3 text-end">
    <a href="<?= base_url('admin/tokens/add'); ?>"
       class="btn btn-primary btn-sm btn-round">
        Create New Token
    </a>
</div> -->

</div>
</div>
    <!-- <pre>
<?php
print_r($tokenwithshift);
?>
</pre> -->
      <div class="card-body table-responsive">
        <table id="services-table" class="table table-striped table-bordered nowrap">

          <thead>
    <tr>
      <th>ID</th>
      <!-- <th><i class="ph-duotone ph-ticket"></i></th> -->
      <th>Token</th>
      <!-- <th>Shift</th> -->
      <th>Name</th>
      <th>Contact</th>
      <!-- <th>Age</th> -->
      <!-- <th>Gender</th> -->
      <th>Services</th>
      <th>Payments</th>
      <th>Updated At</th>
      <th>Actions</th>
    </tr>
  </thead>
            <tbody>
    <?php foreach($tokenwithshift as $row): ?>
      <tr>
        <td><?= $row->id ?></td>
        <td class="text-end">
          <span class="text-left">
  <?= $row->token_number ?>          
          </span>
  

  <span class="badge <?php
      switch (strtolower($row->shift_name)) {
        case 'morning':
          echo 'bg-light-success'; // green
          break;
        case 'afternoon':
          echo 'bg-light-warning'; // yellow/orange
          break;
        case 'evening':
          echo 'bg-light-primary'; // blue
          break;
        case 'night':
          echo 'bg-light-dark'; // dark/black
          break;
        default:
          echo 'bg-light-secondary'; // gray fallback
          break;
      }
    ?> me-1">
    <i data-feather="clock" class="me-25"></i>
    <?= htmlspecialchars($row->shift_name ?? 'No Shift') ?>
  </span>

</td>
        <?php
  $unit = strtolower($row->age_unit);
  $badgeClass = 'bg-light-secondary'; // default color
  $ageNumber = (int)$row->age;

  if ($unit === 'years' || $unit === 'year' || $unit === 'y') {
      $badgeClass = 'bg-light-primary';  // Blue for Years
      $unitText = 'Years';
  } elseif ($unit === 'months' || $unit === 'month' || $unit === 'm') {
      $badgeClass = 'bg-light-warning';  // Yellow for Months
      $unitText = 'Months';
  } elseif ($unit === 'days' || $unit === 'day' || $unit === 'd') {
      $badgeClass = 'bg-light-light-info';     // Light blue for Days
      $unitText = 'Days';
  } else {
      $badgeClass = 'bg-light-dark';     // Dark for others
      $unitText = htmlspecialchars($row->age .' - '. $row->age_unit);
      $ageNumber = '';  // Because age included in unitText already
  }
?>
        <!-- <td><?= $row->shift_name ?? 'No Shift' ?></td> -->
        <td>
  <?php if (strtolower($row->gender) === 'male'): ?>
    <!-- <i class="fa fa-mars" aria-hidden="true" style="color:blue;"></i> -->
      <span class="badge me-1" style="background: #0066ff36;color: #2674e3; font-size: 15px;">
    <i class="fa fa-mars" aria-hidden="true"></i>
  </span>
  <?php elseif (strtolower($row->gender) === 'female'): ?>
    <!-- <i class="fa fa-venus" aria-hidden="true" style="color:pink;"></i> -->
     <span class="badge me-1" style="background: #ffc0cba8;color: #ff5dae;font-size: 15px;">
    <i class="fa fa-venus" aria-hidden="true"></i>
  </span>
  <?php else: ?>
    <?= htmlspecialchars($row->gender) ?>
  <?php endif; ?>

          <?= $row->name ?>

<span style="float: right;">
  <span class="badge <?= $badgeClass ?>">
    <?= $ageNumber ?> <?= $unitText ?>
  </span>



</span>


 





</td>
        <td><?= $row->phone ?></td>
   


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

        $deptName        = strtolower(trim($deptName));
        $subServiceName  = trim($subServiceName);
        $consultantName  = trim($consultantName);

        // helpers
        $byConsultant = $consultantName !== '' ? " By {$consultantName}" : '';

        if ($deptName === 'opd') {
            return trim("OPD");

        } elseif ($deptName === 'lab') {
            return trim("Lab");

        } elseif ($deptName === 'ultrasound') {
            return trim("Ultrasound");

        } elseif ($deptName === 'physiotherapy') {
            return trim("Physiotherapy");

        } elseif ($deptName === 'clinical services') {
            return trim("clinical services");

        } else {
            // fallback (unknown department)
            return trim("{$deptName}");
        }
    }
}
?>

<?php
if (!function_exists('serviceBadgeMeta')) {
    function serviceBadgeMeta($dept) {
        $dept = strtolower(trim($dept));

        switch ($dept) {
            case 'opd':
                return ['class' => 'bg-light-primary', 'icon' => 'user'];
            case 'lab':
                return ['class' => 'bg-light-success', 'icon' => 'activity'];
            case 'ultrasound':
                return ['class' => 'bg-light-warning text-dark', 'icon' => 'radio'];
            case 'physiotherapy':
                return ['class' => 'bg-light-info', 'icon' => 'heart'];
            case 'clinical services':
                return ['class' => 'bg-light-secondary', 'icon' => 'briefcase'];
            default:
                return ['class' => 'bg-secondary', 'icon' => 'tag'];
        }
    }
}
?>
<td>
<?php if (!empty($row->services)): ?>
  <div class="d-flex flex-wrap gap-1">
    <?php foreach ($row->services as $srv): ?>
      <?php
        $meta = serviceBadgeMeta($srv['department_name'] ?? '');
      ?>
      <span class="badge <?= $meta['class'] ?>">
        <i data-feather="<?= $meta['icon'] ?>" class="me-25"></i>
        <?= html_escape(
            formatServiceName(
                $srv['department_name'] ?? '',
                $srv['sub_service_name'] ?? '',
                $srv['service_name'] ?? ''
            )
        ) ?>
      </span>
    <?php endforeach; ?>
  </div>
<?php else: ?>
  <span class="text-muted">No services</span>
<?php endif; ?>
</td>


<td class="text-end">
<?php
$paid_amount = $row->total_fee - $row->balance - $row->refund_amount;
?>

<?= amount_format_pakistani($paid_amount . ' ') ?> 


<?php if ($row->balance > 0 && $row->total_fee != $row->balance): ?>
  <span class="badge bg-light-warning text-dark me-1">
    <i data-feather="alert-circle" class="me-25 text-light-warning"></i>
    <?= amount_format_pakistani($row->balance) ?>
  </span>

<?php elseif ($row->total_fee == $row->balance && $row->balance > 0): ?>
  <span class="badge bg-light-danger me-1">
    <i data-feather="x-circle" class="me-25"></i>
    Un-Paid
  </span>

<?php elseif ($row->balance == 0): ?>
  <span class="badge bg-light-success me-1">
    <i data-feather="check-circle" class="me-25"></i>
    Paid
  </span>
<?php endif; ?>





</td>

        <td>
  <?= date('j-M-Y | h:i A', strtotime($row->created_at)) ?>
</td>
        <td class="text-right">
          <!-- <a href="<?= base_url("admin/tokens/bill_print_view/$row->id") ?>" class="badge  bg-light-info"><i data-feather="printer"></i></a> -->
          <a href="<?= base_url("admin/tokens/view/$row->id") ?>" class="badge  bg-light-success"><i data-feather="eye"></i></a>
          <a href="#" class="badge  bg-light-warning edit-token-btn"
                       data-id="<?= $row->id ?>"
                        data-token_number="<?= $row->token_number ?>"
                        data-name="<?= htmlspecialchars($row->name) ?>"
                        data-phone="<?= $row->phone ?>"
                        data-gender="<?= $row->gender ?>"
                        data-age="<?= $row->age ?>"
                        data-age_unit="<?= $row->age_unit ?>">
                   <i data-feather="edit"></i>
          </a>
          <a href="javascript:void(0);" class="badge  bg-light-danger delete-confirmation-modal"
             data-url="<?= base_url('admin/tokens/delete/' . $row->id) ?>">
          <i data-feather="trash"></i>
          </a>

          <!-- <a href="<?= base_url("admin/tokens/delete/$row->id") ?>" class="label label-danger"><i class="fa fa-trash-o"></i></a> -->
        </td>
      </tr>
    <?php endforeach; ?>
            </tbody>
        </table>
      </div>
