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

<section class="content">
  <div class="container-fluid">
    <?php echo form_open('shopkeeper/discounts/update/'.$discount->id, ['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>
                <?php $a = $discount->applies_to ?>
                <option value="ALL" <?php echo $a==='ALL'?'selected':'' ?>>ALL</option>
                <option value="PRODUCT" <?php echo $a==='PRODUCT'?'selected':'' ?>>PRODUCT</option>
                <option value="PRODUCT_CATEGORY" <?php echo $a==='PRODUCT_CATEGORY'?'selected':'' ?>>PRODUCT_CATEGORY</option>
                <option value="SERVICE" <?php echo $a==='SERVICE'?'selected':'' ?>>SERVICE</option>
                <option value="SERVICE_CATEGORY" <?php echo $a==='SERVICE_CATEGORY'?'selected':'' ?>>SERVICE_CATEGORY</option>
              </select>
            </div>
            <div class="form-group col-md-6">
              <label>Status</label>
              <select name="status" class="form-control">
                <option value="1" <?php echo $discount->status ? 'selected' : '' ?>>Active</option>
                <option value="0" <?php echo !$discount->status ? 'selected' : '' ?>>Inactive</option>
              </select>
            </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): ?>
                <?php $sel = ($a==='PRODUCT' && (int)$discount->target_id===(int)$p->id) ? 'selected' : '' ?>
                <option value="<?php echo $p->id ?>" <?php echo $sel ?>><?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): ?>
                <?php $sel = ($a==='PRODUCT_CATEGORY' && (int)$discount->target_id===(int)$c->id) ? 'selected' : '' ?>
                <option value="<?php echo $c->id ?>" <?php echo $sel ?>><?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): ?>
                <?php $sel = ($a==='SERVICE' && (int)$discount->target_id===(int)$s->id) ? 'selected' : '' ?>
                <option value="<?php echo $s->id ?>" <?php echo $sel ?>><?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): ?>
                <?php $sel = ($a==='SERVICE_CATEGORY' && (int)$discount->target_id===(int)$c->id) ? 'selected' : '' ?>
                <option value="<?php echo $c->id ?>" <?php echo $sel ?>><?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" <?php echo $discount->discount_type==='PERCENT'?'selected':'' ?>>PERCENT</option>
                <option value="AMOUNT" <?php echo $discount->discount_type==='AMOUNT'?'selected':'' ?>>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="<?php echo esc($discount->value) ?>" 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" value="<?php echo esc($discount->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" value="<?php echo esc($discount->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" value="<?php echo esc($discount->end_at) ?>">
            </div>
          </div>
        </div>
        <div class="card-footer">
          <div class="row">
            <div class="col"><a href="<?php echo url('shopkeeper/discounts') ?>" 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);
    var opts = {
      singleDatePicker: true,
      timePicker: true,
      timePicker24Hour: true,
      timePickerSeconds: true,
      locale: { format: 'YYYY-MM-DD HH:mm:ss' }
    };
    $('input[name="start_at"]').daterangepicker(opts);
    $('input[name="end_at"]').daterangepicker(opts);
  });
</script>
<?= $this->endSection() ?>

