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

<head>
  <title>Rincian Transaksi #<?php echo $trx->tr_id; ?></title>
  <?php include_once(VIEWPATH . 'includes/head.php'); ?>
</head>

<body>
  <?php include_once(VIEWPATH . 'includes/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('history'); ?>">Riwayat Transaksi</a></li>
      <li class="active"><span>Transaksi #<?php echo $trx->tr_id; ?></span></li>
    </ul>
    <div class="row">
      <div class="col-sm-8">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3><i class="fa fa-list-alt"></i> Transaksi #<?php echo $trx->tr_id; ?></h3>
          </div>
          <?php
          if (file_exists(VIEWPATH . 'history/' . $trx->op_produk . '.php'))
            include(VIEWPATH . 'history/' . $trx->op_produk . '.php');
          else
            include(VIEWPATH . 'history/undefined.php');
          ?>
          <div class="panel-footer hidden-print">
            <?php /** TODO: v3.9.4 */ if (!$trx->us_id && property_exists($trx, "ppob_amount")) : ?>
              <button type="button" id="payButton" data-token-name="<?php echo $this->security->get_csrf_token_name(); ?>" data-token-hash="<?php echo $this->security->get_csrf_hash(); ?>" class="btn btn-primary pull-right" style="margin-bottom: 15px;"><i class="fa fa-credit-card"></i> Bayar <?php echo format_uang2($trx->ppob_amount); ?></button>
            <?php else : ?>
              <button type="button" id="komplain" class="btn btn-warning pull-right" style="margin-bottom: 15px;"><i class="fa fa-exclamation-circle"></i> Komplain</button>
            <?php endif; ?>
            <a class="btn btn-default" href="#" onclick="window.print();return false;"><i class="fa fa-print"></i> Print</a>
          </div>
        </div>
      </div>
      <div class="col-sm-4 hidden-print">
        <?php if ($trx->tr_pembayaran == 'balance') : ?>
          <?php if ($trx->tr_status_pembayaran == 'refund') : ?>
            <div class="alert alert-success">
              <h3>Refund</h3>Pembayaran direfund ke Saldo Akun member.
            </div>
          <?php else : ?>
            <div class="alert alert-success">
              <h3>Sukses</h3>Pembayaran sudah diselesaikan dengan menggunakan saldo akun.
            </div>
          <?php endif; ?>
        <?php else : ?>
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3><i class="fa fa-money"></i> Bayar dengan <?php echo $this->payment->{$trx->tr_pembayaran}->nama; ?></h3>
            </div>
            <?php if (file_exists(FCPATH . 'assets/payments/' . $trx->tr_pembayaran . '.png')) : ?>
              <div class="panel-body" style="margin-bottom: 0;padding-bottom: 0;">
                <div class="text-center"><img src="<?php echo base_url('assets/payments/' . $trx->tr_pembayaran . '.png'); ?>" class="img-thumbnail" style="margin: 0 auto;" /></div>
              </div>
            <?php endif; ?>
            <?php if ($this->payment->{$trx->tr_pembayaran}->status != 'on') : ?>
              <div class="panel-body">
                <div class="alert alert-danger">Pembayaran <?php echo $this->payment->{$trx->tr_pembayaran}->nama; ?> saat ini sedang tidak aktif.</div>
              </div>
            <?php elseif ($trx->tr_status_pembayaran == 'pending' && $trx->tr_tanggal < $exp_time) : ?>
              <div class="panel-body">
                <div class="alert alert-danger">Pembayaran belum diselesaikan dan pesanan sudah tidak berlaku lagi.</div>
              </div>
            <?php elseif ($trx->tr_status_pembayaran == 'refund') : ?>
              <div class="panel-body">
                <div class="alert alert-info">Pembayaran direfund.</div>
              </div>
            <?php elseif ($trx->tr_status_pembayaran == 'sukses') : ?>
              <div class="panel-body">
                <div class="alert alert-success">Pembayaran berhasil diselesaikan.</div>
              </div>
            <?php elseif ($this->payment->{$trx->tr_pembayaran}->user != 0 && !$this->user->is_user()) : ?>
              <div class="panel-body">
                <div class="alert alert-warning">Untuk melihat detail pembayaran silakan <a class="alert-link" href="<?php echo site_url('user/login'); ?>">Masuk</a> terlebih dahulu.</div>
              </div>
            <?php elseif ($this->payment->{$trx->tr_pembayaran}->user == 2 && !$this->user->verified) : ?>
              <div class="panel-body">
                <div class="alert alert-warning">Untuk melihat detail pembayaran silakan <a class="alert-link" href="<?php echo site_url('akun/verifikasi'); ?>">Verifikasi</a> akun anda terlebih dahulu.</div>
              </div>
            <?php else : ?>
              <?php include_once(VIEWPATH . 'history/' . $trx->tr_pembayaran . '.php'); ?>
            <?php endif; ?>
          </div>
        <?php endif; ?>
        <?php
        if ($trx->tr_status_pembayaran == 'sukses' && $trx->tr_status == 'sukses') :
          $testimonial = $this->trx_model->get_testimonial($trx->tr_id);
          if (!$testimonial && $this->user->is_user() && $trx->us_id == $this->user->us_id) :
        ?>
            <div class="panel panel-default" id="testimonial">
              <div class="panel-heading">
                <h3><i class="fa fa-comments-o"></i> Testimonial</h3>
              </div>
              <div class="panel-body">
                <p>Kamu belum mengirimkan testimonial untuk transaksi ini.</p>
                <?php echo form_open(site_url('akun/riwayat-transaksi/view/' . $trx->tr_id) . '#testimonial'); ?>
                <div class="form-group">
                  <label>Pesan</label>
                  <textarea class="form-control" name="testimonial" rows="4" maxlength="160"></textarea>
                </div>
                <div>
                  <button type="submit" name="submit" value="testimonial" class="btn btn-primary btn-block">Kirim Testimonial</button>
                </div>
                <?php echo form_close(); ?>
              </div>
            </div>
          <?php
          elseif ($testimonial && ($this->user->is_admin() || !$testimonial->moderasi || ($this->user->is_user() && $trx->us_id == $this->user->us_id))) : ?>
            <div class="panel panel-default" id="testimonial">
              <div class="panel-heading">
                <h3><i class="fa fa-comments-o"></i> Testimonial</h3>
              </div>
              <div class="panel-body">
                <div>
                  <span class="text-muted pull-right"><small><i class="fa fa-clock-o"></i> <?php echo format_tanggal($testimonial->tanggal); ?></small></span>
                  <?php echo ($this->user->is_admin() ? '<a href="' . site_url('admin/users/profil/' . $testimonial->us_id) . '"><strong><i class="fa fa-user"></i> ' . html_escape($testimonial->nama) . '</a></strong>' : '<strong><i class="fa fa-user"></i> ' . html_escape($testimonial->nama) . '</strong>'); ?>&nbsp;
                </div>
                <div>
                  <p style="margin-bottom: 0;"><?php echo nl2br(html_escape($testimonial->pesan)) . (!$testimonial->moderasi && $this->user->is_admin() ? '&nbsp;<a href="' . site_url('admin/testimonial/delete/' . $testimonial->id) . '" data-toggle="modal" data-target="#myModal">[Hapus]</a>' : ''); ?></p>
                  <?php if ($testimonial->moderasi && $this->user->is_admin()) : ?>
                    <div class="row" style="margin-top: 10px;">
                      <div class="col-xs-6">
                        <a class="btn btn-primary btn-sm btn-block" href="<?php echo site_url('admin/testimonial/confirm/' . $testimonial->id); ?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-check"></i> Konfirmasi</a>
                      </div>
                      <div class="col-xs-6">
                        <a class="btn btn-danger btn-sm btn-block" href="<?php echo site_url('admin/testimonial/delete/' . $testimonial->id); ?>" data-toggle="modal" data-target="#myModal"><i class="fa fa-trash"></i> Hapus</a>
                      </div>
                    </div>
                  <?php endif; ?>
                </div>
              </div>
            </div>
          <?php endif; ?>
        <?php endif; ?>
      </div>
    </div>
  </div>
  <?php include_once(VIEWPATH . 'includes/foot.php'); ?>
  <?php
  $content =  '<ul class="list-unstyled">
        <li><a href="' . site_url('contact-us?trx_id=' . $trx->tr_id . '&amp;pesan=' . urlencode('Sudah membayar tapi status pembayaran masih pending')) . '">Sudah membayar tapi status pembayaran masih pending</a></li>
        <li><a href="' . site_url('contact-us?trx_id=' . $trx->tr_id . '&amp;pesan=' . urlencode('Sudah membayar tapi status pengisian masih pending')) . '">Sudah membayar tapi status pengisian masih pending</a></li>
        <li><a href="' . site_url('contact-us?trx_id=' . $trx->tr_id . '&amp;pesan=' . urlencode('Keterangan status pengisian sukses tapi pulsa belum masuk')) . '">Keterangan status pengisian sukses tapi pulsa belum masuk</a></li>
        <li><a href="' . site_url('contact-us?trx_id=' . $trx->tr_id . '&amp;pesan=' . urlencode('Data transfer salah')) . '">Data transfer salah</a></li></li>
        <li><a href="' . site_url('contact-us?trx_id=' . $trx->tr_id) . '">Masalah lainnya</a></li></li>
        </ul>';
  ?>
  <script>
    $(function() {
      $('#komplain').popover(<?php echo json_encode(array('title' => 'Pilih topik permasalahan', 'content' => $content, 'html' => true, 'placement' => 'top')); ?>);
      <?php /** TODO: v3.9.4 */ if (!$trx->us_id && property_exists($trx, "ppob_amount")) : ?>
        <?php
        $reseller_config = json_decode($this->system->get_set('reseller'));
        $payment_active = '';
        $payment_inactive = '';
        foreach ($this->payment->get() as $payment) {
          if ($payment->status == 'off')
            continue;
          elseif ($this->user->is_reseller && $reseller_config->payment == 'balance' && $payment->key != 'balance')
            $payment_inactive .= '<option value="' . $payment->key . '" disabled="disabled">' . $payment->nama . ' (Hanya member)</option>';
          elseif ($payment->user == 1 && !$this->user->is_user())
            $payment_inactive .= '<option value="' . $payment->key . '" disabled="disabled">' . $payment->nama . ' (Hanya member)</option>';
          elseif ($payment->user == 2 && !$this->user->verified)
            $payment_inactive .= '<option value="' . $payment->key . '" disabled="disabled">' . $payment->nama . ' (Hanya member terverifikasi)</option>';
          elseif ($this->system->produk->{$trx->ppob_pay->op_produk}->payments && !in_array($payment->key, $this->system->produk->{$trx->ppob_pay->op_produk}->payments))
            $payment_inactive .= '<option value="' . $payment->key . '" disabled="disabled">' . $payment->nama . ' (Tidak diijinkan)</option>';
          elseif ($this->payment->is_offline($payment->key))
            $payment_inactive .= '<option value="' . $payment->key . '" disabled="disabled">' . $payment->nama . ' (Offline)</option>';
          else
            $payment_active .= '<option value="' . $payment->key . '">' . $payment->nama . '</option>';
        }
        ?>
        var captchaField = '<div class="form-group" id="captcha" style="margin-top:15px;display:<?php echo $this->system->perm->captcha_order == 'yes' ? 'block' : 'none';?>"><label>Kode Keamanan</label><div class="input-group"><span class="input-group-addon" style="padding: 0;"><img id="captcha-image" src="<?php echo site_url('captcha?r=' . time()); ?>" style="height:30px"/></span><input type="text" name="captcha" class="form-control" <?php echo $this->system->perm->captcha_order == 'yes' ? 'required="required"' : '';?> placeholder="Kode keamanan" autocomplete="off"/></div></div>';
        $("#payButton").click(function() {
          $("body").append('<div class="modal fade" id="payModal" tabindex="-1" role="dialog" aria-labelledby="payModal-label" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"></div></div></div>');
          var mContent = '<form id="payForm" method="post" action="<?php echo site_url($trx->ppob_pay->op_produk); ?>"><input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">' +
            '<input type="hidden" name="nomor_hp" value="<?php echo html_escape($trx->tr_no_hp); ?>">' +
            '<input type="hidden" name="id_plgn" value="<?php echo html_escape($trx->tr_id_plgn); ?>">' +
            '<input type="hidden" name="operator" value="<?php echo $trx->ppob_pay->op_id; ?>">' +
            '<input type="hidden" name="voucher" value="<?php echo $trx->ppob_pay->vo_id; ?>">' +
            '<input type="hidden" name="json_format" value="1">' +
            '<div class="modal-header"><button type="button" class="close" data-dismiss="modal" ' +
            'aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title">Informasi' +
            ' Tagihan</h4></div><div class="modal-body"><p style="margin-bottom: 15px;">Pembayaran tagihan <?php echo html_escape($trx->op_nama); ?> sudah tersedia, jika kamu belum melakukan pembayaran silakan klik tombol <span class="label label-primary">Bayar Sekarang</span> untuk melakukan pembayaran.</p><dl><dt>Rincian Tagihan</dt><dd style="margin-bottom:5px"><i>-- disembunyikan --</i></dd><dt>Jumlah Tagihan</dt><dd><?php echo format_uang2($trx->ppob_amount); ?></dd></dl>' +
            '<div class="form-group" style="margin-bottom:0"><label>Pembayaran</label><select class="form-control" name="pembayaran"><?php echo $payment_active . $payment_inactive; ?></select></div>' + captchaField +
            '</div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-times"></i> Tutup</button><button class="btn btn-primary" id="paySubmitBtn" type="submit" style="margin-left: 15px;"><i class="fa fa-credit-card"></i> Bayar Sekarang</button></div></form>';
          $("#payModal").modal({
            backdrop: 'static',
            keyboard: false
          });
          setTimeout(function() {
            $("#payModal .modal-content").html(mContent);
          }, 1000);
          return false;
        });
        $(document).on("submit", "#payForm", function() {
          $("#paySubmitBtn").button('loading');
          var url = $(this).attr('action');
          $.post(url, $(this).serialize(), function(data) {
              if (data.success) {
                window.location.href = "<?php echo site_url('history/view'); ?>/" + data.id;
              } else {
                for (i = 0; i < data.errors.length; i++) {
                  $.notify(data.errors[i], "error");
                }
                $.get("<?php echo current_url(); ?>", function(data) {
                  var csrf = $(data).find('[data-token-name="<?php echo $this->security->get_csrf_token_name(); ?>"]').data("token-hash");
                  $('[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(csrf);
                  <?php if ($this->system->perm->captcha_order == 'yes') : ?>
                    var captcha = $(data).find('#captcha-image').attr('src');
                    $("#captcha-image").attr("src", captcha);
                    $("#captcha .form-control").val('');
                  <?php endif; ?>
                })
                $("#paySubmitBtn").button('reset');
              }
            })
            .fail(function() {
              $.get("<?php echo current_url(); ?>", function(data) {
                var csrf = $(data).find('[data-token-name="<?php echo $this->security->get_csrf_token_name(); ?>"]').data("token-hash");
                $('[name="<?php echo $this->security->get_csrf_token_name(); ?>"]').val(csrf);
                <?php if ($this->system->perm->captcha_order == 'yes') : ?>
                  var captcha = $(data).find('#captcha-image').attr('src');
                  $("#captcha-image").attr("src", captcha);
                  $("#captcha .form-control").val('');
                <?php endif; ?>
              })
              $.notify("Terjadi kesalahan saat memproses permintaan, silakan coba lagi atau refresh halaman ini.", "error");
              $("#paySubmitBtn").button('reset');
            })
          return false;
        });
      <?php endif; ?>
    });
  </script>
  <?php if ($trx->tr_status_pembayaran == 'sukses' && ($trx->tr_status == 'pending' || $trx->tr_status == '-' || $trx->tr_status == 'dalam_proses')) : ?>
    <script>
      function refreshHistory() {
        $.get("<?php echo site_url('cron'); ?>", function(data) {
          var terkirim = new RegExp(": <?php echo $trx->tr_id; ?>...");
          if (terkirim.test(data) == true) {
            location.reload();
          }
        });
      }
      setInterval(refreshHistory, 5000);
    </script>
  <?php endif; ?>
</body>

</html>