<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>
  <head>
    <title>Produk Okepay</title>
    <?php include_once(VIEWPATH.'includes/head.php'); ?>
  </head>
  <body>
    <?php include_once(VIEWPATH. 'admin/_navbar.php'); ?>
    <div class="container">
      <ul class="breadcrumb">
        <li><a href="<?php echo site_url();?>"><i class="fa fa-home"></i></a></li>
        <li><a href="<?php echo site_url('admin');?>">Admin Panel</a></li>
        <li><a href="<?php echo site_url('admin/modules/okepay');?>">Okepay</a></li>
        <li class="active"><span>Produk</span></li>
      </ul>
      <div class="row">
        <div class="col-xs-12">
          <div class="row">
          <div class="col-xs-12 col-sm-3 col-lg-2">
            <a class="btn btn-default btn-block" id="btn-sinkronisasi" href="<?php echo site_url('admin/modules/okepay/sinkronisasi/'.$server->sv_id);?>" style="margin-bottom: 15px;"><i class="fa fa-refresh"></i> Sinkronkan</a>
          </div>
          <div class="col-xs-12 col-sm-9 col-lg-10">
          <div class="row">
            <div class="col-sm-4 col-lg-3">
              <div class="input-group" style="margin-bottom: 15px;">
                <span class="input-group-addon"><i class="fa fa-tags"></i></span>
                <select class="form-control jump"><?php foreach (array_keys($categories) as $category):?><option value="<?php echo site_url('admin/modules/okepay/produk/'.$server->sv_id.'?jenis='.urlencode($category));?>"<?php echo $jenis == $category ? ' selected="selected"' : '';?>><?php echo html_escape($category);?></option><?php endforeach;?></select>
              </div>
            </div>
            <div class="col-sm-8 col-lg-9">
              <div class="input-group" style="margin-bottom: 15px;">
                <span class="input-group-btn"><button class="btn btn-default" type="button" id="btn-prev"><i class="fa fa-arrow-left"></i></button></span>
                <select id="filter" class="form-control jump"><?php foreach ($categories as $category => $providers):?><optgroup label="<?php echo $category;?>"><?php foreach ($providers as $pcode => $pname):?><option value="<?php echo site_url('admin/modules/okepay/produk/'.$server->sv_id.'?jenis='.urlencode($category).'&kode='.urlencode($pcode));?>"<?php echo $jenis == $category && $kode == $pcode ? ' selected="selected"' : '';?>><?php echo html_escape($pname);?></option><?php endforeach;?></optgroup><?php endforeach;?></select>
                <span class="input-group-btn"><button class="btn btn-default" type="button" id="btn-next"><i class="fa fa-arrow-right"></i></button></span>
              </div>
            </div>
          </div>
          </div>
          </div>
          <?php echo form_open(site_url('admin/modules/okepay/simpan-produk/'.$server->sv_id), 'id="produkForm"');?>
          <?php echo form_hidden('jenis', $jenis); ?>
          <?php echo form_hidden('kode', $kode); ?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3><i class="fa fa-list-alt"></i> <?php echo html_escape($categories[$jenis][$kode]);?></h3>
            </div>
            <div class="table-responsive">
              <table class="table table-bordered table-hover">
                <thead>
                  <tr>
                    <th class="text-center" style="width: 40px; min-width: 40px;"><input type="checkbox" id="select-all"/></th>
                    <th style="width: 80px; min-width: 80px;">Kode</th>
                    <th style="width: 120px; min-width: 120px;">Produk</th>
                    <th style="width: 140px; min-width: 140px;">Operator</th>
                    <th style="min-width: 180px;">Nama Voucher</th>
                    <th colspan="3" class="text-center">Harga</th>
                    <th style="text-align: center; width: 54px;">Status</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($vouchers as $vcode=>$vdata):?>
                  <tr class="<?php echo !$vdata['status'] ? 'text-danger' : '';?>" id="item-<?php echo $vcode;?>">
                    <td class="text-center" style="vertical-align: middle"><input data-kode="<?php echo html_escape($vcode);?>" type="checkbox" class="checkbox1" <?php echo (!is_null($vdata['vo_id']) ? 'checked="checked"' : '');?> /></td>
                    <td style="vertical-align: middle;white-space: nowrap"><?php echo (!is_null($vdata['vo_id']) ? '<a href="'.site_url('admin/produk/edit-voucher/'.$vdata['vo_id']).'" data-toggle="modal" data-target="#myModal">'.(@$vdata['vo_ppob_cek'] ? '<i class="fa fa-credit-card"></i> ' : '').html_escape($vcode).'</a>' : html_escape($vcode));?></td>
                    <td style="vertical-align: middle;"><?php if (!is_null($vdata['op_produk'])):?><a href="<?php echo site_url('admin/produk/browse/'.$vdata['op_produk']);?>" target="view"><?php echo html_escape($this->system->produk->{$vdata['op_produk']}->nama);?></a><input data-kode="<?php echo html_escape($vcode);?>" type="hidden" name="items[<?php echo html_escape($vcode);?>][produk]" value="<?php echo $vdata['op_produk'];?>" required="required" /><?php else:?><select data-kode="<?php echo html_escape($vcode);?>" name="items[<?php echo html_escape($vcode);?>][produk]" class="form-control input-sm data-produk" disabled="disabled"><option value="">-- Pilih --</option><?php foreach ($this->system->produk as $pr_k=>$pr_v):?><option value="<?php echo $pr_k;?>"<?php echo (!is_null($vdata['op_produk']) && $vdata['op_produk'] == $pr_k ? ' selected="selected"' : '')?>><?php echo html_escape($pr_v->nama);?></option><?php endforeach;?></select><?php endif;?></td>
                    <td style="vertical-align: middle;">
                      <?php if (!is_null($vdata['op_nama'])):?>
                      <a href="<?php echo site_url('admin/produk/browse/'.$vdata['op_produk'].'/'.$vdata['op_slug']);?>" target="view"><span style="white-space: nowrap;"><?php echo html_escape($vdata['op_nama']);?></span></a>
                      <input data-kode="<?php echo html_escape($vcode);?>" type="hidden" name="items[<?php echo html_escape($vcode);?>][operator]" value="<?php echo $vdata['op_id'];?>" required="required" />
                      <?php else:?>
                      <select data-kode="<?php echo html_escape($vcode);?>" name="items[<?php echo html_escape($vcode);?>][operator]" class="form-control input-sm data-operator" data-operator="<?php echo html_escape($categories[$jenis][$kode]);?>" disabled="disabled"></select>
                      <?php endif;?>
                    </td>
                    <td style="vertical-align: middle;">
                      <input data-kode="<?php echo html_escape($vcode);?>" type="text" name="items[<?php echo html_escape($vcode);?>][nama]" class="form-control input-sm data-nama" value="<?php echo html_escape(!is_null($vdata['vo_nominal']) ? $vdata['vo_nominal'] : $vdata['keterangan']);?>" maxlength="64" <?php echo (!is_null($vdata['vo_id']) ? 'required="required"' : 'disabled="disabled"');?> data-nama="<?php echo html_escape($vdata['keterangan']);?>"/>
                    </td>
                    <td style="vertical-align: middle; width: 100px; min-width: 100px;"><input data-kode="<?php echo html_escape($vcode);?>" data-toggle="tooltip" title="harga beli" type="number" name="items[<?php echo html_escape($vcode);?>][harga_beli]" class="form-control input-sm" value="<?php echo $vdata['harga'];?>" readonly="readonly" <?php echo (!is_null($vdata['vo_id']) ? 'required="required"' : 'disabled="disabled"');?>/></td>
                    <td style="vertical-align: middle; width: 100px; min-width: 100px;"><input data-kode="<?php echo html_escape($vcode);?>" data-toggle="tooltip" title="harga jual untuk umum" type="number" name="items[<?php echo html_escape($vcode);?>][harga_jual]" class="form-control input-sm data-humum" min="<?php echo $vdata['harga'];?>" data-mumum="<?php echo $vdata['vo_harga'] - $vdata['harga'];?>" data-mreseller="<?php echo $vdata['vo_harga_reseller'] - $vdata['harga'];?>" value="<?php echo $vdata['vo_harga'];?>" <?php echo (!is_null($vdata['vo_id']) ? 'required="required"' : 'disabled="disabled"');?>/></td>
                    <td style="vertical-align: middle; width: 100px; min-width: 100px;"><input data-kode="<?php echo html_escape($vcode);?>" data-toggle="tooltip" title="harga jual untuk reseller" type="number" name="items[<?php echo html_escape($vcode);?>][harga_jual_reseller]" class="form-control input-sm data-hreseller" min="<?php echo $vdata['harga'];?>" max="<?php echo $vdata['vo_harga'];?>" value="<?php echo $vdata['vo_harga_reseller'];?>" <?php echo (!is_null($vdata['vo_id']) ? 'required="required"' : 'disabled="disabled"');?>/></td>
                    <td style="text-align: center; vertical-align: middle;"><?php if ($vdata['status']):?><span class="text-success" data-toggle="tooltip" data-title="Normal" data-original-title="" title=""><i class="fa fa-check"></i></span><?php else:?><span class="text-danger" data-toggle="tooltip" data-title="Gangguan / Kosong" data-original-title="" title=""><i class="fa fa-times"></i></span><?php endif;?></td>
                  </tr>
                  <?php endforeach;?>
                </tbody>
              </table>
            </div>
          </div>
          <div style="margin-bottom: 20px;">
            <button type="submit" class="btn btn-primary"><i class="fa fa-check"></i> Simpan yang ditandai</button>
          </div>
          <?php echo form_close();?>
        </div>
      </div>
    </div>
    <div id="mset" style="display: none;">
      <div class="modal-header">
        <h4 class="modal-title">Edit Default Mark Up</h4>
      </div>
      <div class="modal-body">
        <div class="form-group">
          <label>Mark up harga jual untuk umum</label>
          <div class="input-group">
            <span class="input-group-addon">Rp</span>
            <input class="form-control" type="number" name="mu" value="" placeholder="500"/>
          </div>
          <p class="help-block">Selisih antara harga beli dan harga jual untuk umum.</p>
        </div>
        <div class="form-group" style="margin-bottom: 0;">
          <label>Mark up harga jual untuk reseller</label>
          <div class="input-group">
            <span class="input-group-addon">Rp</span>
            <input class="form-control" type="number" name="mr" value="" placeholder="400"/>
          </div>
          <p class="help-block">Selisih antara harga beli dan harga jual untuk reseller.</p>
        </div>
      </div>
      <div class="modal-footer">
        <button class="btn btn-primary" type="button" data-dismiss="modal" id="msave"><i class="fa fa-check"></i> Simpan</button>
      </div>
    </div>
    <?php include_once(VIEWPATH.'includes/foot.php'); ?>
    <script type="text/javascript">
      $("#btn-sinkronisasi").click(function(){var btn=$(this);btn.attr("disabled","disabled").html('<i class="fa fa-refresh fa-spin"></i> Menyinkronkan');$.get($(this).attr('href'),function(data){if(data.updated>0){$.notify(data.updated+" produk berhasil diperbarui.","success");}else{$.notify("Tidak ada produk yang diperbarui","info");}}).fail(function(){$.notify("Terjadi kesalahan ketika menyinkronkan produk.","error");}).done(function(data){btn.removeAttr("disabled").html('<i class="fa fa-refresh"></i> Sinkronisasi');$.notify("Sinkronisasi selesai","success");});return false;});
      var providers = <?php echo json_encode(array_fill_keys(array_keys((array)$this->system->produk), null));?>;
      var produkChanged = false;
      
      var dUmum = 0;
      var dReseller = 0;
      var openSetMarkup = false;
      $(document.body).on("show.bs.modal",function(){if(openSetMarkup){setTimeout(function(){$(".modal-content").html($("#mset").html());},500);}});
      $(document).on("change",'[name="mu"], [name="mr"]',function(){var val=$(this).val();if(val.length>0){val=parseInt(val);sessionStorage.setItem($(this).attr("name"),val);}});
      $(document).on("click","#msave",function(){var mu=sessionStorage.getItem('mu');var mr=sessionStorage.getItem('mr');if(mu!=null&&mr!=null){mu=parseInt(mu);mr=parseInt(mr);if(mr>mu){mr=mu;sessionStorage.setItem('mr',mr);}updateAllPrice(mu,mr);$.notify("Default harga markup disimpan.","success");}});
      function updateDx(type,value){if(type=='u'){dUmum=value;}if(type=='r'){dReseller=value;}}
      $('[data-mumum]').each(function(){xU=parseInt($(this).attr('data-mumum'));xR=parseInt($(this).attr('data-mreseller'));if(xU!=0){updateDx('u',xU);updateDx('r',xR);return true;}});
      if(dUmum==0){var xu=sessionStorage.getItem('mu');var xr=sessionStorage.getItem('mr');if(xu==null||xr==null){openSetMarkup=true;$('#myModal').modal({backdrop:'static',keyboard:false});}else{updateAllPrice(parseInt(xu),parseInt(xr));}}else{updateAllPrice(dUmum,dReseller);}
      function updateAllPrice(mUmum,mReseller){$('[data-mumum="0"]').each(function(){var kode=$(this).attr('data-kode');var hBeli=parseInt($(this).attr('min'));$(this).val(hBeli+mUmum);var inpRs=$('[name="items['+kode+'][harga_jual_reseller]"]');inpRs.attr('max',hBeli+mUmum);inpRs.val(hBeli+mReseller);});}
      $('select.data-produk').change(function(){var kode=$(this).attr('data-kode');$('[name="items['+kode+'][operator]"]').html('');var value=$(this).find('option:selected').val();if(value!=""&&typeof providers[value]!="undefined"){setProviders($(this).attr("data-kode"),value,produkChanged);}if(produkChanged==false){$('select.data-produk').each(function(){$(this).val(value);});produkChanged=true;}});
      function escapeHtml(unsafe){return unsafe.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;").replace(/'/g,"&#039;");}
      function updateProviders(produk,data){providers[produk]=data;}
      function setProviders(kode,produk,produkChanged){if(providers[produk]==null){$.get("<?php echo site_url('admin/modules/okepay/produk/'.$server->sv_id);?>/"+produk,function(response){updateProviders(produk,response);setProviders(kode,produk,produkChanged);});}else if(providers[produk]!=null){var option;option+='<option value="0">[+] <?php echo html_escape($categories[$jenis][$kode]);?> (<?php echo html_escape($kode);?>)</option>';if(providers[produk].length>0){for(var i=0;i<providers[produk].length;i++){option+='<option value="'+providers[produk][i]['id']+'" '+('<?php echo html_escape($kode);?>'==providers[produk][i]['kode']?'selected="selected"':'')+'>'+escapeHtml(providers[produk][i]['nama'])+' ('+providers[produk][i]['kode']+')</option>';}}if(produkChanged==false){$('select.data-operator').each(function(){$(this).html(option);});}else{$('select[name="items['+kode+'][operator]"]').html(option);}}}
      function setRequired(selector,checked){var input=$('[name^="items['+selector+']"]');if(checked){input.attr("required","required");input.removeAttr("disabled");}else{input.removeAttr("required");input.attr("disabled","disabled");}}
      $('.data-nama').on('input',function(e){var th=$(this);if(th.val().length==0){th.val(th.attr("data-nama"));th.attr("disabled","disabled");setTimeout(function(){th.removeAttr("disabled");},500);}});
      $('.data-humum').on('input',function(e){var kode=$(this).attr("data-kode");var inpRs=$('[name="items['+kode+'][harga_jual_reseller]"]');var value=$(this).val();if(value.length==0){inpRs.val('');return;}var hBeli=parseInt($(this).attr("min"));var mUmum=parseInt($(this).attr("data-mumum"));var mReseller=parseInt($(this).attr("data-mreseller"));var hJual=parseInt(value);var mUmumNew=hJual-hBeli;var mResellerNew=mUmumNew-(mUmum-mReseller);var hReseller=hBeli+mResellerNew;if(hReseller<hBeli||hReseller>hJual){hReseller=hJual;}inpRs.attr("max",hJual);inpRs.val(hReseller);});
      $('select.jump').change(function(){window.location.href=$(this).find("option:selected").val();});
      $("#btn-prev").click(function(){var option=$('#filter option:selected').prev();if(option.length>0){window.location.href=option.val();}else{var parent=$('#filter option:selected').parent().prev();if(parent.length>0){window.location.href=parent.find("option:last-child").val();}else{window.location.href=$('#filter optgroup:last-child option:last-child').val();}}return false;});
      $("#btn-next").click(function(){var option=$('#filter option:selected').next();if(option.length>0){window.location.href=option.val();}else{var parent=$('#filter option:selected').parent().next();if(parent.length>0){window.location.href=parent.find("option:first-child").val();}else{window.location.href=$('#filter optgroup:first-child option:first-child').val();}}return false;});
      $('#select-all').click(function(event){if(this.checked){$('.checkbox1').each(function(){this.checked=true;setRequired($(this).attr("data-kode"),true);});}else{$('.checkbox1').each(function(){this.checked=false;setRequired($(this).attr("data-kode"),false);});}});
      $('#produkForm :checkbox').change(function(){if(this.checked){setRequired($(this).attr("data-kode"),true);}else{setRequired($(this).attr("data-kode"),false);}});
      $(document).on("submit","#produkForm",function(){if($(this).serialize().indexOf('items')<=0){return false;}});
    </script>
  </body>
</html>