<?php defined('BASEPATH') or exit('No direct script access allowed');?>
<style type="text/css">
.list-group-item.list-group-item-success>.badge-price,.list-group-item>.badge-selected{display: none;}.list-group-item.list-group-item-success>.badge-selected{display: block;}
</style>
<form action="#" name="myFrom" id="myForm" class="form-horizontal">
  <div class="form-group">
    <label class="col-sm-2 control-label" style="white-space: nowrap; overflow: hidden">Jenis Produk</label>
    <div class="col-sm-10">
      <select class="form-control" id="category">
        <option value=""> -- Pilih -- </option>
        <?php
        $labels = array();
        foreach ($this->system->produk as $pr_k => $pr_v) {
          if ($pr_v->status != 'on')
            continue;
          $labels[$pr_k] = array($pr_v->form->provider->label,$pr_v->form->voucher->label);
          echo '<option value="'.$pr_k.'">'.html_escape($pr_v->nama).'</option>';
        }
        ?>
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label" id="provider-label" style="white-space: nowrap; overflow: hidden"></label>
    <div class="col-sm-10">
      <select class="form-control" id="provider"></select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-sm-2 control-label" id="voucher-label" style="white-space: nowrap; overflow: hidden"></label>
    <div class="col-sm-10">
      <div class="list-group" id="products" style="margin-bottom: 0;max-height: 196px; overflow-y: scroll;"></div>
    </div>
  </div>
  <div class="form-group" style="margin-bottom:0">
    <div class="col-sm-offset-2 col-sm-10">
      <button type="button" class="btn btn-primary" data-dismiss="modal" id="select-product-btn" data-values="<?php echo base64_encode(json_encode(array()));?>" data-product="" data-provider=""><i class="fa fa-check"></i> Tambahkan</button>
      &nbsp;
      <a href="<?php echo site_url('admin/modules/aplikasi-android/main-page');?>" class="btn btn-default" data-dismiss="modal"><i class="fa fa-remove"></i> Batal</a>
      &nbsp;
      <span class="text-info" style="display: none;padding: 6px 12px;margin-bottom: 0;font-size: 14px;font-weight: 400;line-height: 1.42857143;white-space: nowrap;vertical-align: middle;" id="loading"><i class="fa fa-spinner fa-pulse fa-fw"></i> Mohon tunggu...</span>
    </div>
  </div>
</form>
<script>var button=$("#select-product-btn");var selectedProducts=[];var labels=<?php echo json_encode($labels);?>;function selectProduct(e){var data=JSON.parse(atob($(e).attr("data-value")));if($(e).hasClass('list-group-item-success')){for(var i=0;i<selectedProducts.length;i++){if(selectedProducts[i].id==data.id){selectedProducts.splice(i,1);}}button.attr("data-values",btoa(JSON.stringify(selectedProducts)));$(e).removeClass('list-group-item-success');}else{selectedProducts.push(data);button.attr("data-values",btoa(JSON.stringify(selectedProducts)));$(e).addClass('list-group-item-success');}}$(document).on("change","#category",function(){$("#provider-label").text("");$("#provider").empty();$("#voucher-label").text("");$("#products").empty();selectedProducts=[];button.attr("data-values",btoa(JSON.stringify(selectedProducts)));button.attr("data-product","");button.attr("data-provider","");var selected=$("#category option:selected").val();if(selected!=''){button.attr("data-product",$("#category option:selected").text());$("#provider-label").text(labels[selected][0]);$("#voucher-label").text(labels[selected][1]);$("#loading").css({display:"inline-block"});$.get("<?php echo site_url('admin/modules/aplikasi-android/select-product');?>/"+selected,function(data){if(data.length>0){var options='';options+='<option value="" selected="selected">-- Pilih --</option>';for(var i=0;i<data.length;i++){options+='<option value="'+data[i].id+'">'+data[i].name+'</option>';}$("#provider").html(options);}else{$("#provider").html('<option value="" selected="selected">-- Tidak ada daftar --</option>');}}).fail(function(){alert('Error!')}).always(function(){$("#loading").css({display:"none"})});}});$(document).on("change","#provider",function(){$("#products").empty();selectedProducts=[];button.attr("data-values",btoa(JSON.stringify(selectedProducts)));button.attr("data-provider","");var selected=$("#provider option:selected").val();if(selected!=''){button.attr("data-provider",$("#provider option:selected").text());$("#loading").css({display:"inline-block"});$.get("<?php echo site_url('admin/modules/aplikasi-android/select-product');?>/"+$("#category option:selected").val()+"/"+selected,function(data){if(data.length>0){var options='';for(var i=0;i<data.length;i++){options+='<a href="#" onClick="selectProduct(this);return false;" class="list-group-item" data-value="'+btoa(JSON.stringify(data[i]))+'"><span class="badge badge-selected" style="background-color: #3c763d;"><i class="fa fa-check"></i></span><span class="badge badge-price">'+data[i].price+'</span>'+data[i].name+'</a>';}$("#products").html(options);}else{$("#products").html('<option value="" selected="selected">-- Tidak ada daftar --</option>');}}).fail(function(){alert('Error!')}).always(function(){$("#loading").css({display:"none"})});}});</script>