<?= $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>Add Discount</h1>
      </div>
    </div>
  </div>
</section>

<section class="content">
<?php echo form_open('rewarderDiscounts/save', ['class' => 'form-validate', 'autocomplete' => 'off']); ?>
  <div class="card">
    <div class="card-body">
      <div class="form-row">
        <div class="form-group col-md-6">
          <label>Shop</label>
          <?php $defaultShopId = count($shops) === 1 ? (int) ($shops[0]->id ?? 0) : 0 ?>
          <select id="shop_id" name="shop_id" class="form-control select2" required>
            <option value="">Select</option>
            <?php foreach ($shops as $s): ?>
              <?php $sel = $defaultShopId && (int) $s->id === $defaultShopId ? 'selected' : '' ?>
              <option value="<?php echo $s->id ?>" <?php echo $sel ?>><?php echo esc($s->name) ?></option>
            <?php endforeach ?>
          </select>
        </div>
        <div class="form-group col-md-6">
          <label>Status</label>
          <select name="status" class="form-control">
            <option value="1" selected>Active</option>
            <option value="0">Inactive</option>
          </select>
        </div>
      </div>

      <div class="form-group">
        <label>Applies To</label>
        <select id="applies_to" name="applies_to" class="form-control" required>
          <option value="ALL" selected>ALL</option>
          <option value="PRODUCT">PRODUCT</option>
          <option value="PRODUCT_CATEGORY">PRODUCT_CATEGORY</option>
          <option value="SERVICE">SERVICE</option>
          <option value="SERVICE_CATEGORY">SERVICE_CATEGORY</option>
        </select>
      </div>

      <div class="form-group target target-product d-none">
        <label>Product</label>
        <select name="product_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($products as $p): ?>
            <option value="<?php echo $p->id ?>" data-shop="<?php echo $p->shop_id ?>"><?php echo esc($p->name) ?></option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-group target target-product-category d-none">
        <label>Product Category</label>
        <select name="product_category_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($productCategories as $c): ?>
            <option value="<?php echo $c->id ?>" data-shop="<?php echo $c->shop_id ?>"><?php echo esc($c->name) ?></option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-group target target-service d-none">
        <label>Service</label>
        <select name="service_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($services as $s): ?>
            <option value="<?php echo $s->id ?>" data-shop="<?php echo $s->shop_id ?>"><?php echo esc($s->name) ?></option>
          <?php endforeach ?>
        </select>
      </div>
      <div class="form-group target target-service-category d-none">
        <label>Service Category</label>
        <select name="service_category_id" class="form-control select2">
          <option value="">Select</option>
          <?php foreach ($serviceCategories as $c): ?>
            <option value="<?php echo $c->id ?>" data-shop="<?php echo $c->shop_id ?>"><?php echo esc($c->name) ?></option>
          <?php endforeach ?>
        </select>
      </div>

      <div class="form-row">
        <div class="form-group col-md-4">
          <label>Discount Type</label>
          <select name="discount_type" class="form-control">
            <option value="PERCENT" selected>PERCENT</option>
            <option value="AMOUNT">AMOUNT</option>
          </select>
        </div>
        <div class="form-group col-md-4">
          <label>Value</label>
          <input type="number" step="0.01" class="form-control" name="value" value="0" required>
        </div>
        <div class="form-group col-md-4">
          <label>Min Amount</label>
          <input type="number" step="0.01" class="form-control" name="min_amount">
        </div>
      </div>

      <div class="form-row">
        <div class="form-group col-md-6">
          <label>Start at (Y-m-d H:i:s)</label>
          <input type="text" class="form-control" name="start_at">
        </div>
        <div class="form-group col-md-6">
          <label>End at (Y-m-d H:i:s)</label>
          <input type="text" class="form-control" name="end_at">
        </div>
      </div>
    </div>
    <div class="card-footer">
      <div class="row">
        <div class="col"><a href="<?php echo url('rewarderDiscounts') ?>" class="btn btn-flat btn-danger" onclick="return confirm('Leave without saving?')">Cancel</a></div>
        <div class="col text-right"><button type="submit" class="btn btn-flat btn-primary">Save</button></div>
      </div>
    </div>
  </div>
<?php echo form_close(); ?>
</section>

<?= $this->endSection() ?>
<?= $this->section('js') ?>
<?= $this->include('admin/rewarder/discounts/_form_js') ?>
<?= $this->endSection() ?>

