<?= $this->extend('admin/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">
        <?php if (hasPermissions('shop_discounts_manage')): ?>
          <a href="<?php echo url('rewarderDiscounts/add') ?>" class="btn btn-primary btn-sm"><i class="fa fa-plus pr-1"></i> Add</a>
        <?php endif ?>
      </div>
    </div>
  </div>
</section>

<section class="content">
  <div class="card">
    <div class="card-body">
      <table id="example1" class="table table-bordered table-striped">
        <thead>
          <tr>
            <th width="60">ID</th>
            <th>Shop</th>
            <th>Applies To</th>
            <th>Target</th>
            <th width="110">Type</th>
            <th width="110">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
              $shopName = $shops[$d->shop_id] ?? '';
              if ($d->applies_to === 'ALL') {
                $targetName = 'ALL';
              } 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($shopName) ?></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>
                <?php if (hasPermissions('shop_discounts_manage') || hasPermissions('shop_discounts_view')): ?>
                  <a href="<?php echo url('rewarderDiscounts/view/'.$d->id) ?>" class="btn btn-sm btn-default" data-toggle="tooltip" title="View"><i class="fas fa-eye"></i></a>
                <?php endif ?>
                <?php if (hasPermissions('shop_discounts_manage')): ?>
                  <a href="<?php echo url('rewarderDiscounts/edit/'.$d->id) ?>" class="btn btn-sm btn-default" data-toggle="tooltip" title="Edit"><i class="fas fa-edit"></i></a>
                  <a href="<?php echo url('rewarderDiscounts/delete/'.$d->id) ?>" class="btn btn-sm btn-danger" onclick="return confirm('Delete this discount?')" data-toggle="tooltip" title="Delete"><i class="fas fa-trash"></i></a>
                <?php endif ?>
              </td>
            </tr>
          <?php endforeach ?>
        </tbody>
      </table>
    </div>
  </div>
</section>

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

