<?= $this->extend('frontend/layout/default') ?>
<?= $this->section('content') ?>

<section class="content-header">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-6">
        <h1>Reward Rules</h1>
      </div>
      <div class="col-sm-6 text-right">
        <a class="btn btn-primary btn-sm" href="<?php echo url('shopkeeper/rewards/add') ?>"><i class="fa fa-plus pr-1"></i> Add Rule</a>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="container-fluid">
    <div class="card">
      <div class="card-body">
        <table id="example1" class="table table-bordered table-striped">
          <thead>
            <tr>
              <th width="60">ID</th>
              <th>Applies To</th>
              <th>Target</th>
              <th width="120">Type</th>
              <th width="120">Value</th>
              <th width="170">Expiry (Days)</th>
              <th width="160">Bill Range</th>
              <th width="90">Status</th>
              <th width="140">Action</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($rules as $r): ?>
              <?php
                $targetName = '';
                if ($r->applies_to === 'ALL') {
                  $targetName = 'All items/services';
                } else {
                  $map = $lookups[$r->applies_to] ?? [];
                  $targetName = $map[$r->target_id] ?? ('#'.$r->target_id);
                }

                $minBill = isset($r->min_bill_value) ? (float) $r->min_bill_value : 0.0;
                $maxBill = isset($r->max_bill_value) ? (float) $r->max_bill_value : 0.0;
                if ($minBill <= 0 && $maxBill <= 0) {
                  $billRangeLabel = 'No limit';
                } elseif ($minBill > 0 && $maxBill <= 0) {
                  $billRangeLabel = 'From ₹' . rtrim(rtrim(number_format($minBill, 2, '.', ''), '0'), '.');
                } elseif ($minBill <= 0 && $maxBill > 0) {
                  $billRangeLabel = 'Up to ₹' . rtrim(rtrim(number_format($maxBill, 2, '.', ''), '0'), '.');
                } else {
                  $billRangeLabel = '₹' . rtrim(rtrim(number_format($minBill, 2, '.', ''), '0'), '.') . ' - ₹' . rtrim(rtrim(number_format($maxBill, 2, '.', ''), '0'), '.');
                }
              ?>
              <tr>
                <td><?php echo $r->id ?></td>
                <td><?php echo esc($r->applies_to) ?></td>
                <td><?php echo esc($targetName) ?></td>
                <td><?php echo esc($r->points_type) ?></td>
                <td><?php echo esc($r->value) ?></td>
                <td><?php echo isset($r->reward_points_expire_days) ? (int) $r->reward_points_expire_days : 0 ?></td>
                <td><?php echo esc($billRangeLabel) ?></td>
                <td><?php echo $r->status ? 'Active' : 'Inactive' ?></td>
                <td>
                  <a class="btn btn-sm btn-default" href="<?php echo url('shopkeeper/rewards/edit/'.$r->id) ?>"><i class="fas fa-edit"></i></a>
                  <a class="btn btn-sm btn-danger" href="<?php echo url('shopkeeper/rewards/delete/'.$r->id) ?>" onclick="return confirm('Delete this rule?')"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
            <?php endforeach ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</section>

<?= $this->endSection() ?>

