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

<section class="content">
<?php echo form_open('rewarderRewardRules/update/'.$rule->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>Status</label>
          <select name="status" class="form-control">
            <option value="1" <?php echo $rule->status ? 'selected' : '' ?>>Active</option>
            <option value="0" <?php echo !$rule->status ? 'selected' : '' ?>>Inactive</option>
          </select>
        </div>
      </div>

      <div class="form-group">
        <label>Applies To</label>
        <?php $a = $rule->applies_to ?>
        <select id="applies_to" name="applies_to" class="form-control" required>
          <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-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="<?php echo isset($rule->min_bill_value) ? esc($rule->min_bill_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="<?php echo isset($rule->max_bill_value) ? esc($rule->max_bill_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): ?>
            <?php $sel = ($a==='PRODUCT' && (int)$rule->target_id===(int)$p->id) ? 'selected' : '' ?>
            <option value="<?php echo $p->id ?>" data-shop="<?php echo $p->shop_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)$rule->target_id===(int)$c->id) ? 'selected' : '' ?>
            <option value="<?php echo $c->id ?>" data-shop="<?php echo $c->shop_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)$rule->target_id===(int)$s->id) ? 'selected' : '' ?>
            <option value="<?php echo $s->id ?>" data-shop="<?php echo $s->shop_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)$rule->target_id===(int)$c->id) ? 'selected' : '' ?>
            <option value="<?php echo $c->id ?>" data-shop="<?php echo $c->shop_id ?>" <?php echo $sel ?>><?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" <?php echo $rule->points_type==='PERCENT'?'selected':'' ?>>PERCENT</option>
            <option value="FIXED" <?php echo $rule->points_type==='FIXED'?'selected':'' ?>>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="<?php echo esc($rule->value) ?>" 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="<?php echo isset($rule->reward_points_expire_days) ? (int) $rule->reward_points_expire_days : 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('rewarderRewardRules') ?>" 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/reward_rules/_form_js') ?>
<?= $this->endSection() ?>

