<?php defined('BASEPATH') or exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html>

<head>
  <title>Produk &gt; <?php echo html_escape($this->system->produk->{$provider->op_produk}->nama) . ' &gt; ' . html_escape($provider->op_nama); ?></title>
  <?php include_once(VIEWPATH . 'includes/head.php'); ?>
  <style type="text/css">
    .popover-content>.list-group {
      margin: -9px -15px;
      border-radius: 0;
    }

    .popover-content>.list-group>.list-group-item {
      padding: 5px 8px;
      border-left: 0;
      border-right: 0;
    }

    .popover-content>.list-group>.list-group-item:first-child {
      border-top: 0;
    }

    .popover-content>.list-group>.list-group-item:last-child {
      border-bottom: 0;
    }

    nav {
      float: right;
      margin-bottom: 15px;
    }
  </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/' . $provider->op_produk); ?>"><?php echo html_escape($this->system->produk->{$provider->op_produk}->nama); ?></a></li>
      <li class="active"><span><?php echo html_escape($provider->op_nama); ?></span></li>
    </ul>
    <div class="row">
      <div class="col-sm-4 col-md-3" id="left-content">
        <?php
        $params = array(array(
          'title' => html_escape($this->system->produk->{$provider->op_produk}->nama),
          'links' => array()
        ));
        foreach ($providers as $prov) {
          $params[0]['links'][] = array($prov->op_nama, site_url('admin/produk/browse/' . $provider->op_produk . '/' . $prov->op_slug), null, $prov->op_slug == $this->uri->segment(5) ? true : false);
        }
        echo sidebar_menu($params);
        ?>
      </div>
      <div class="col-sm-8 col-md-9" id="right-content">
        <div class="row">
          <div class="col-sm-6 col-md-4 col-lg-3">
            <a class="btn btn-default btn-block" style="margin-bottom: 15px;" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/produk/tambah-voucher/' . $provider->op_id); ?>"><i class="fa fa-plus"></i> Tambah <?php echo html_escape($this->system->produk->{$provider->op_produk}->form->voucher->label); ?></a>
          </div>
          <div class="col-sm-6 col-md-offset-4 col-md-4 col-lg-offset-6 col-lg-3">
            <a class="btn btn-danger btn-block" style="margin-bottom: 15px;" data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/produk/kosongkan-voucher/' . $provider->op_id); ?>"><i class="fa fa-trash"></i> Hapus semua</a>
          </div>
        </div>
        <?php if (!$vouchers) : ?>
          <div class="alert alert-warning">Tidak ada <?php echo html_escape($this->system->produk->{$provider->op_produk}->form->voucher->label); ?></div>
        <?php else : ?>
          <?php echo form_open(site_url('admin/produk/edit-status-voucher/' . $provider->op_id), 'id="form-confirm"'); ?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3><?php echo $this->system->produk->{$provider->op_produk}->mini_icon; ?> <?php echo html_escape($provider->op_nama); ?></h3>
            </div>
            <div class="table-responsive">
              <table class="table table-bordered table-hover">
                <thead>
                  <tr>
                    <th class="text-center" style="width: 40px;"><input type="checkbox" id="select-all" /></th>
                    <th style="width: 100px;">Server</th>
                    <th colspan="2" class="text-center">Kode</th>
                    <th><?php echo html_escape($this->system->produk->{$provider->op_produk}->form->voucher->label); ?></th>
                    <th colspan="3" class="text-center">Harga</th>
                    <th style="width: 45px;">Stok</th>
                    <th style="text-align: center; width: 54px;">Status</th>
                    <th style="width: 39px; text-align: center;"><i class="fa fa-ellipsis-h"></i></th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($vouchers as $voucher) : ?>
                    <tr id="voucher-<?php echo $voucher->vo_id; ?>" class="<?php echo $voucher->vo_status == 0 ? 'text-danger' : ''; ?>">
                      <td class="text-center"><input type="checkbox" class="checkbox1" name="vouchers[]" value="<?php echo $voucher->vo_id; ?>" /></td>
                      <td><span style="white-space: nowrap;"><?php echo html_escape(@$voucher->sv_nama); ?></span></td>
                      <td style="width: 55px"><span data-toggle="tooltip" data-title="Kode Server"><?php echo html_escape($voucher->vo_kode); ?></span></td>
                      <td style="width: 55px"><span data-toggle="tooltip" data-title="Kode Trx"><?php echo html_escape($voucher->vo_kode_trx); ?></span></td>
                      <td><a href="<?php echo site_url('/produk/' . $voucher->op_produk . '/' . $provider->op_slug . '/' . str_link($voucher->vo_nominal) . '-' . $voucher->vo_id); ?>" target="produk_page"><?php echo ($voucher->vo_shipping != '0' ? '<i class="fa fa-truck"></i> ' : '') . ($voucher->vo_image != '' && $voucher->vo_image != '[]' ? '<i class="fa fa-image"></i> ' : '') . ($voucher->vo_description ? '<i class="fa fa-sticky-note-o"></i> ' : '') . ($voucher->vo_ppob_cek ? '<i class="fa fa-credit-card"></i> ' : '') . html_escape($voucher->vo_nominal); ?></a></td>
                      <td style="width: 55px;"><span data-toggle="tooltip" data-title="Harga beli"><?php echo $voucher->vo_harga_beli ? format_uang($voucher->vo_harga_beli) : ''; ?></span></td>
                      <td style="width: 55px;"><span data-toggle="tooltip" data-title="Harga jual umum"><?php echo format_uang($voucher->vo_harga); ?></span></td>
                      <td style="width: 55px;"><span data-toggle="tooltip" data-title="Harga jual reseller"><?php echo ($voucher->vo_harga_reseller ? format_uang($voucher->vo_harga_reseller) : format_uang($voucher->vo_harga)); ?></span></td>
                      <td style="width: 45px;"><?php echo ($voucher->vo_kode_fisik ? count(json_decode($this->system->decrypt_data($voucher->vo_kode_fisik), true)) : ''); ?></td>
                      <td style="text-align: center; width: 54px;"><?php echo ($voucher->vo_status ? '<span class="text-success" data-toggle="tooltip" data-title="Tersedia"><i class="fa fa-check"></i></span>' : '<span class="text-danger" data-toggle="tooltip" data-title="Tidak tersedia"><i class="fa fa-times"></i></span>'); ?></td>
                      <td>
                        <button type="button" class="btn btn-default btn-xs" data-container="body" data-toggle="popover" data-placement="left" data-html="true" data-content="<?php echo html_escape('<div class="list-group"><a data-toggle="modal" data-target="#myModal" class="list-group-item" href="' .
                                                                                                                                                                                site_url('admin/produk/edit-voucher/' . $voucher->vo_id . '/' . $page) .
                                                                                                                                                                                '"><i class="fa fa-pencil"></i> Edit cepat</a><a class="list-group-item" href="' .
                                                                                                                                                                                site_url('admin/produk/edit-voucher/' . $voucher->vo_id . '/' . $page) .
                                                                                                                                                                                '"><i class="fa fa-edit"></i> Edit lebih lanjut</a><a data-toggle="modal" data-target="#myModal" class="list-group-item" href="' .
                                                                                                                                                                                site_url('admin/produk/hapus-voucher/' . $voucher->op_id . '/' . $voucher->vo_id) .
                                                                                                                                                                                '"><span class="text-danger"><i class="fa fa-remove"></i> Hapus</span></a></div>'); ?>" data-trigger="focus">
                          <i class="fa fa-edit"></i>
                        </button>
                      </td>
                    </tr>
                  <?php endforeach; ?>
                </tbody>
              </table>
            </div>
          </div>
          <div class="row" style="margin-bottom: 5px;">
            <div class="col-sm-4">
              <div class="input-group" style="margin-bottom: 15px;">
                <select class="form-control input-sm" name="action">
                  <option value="">-- Yang ditandai --</option>
                  <option value="edit">Edit</option>
                  <option value="status_tersedia">Status Tersedia</option>
                  <option value="status_tidak_tersedia">Status Tidak tersedia</option>
                  <option value="delete">Hapus</option>
                </select>
                <span class="input-group-btn">
                  <button class="btn btn-primary btn-sm" type="submit" name="action_submit" value="Go">GO</button>
                </span>
              </div>
            </div>
            <div class="col-sm-8">
              <?php echo pagination(site_url('admin/produk/browse/' . $provider->op_produk . '/' . $provider->op_slug), $start, $total, $this->system->set['list_per_page'], '/%d'); ?>
            </div>
          </div>
          <?php echo form_close(); ?>
        <?php endif; ?>
      </div>
    </div>
  </div>
  <?php include_once(VIEWPATH . 'includes/foot.php'); ?>
  <script>
    /**if ($(window).width() > 767) {
      $("#right-content .panel").hover(function() {
        $("#left-content").hide();
        $("#right-content").attr("class", "col-sm-12 col-md-12");
      }, function() {
        $("#left-content").show();
        $("#right-content").attr("class", "col-sm-8 col-md-9");
      });
    }**/
    $(document).ready(function() {
      $('#select-all').click(function(event) {
        if (this.checked) {
          $('.checkbox1').each(function() {
            this.checked = true;
          });
        } else {
          $('.checkbox1').each(function() {
            this.checked = false;
          });
        }
      });
    });
    $(document.body).on("show.bs.modal", function() {
      $("#myModal .modal-dialog").removeClass('modal-lg');
    });
    $(document).on("submit", "#form-confirm", function() {
      var formData = $(this).serialize();
      if (formData.indexOf('vouchers') > 0) {
        var selected = $('select[name="action"] option:selected').val();
        if (selected == "delete") {
          $("#myModal").modal("show");
          $.ajax({
            type: "POST",
            url: "<?php echo site_url('admin/produk/hapus-voucher/' . $provider->op_id); ?>",
            data: formData,
            success: function(data) {
              var csrf = $(data).find('[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val();
              $('#form-confirm [name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(csrf);
              $("#myModal .modal-content").html(data);
            },
            error: function() {
              alert("Terjadi kesalahan!");
            }
          });
          return false;
        } else if (selected == "edit") {
          $("#myModal").modal("show");
          $.ajax({
            type: "POST",
            url: "<?php echo site_url('admin/produk/edit-voucher-massal/' . $provider->op_id); ?>",
            data: formData,
            success: function(data) {
              var csrf = $(data).find('[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val();
              $('#form-confirm [name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(csrf);
              $("#myModal .modal-dialog").addClass('modal-lg');
              $("#myModal .modal-content").html(data);
            },
            error: function() {
              alert("Terjadi kesalahan!");
            }
          });
          return false;
        }
      } else {
        return false;
      }
    });
  </script>
</body>

</html>