<?= $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>Discounts</h1>
      </div>
      <div class="col-sm-6 text-right">
        <a class="btn btn-primary btn-sm" href="<?php echo url('shopkeeper/discounts/add') ?>"><i class="fa fa-plus pr-1"></i> Add Discount</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">Start</th>
              <th width="170">End</th>
              <th width="90">Status</th>
              <th width="140">Action</th>
            </tr>
          </thead>
          <tbody>
            <?php foreach ($discounts as $d): ?>
              <?php
                $targetName = '';
                if ($d->applies_to === 'ALL') {
                  $targetName = 'All items/services';
                } else {
                  $map = $lookups[$d->applies_to] ?? [];
                  $targetName = $map[$d->target_id] ?? ('#'.$d->target_id);
                }
              ?>
              <tr>
                <td><?php echo $d->id ?></td>
                <td><?php echo esc($d->applies_to) ?></td>
                <td><?php echo esc($targetName) ?></td>
                <td><?php echo esc($d->discount_type) ?></td>
                <td><?php echo esc($d->value) ?></td>
                <td><?php echo esc($d->start_at) ?></td>
                <td><?php echo esc($d->end_at) ?></td>
                <td><?php echo $d->status ? 'Active' : 'Inactive' ?></td>
                <td>
                  <a class="btn btn-sm btn-default" href="<?php echo url('shopkeeper/discounts/edit/'.$d->id) ?>"><i class="fas fa-edit"></i></a>
                  <a class="btn btn-sm btn-danger" href="<?php echo url('shopkeeper/discounts/delete/'.$d->id) ?>" onclick="return confirm('Delete this discount?')"><i class="fas fa-trash"></i></a>
                </td>
              </tr>
            <?php endforeach ?>
          </tbody>
        </table>
      </div>
    </div>
  </div>
</section>

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

