<?php defined('BASEPATH') or exit('No direct script access allowed');
$voucher_couriers = json_decode($voucher->vo_couriers, true); ?>
<!DOCTYPE html>
<html>

<head>
  <title>Admin &gt; Edit <?php echo $this->system->produk->{$voucher->op_produk}->nama; ?> <?php echo $voucher->op_nama; ?> <?php echo $voucher->vo_nominal; ?></title>
  <?php include_once(VIEWPATH . 'includes/head.php'); ?>
  <link href="<?php echo base_url('assets/css/summernote.css'); ?>" rel="stylesheet" />
  <style type="text/css">
    .note-group-select-from-files {
      display: none;
    }
  </style>
  <style type="text/css">
    #shipping-address>div:nth-child(2) {
      margin-top: 0
    }
  </style>
</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/produk'); ?>">Produk</a></li>
      <li><a href="<?php echo site_url('admin/produk/browse/' . $voucher->op_produk); ?>"><?php echo $this->system->produk->{$voucher->op_produk}->nama; ?></a></li>
      <li><a href="<?php echo site_url('admin/produk/browse/' . $voucher->op_produk . '/' . $voucher->op_slug . '/' . $this->uri->segment(5)); ?>"><?php echo $voucher->op_nama; ?></a></li>
      <li><span><?php echo html_escape($voucher->vo_nominal); ?></span></li>
    </ul>
    <div class="row">
      <div class="col-sm-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3><i class="fa fa-edit"></i> <?php echo html_escape($voucher->vo_nominal); ?></h3>
          </div>
          <div class="panel-body" style="padding-bottom: 0;">
            <?php echo form_open(current_url(), 'name="editForm" id="editForm"'); ?>
            <div class="row">
              <div class="col-sm-6 col-lg-4">
                <div class="form-group">
                  <label>Server <span class="text-danger">*</span></label>
                  <select class="form-control" name="server">
                    <option value="">-- Pilih --</option>
                    <?php foreach ($daftar_server as $server) : ?>
                      <option value="<?php echo $server->sv_id ?>" <?php echo set_select('server', $server->sv_id, $voucher->sv_id == $server->sv_id ? true : false); ?>><?php echo html_escape($server->sv_nama) ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Kode Server <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" onkeyup="updateKodeTrx(value)" name="kode" value="<?php echo set_value('kode', $voucher->vo_kode); ?>" placeholder="<?php echo html_escape($voucher->vo_kode); ?>" maxlength="32" required="required" />
                </div>
                <div class="form-group">
                  <label>Kode Trx <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" id="kode_trx" name="kode_trx" value="<?php echo set_value('kode_trx', $voucher->vo_kode_trx); ?>" placeholder="<?php echo html_escape($voucher->vo_kode_trx); ?>" maxlength="32" required="required" />
                </div>
                <div class="form-group">
                  <label><?php echo html_escape($this->system->produk->{$voucher->op_produk}->form->voucher->label); ?> <span class="text-danger">*</span></label>
                  <input type="text" class="form-control" name="nominal" value="<?php echo set_value('nominal', $voucher->vo_nominal); ?>" placeholder="<?php echo html_escape($voucher->vo_nominal); ?>" maxlength="120" required="required" />
                </div>
                <div class="form-group">
                  <label>Harga Beli</label>
                  <input type="number" class="form-control" id="harga_beli" name="harga_beli" value="<?php echo set_value('harga_beli', $voucher->vo_harga_beli); ?>" placeholder="<?php echo $voucher->vo_harga_beli; ?>" />
                </div>
                <div class="form-group">
                  <label>Harga Jual Umum <span class="text-danger">*</span></label>
                  <input type="number" class="form-control" id="harga" onkeyup="updateHargaReseller(value)" name="harga" value="<?php echo set_value('harga', $voucher->vo_harga); ?>" data-harga="<?php echo $voucher->vo_harga; ?>" placeholder="<?php echo $voucher->vo_harga; ?>" required="required" />
                </div>
                <div class="form-group">
                  <label>Harga Jual Reseller <span class="text-danger">*</span></label>
                  <input type="number" class="form-control" id="harga_reseller" name="harga_reseller" value="<?php echo set_value('harga_reseller', ($voucher->vo_harga_reseller ? $voucher->vo_harga_reseller : $voucher->vo_harga)); ?>" data-harga="<?php echo $voucher->vo_harga_reseller ? $voucher->vo_harga_reseller : $voucher->vo_harga; ?>" placeholder="<?php echo $voucher->vo_harga_reseller ? $voucher->vo_harga_reseller : $voucher->vo_harga; ?>" />
                </div>
                <div class="form-group">
                  <label>Produk Cek PPOB</label>
                  <select class="form-control" name="ppob_cek">
                    <option value="0">-- Tidak ada --</option>
                    <?php foreach ($vouchers as $vcr) : ?>
                      <option value="<?php echo $vcr->vo_id; ?>" <?php echo ($voucher->vo_ppob_cek == $vcr->vo_id ? ' selected="selected"' : ''); ?>><?php echo html_escape($vcr->vo_nominal . ' (' . $vcr->vo_kode . ')'); ?></option>
                    <?php endforeach; ?>
                  </select>
                </div>
                <div class="form-group">
                  <label>Status <span class="text-danger">*</span></label>
                  <select class="form-control" name="status">
                    <option value="1" <?php echo  set_select('status', '1', $voucher->vo_status ? true : false); ?>>Tersedia</option>
                    <option value="0" <?php echo  set_select('status', '0', !$voucher->vo_status ? true : false); ?>>Tidak tersedia</option>
                  </select>
                </div>
                <div class="form-group">
                  <label>Pengiriman</label>
                  <select class="form-control" name="shipping">
                    <option value="0" <?php echo set_select('shipping', '0', $voucher->vo_shipping == 0 ? true : false); ?>>Tidak membutuhkan pengiriman</option>
                    <option value="1" <?php echo  set_select('shipping', '1', $voucher->vo_shipping != 0 ? true : false); ?>>Membutuhkan pengiriman</option>
                  </select>
                </div>
                <div id="shipping-form" class="well well-sm" style="<?php echo $voucher->vo_shipping == 0 ? "display: none;" : ""; ?>">
                  <div class="form-group">
                    <label>Alamat Pengiriman</label>
                    <div class="input-group">
                      <select class="form-control" name="shipping_address"></select>
                      <span class="input-group-btn"><a href="<?php echo site_url('admin/alamat-pengiriman/tambah'); ?>" class="btn btn-default" data-toggle="modal" data-target="#myModal"><i class="fa fa-plus"></i></a></span>
                    </div>
                  </div>
                  <div class="form-group">
                    <label>Berat Barang</label>
                    <div class="input-group">
                      <input type="number" class="form-control" name="weight" value="<?php echo set_value('weight', $voucher->vo_weight); ?>" />
                      <span class="input-group-addon">gram</span>
                    </div>
                  </div>
                  <div class="form-group">
                    <label>Kuantitas Pembelian</label>
                    <div class="row">
                      <div class="col-xs-6">
                        <div class="input-group">
                          <span class="input-group-addon">Min.</span>
                          <input type="number" class="form-control" name="min_qty" value="<?php echo set_value('min_qty', $voucher->vo_min_qty); ?>" />
                        </div>
                      </div>
                      <div class="col-xs-6">
                        <div class="input-group">
                          <span class="input-group-addon">Max.</span>
                          <input type="number" class="form-control" name="max_qty" value="<?php echo set_value('max_qty', $voucher->vo_max_qty); ?>" />
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="form-group" style="margin-bottom: 0;">
                    <label>Jasa Pengiriman</label>
                    <?php foreach ($couriers as $courier_id => $courier_name) : ?>
                      <div class="checkbox" <?php echo $courier_id == 'jne' ? 'style="margin-top: 0;"' : ''; ?>><label><input type="checkbox" name="couriers[]" value="<?php echo $courier_id; ?>" <?php echo in_array($courier_id, $voucher_couriers) ? 'checked="checked"' : ''; ?> /> <?php echo $courier_name; ?></label></div>
                    <?php endforeach; ?>
                  </div>
                </div>
              </div>
              <div class="col-sm-6 col-lg-8">
                <div class="form-group">
                  <label>Voucher Fisik</label>
                  <textarea class="form-control" rows="8" name="kode_fisik" placeholder="<?php echo ($voucher->vo_kode_fisik ? implode(', ', json_decode($this->system->decrypt_data($voucher->vo_kode_fisik), true)) : ''); ?>"><?php echo set_value('kode_fisik', ($voucher->vo_kode_fisik ? implode(', ', json_decode($this->system->decrypt_data($voucher->vo_kode_fisik), true)) : '')); ?></textarea>
                  <p class="help-block" style="margin-bottom: 0;">(Opsional). Masukan kode voucher fisik, pisahkan dengan tanda koma (,).</p>
                </div>
                <div class="form-group" style="margin-bottom: 0;">
                  <label>Gambar</label>
                  <div class="row">
                    <?php $images = $voucher->vo_image ? json_decode($voucher->vo_image, true) : array(); ?>
                    <div class="col-sm-4" id="image1" style="margin-bottom: 15px;">
                      <p class="text-center"><img class="img-thumbnail" src="<?php echo (isset($images[0]) ? base_url('images/produk/' . $voucher->op_produk . '/thumb-' . $images[0]) : base_url('images/produk/thumb-default.png')); ?>" /></p>
                      <div class="progress" style="margin-bottom: 10px;display: none;">
                        <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div>
                      </div>
                      <div class="btn-group btn-group-justified">
                        <div class="btn-group">
                          <label class="btn btn-default btn-sm btn-file"><i class="fa fa-upload"></i> <span class="hidden-xs hidden-sm">Upload</span><input type="file" name="image1" accept="image/*" style="display: none;" /></label>
                        </div>
                        <div class="btn-group">
                          <button type="button" class="btn btn-default btn-sm" data-toggle="delete" data-image="image1" data-id="<?php echo isset($images[0]) ? $images[0] : '1'; ?>"><i class="fa fa-trash"></i> <span class="hidden-xs hidden-sm">Hapus</span></button>
                        </div>
                      </div>
                    </div>
                    <div class="col-sm-4" id="image2" style="margin-bottom: 15px;">
                      <p class="text-center"><img class="img-thumbnail" src="<?php echo (isset($images[1]) ? base_url('images/produk/' . $voucher->op_produk . '/thumb-' . $images[1]) : base_url('images/produk/thumb-default.png')); ?>" /></p>
                      <div class="progress" style="margin-bottom: 10px;display: none;">
                        <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div>
                      </div>
                      <div class="btn-group btn-group-justified">
                        <div class="btn-group">
                          <label class="btn btn-default btn-sm btn-file"><i class="fa fa-upload"></i> <span class="hidden-xs hidden-sm">Upload</span><input type="file" name="image2" accept="image/*" style="display: none;" /></label>
                        </div>
                        <div class="btn-group">
                          <button type="button" class="btn btn-default btn-sm" data-toggle="delete" data-image="image2" data-id="<?php echo isset($images[1]) ? $images[1] : '2'; ?>"><i class="fa fa-trash"></i> <span class="hidden-xs hidden-sm">Hapus</span></button>
                        </div>
                      </div>
                    </div>
                    <div class="col-sm-4" id="image3" style="margin-bottom: 15px;">
                      <p class="text-center"><img class="img-thumbnail" src="<?php echo (isset($images[2]) ? base_url('images/produk/' . $voucher->op_produk . '/thumb-' . $images[2]) : base_url('images/produk/thumb-default.png')); ?>" /></p>
                      <div class="progress" style="margin-bottom: 10px;display: none;">
                        <div class="progress-bar progress-bar-success" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%;">0%</div>
                      </div>
                      <div class="btn-group btn-group-justified">
                        <div class="btn-group">
                          <label class="btn btn-default btn-sm btn-file"><i class="fa fa-upload"></i> <span class="hidden-xs hidden-sm">Upload</span><input type="file" name="image3" accept="image/*" style="display: none;" /></label>
                        </div>
                        <div class="btn-group">
                          <button type="button" class="btn btn-default btn-sm" data-toggle="delete" data-image="image3" data-id="<?php echo isset($images[2]) ? $images[2] : '3'; ?>"><i class="fa fa-trash"></i> <span class="hidden-xs hidden-sm">Hapus</span></button>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label>Deskripsi</label>
                  <div id="summernote"><?php echo html_entity_decode(set_value('deskripsi', $voucher->vo_description)); ?></div>
                  <textarea style="display: none;" class="form-control" rows="12" name="deskripsi" id="deskripsi"><?php echo set_value('deskripsi', $voucher->vo_description); ?></textarea>
                </div>
              </div>
              <div class="col-sm-offset-6 col-sm-6 col-lg-offset-4 col-lg-8">
                <div class="row">
                  <div class="col-sm-offset-4 col-sm-4 col-lg-offset-6 col-lg-3" style="margin-bottom: 15px;">
                    <button type="submit" class="btn btn-primary btn-block" id="submit" name="save" value="advanced"><i class="fa fa-check"></i> Simpan</button>
                  </div>
                  <div class="col-sm-4 col-lg-3" style="margin-bottom: 15px;">
                    <a href="<?php echo site_url('admin/produk/browse/' . $voucher->op_produk . '/' . $voucher->op_slug . '/' . $this->uri->segment(5)); ?>" class="btn btn-default btn-block" data-dismiss="modal"><i class="fa fa-remove"></i> Batal</a>
                  </div>
                </div>
              </div>
            </div>
            <?php echo form_close(); ?>
          </div>
        </div>
      </div>
    </div>
  </div>
  <?php include_once(VIEWPATH . 'includes/foot.php'); ?>
  <script src="<?php echo base_url('assets/js/jquery.ui.widget.js'); ?>"></script>
  <script src="<?php echo base_url('assets/js/jquery.iframe-transport.js'); ?>"></script>
  <script src="<?php echo base_url('assets/js/jquery.fileupload.js'); ?>"></script>
  <script src="<?php echo base_url('assets/js/summernote.min.js'); ?>"></script>
  <script type="text/javascript">
    function getShippingAddress(id) {
      $.get("<?php echo site_url('admin/alamat-pengiriman/get'); ?>", function(data) {
        var s = $('[name="shipping_address"]');
        s.empty();
        var options = '<option value="">-- Pilih --</option>';
        var i;
        for (i = 0; i < data.length; i++) {
          options += '<option value="' + data[i]['id'] + '"' + (id == data[i]['id'] ? ' selected="selected"' : '') + '>' + data[i]['name'] + '</option>';
        }
        s.html(options);
      });
    }
    getShippingAddress(<?php echo $voucher->vo_shipping; ?>);
    $(document).on("change", '[name="shipping"]', function() {
      if ($('[name="shipping"] option:selected').val() == '1') {
        $('[name="shipping_from"]').attr("required", "require");
        $("#shipping-form").show();
      } else {
        $('[name="shipping_from"]').removeAttr("required");
        $("#shipping-form").hide();
      }
    });
    $(document).on("submit", "#addressForm", function() {
      var buttonClose = $(this).find('[data-dismiss="modal"]');
      var button = $(this).find('button[type="submit"]');
      button.attr("disabled", "disabled");
      button.html('<i class="fa fa-spinner fa-pulse"></i> Memproses...');
      $.post($(this).attr("action") + "?ajax=1", $(this).serialize()).done(function(data) {
        $('[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(data.token);
        if (data.success) {
          getShippingAddress(data.results.id);
          buttonClose.click();
        } else {
          $.notify(data.message, "error");
        }
      }).fail(function() {
        $.notify("Terjadi kesalahan", "error");
      }).always(function() {
        button.removeAttr("disabled");
        button.html('<i class="fa fa-check"></i> Simpan');
      });
      return false;
    });

    function updateKodeTrx(val) {
      document.getElementById("kode_trx").value = val;
    }
    var umum = document.getElementById("harga");
    var reseller = document.getElementById("harga_reseller");
    var selisih = umum.getAttribute("data-harga") - reseller.getAttribute("data-harga");

    function updateHargaReseller(val) {
      reseller.value = val - selisih;
    }
    $(function() {
      'use strict';
      var url = "<?php echo site_url('admin/produk/upload-gambar-voucher/' . $voucher->vo_id); ?>";
      $('input[type="file"]').fileupload({
        url: url,
        dataType: 'json',
        cache: false,
        processData: false,
        done: function(e, data) {
          if (data.result.success != false) {
            var image_id = $(this).attr('name');
            $("#" + image_id + " img").attr('src', data.result.file_url);
            $('button[data-image="' + image_id + '"]').attr('data-id', data.result.file_id);
          } else {
            alert(data.result.message);
          }
          $('input[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(data.result.token);
        },
        progressall: function(e, data) {
          $("#" + $(this).attr('name') + " .progress").show();
          var percent = parseInt(data.loaded / data.total * 100, 10);
          $('#' + $(this).attr('name') + ' .progress-bar').attr('valuenow', percent);
          $('#' + $(this).attr('name') + ' .progress-bar').css("width", +percent + "%");
          $('#' + $(this).attr('name') + ' .progress-bar').text(percent + "%");
        }
      });
      $(document).on('click', '[data-toggle="delete"]', function() {
        var id = $(this).attr('data-id');
        var image = $(this).attr('data-image');
        $.get("<?php echo site_url('admin/produk/hapus-gambar-voucher/' . $voucher->vo_id); ?>/" + id, function() {
          $("#" + image + " .progress").hide();
          $("#" + image + " img").attr('src', "<?php echo base_url('images/produk/thumb-default.png'); ?>");
        });
        return false;
      });
      $('#summernote').summernote({
        height: 300,
        minHeight: null,
        maxHeight: null,
        focus: false
      });
      $("#submit").click(function() {
        $("#deskripsi").text($('#summernote').summernote('code'));
        $("#editForm").submit();
      })
    });
  </script>
</body>

</html>