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

<section class="content">
  <div class="container-fluid">
    <?php echo form_open('shopkeeper/rewards/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>Applies To</label>
              <select name="applies_to" id="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 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-row">
            <div class="form-group col-md-6">
              <label>Min Bill Value (₹)</label>
              <input type="number" min="0" step="1" class="form-control" name="min_bill_value" value="0">
              <small class="form-text text-muted">0 means no limit.</small>
            </div>
            <div class="form-group col-md-6">
              <label>Max Bill Value (₹)</label>
              <input type="number" min="0" step="1" class="form-control" name="max_bill_value" value="0">
              <small class="form-text text-muted">0 means no limit.</small>
            </div>
          </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 ?>"><?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 ?>"><?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 ?>"><?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 ?>"><?php echo esc($c->name) ?></option>
              <?php endforeach ?>
            </select>
          </div>

          <div class="form-row">
            <div class="form-group col-md-6">
              <label>Points Type</label>
              <select name="points_type" class="form-control">
                <option value="PERCENT" selected>PERCENT</option>
                <option value="FIXED">FIXED</option>
              </select>
            </div>
            <div class="form-group col-md-6">
              <label>Value</label>
              <input type="number" step="0.01" class="form-control" name="value" value="0" required>
            </div>
          </div>
          <div class="form-row">
            <div class="form-group col-md-6">
              <label>Reward Points Expire in Days</label>
              <input type="number" min="0" step="1" class="form-control" name="reward_points_expire_days" value="0">
              <small class="form-text text-muted">0 means points do not expire.</small>
            </div>
          </div>
        </div>
        <div class="card-footer">
          <div class="row">
            <div class="col"><a href="<?php echo url('shopkeeper/rewards') ?>" 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(); ?>
  </div>
</section>

<?= $this->endSection() ?>
<?= $this->section('js') ?>
<script>
  function updateTargetVisibility() {
    var val = $('#applies_to').val();
    $('.target').addClass('d-none');
    if (val === 'PRODUCT') $('.target-product').removeClass('d-none');
    if (val === 'PRODUCT_CATEGORY') $('.target-product-category').removeClass('d-none');
    if (val === 'SERVICE') $('.target-service').removeClass('d-none');
    if (val === 'SERVICE_CATEGORY') $('.target-service-category').removeClass('d-none');
  }
  $(document).ready(function() {
    $('.form-validate').validate();
    $('.select2').select2();
    updateTargetVisibility();
    $('#applies_to').on('change', updateTargetVisibility);
  });
</script>
<?= $this->endSection() ?>

