<?php

defined('BASEPATH') or exit('No direct script access allowed');

$filter = 0;
if ($id)
    $filter += 1;
if ($produk)
    $filter += 1;
if ($provider_id)
    $filter += 1;
if ($voucher_id)
    $filter += 1;
if ($username)
    $filter += 1;
if ($server)
    $filter += 1;
if ($no_hp)
    $filter += 1;
if ($pembayaran)
    $filter += 1;
if ($dari_tanggal)
    $filter += 1;
if ($ke_tanggal)
    $filter += 1;
if ($status_pembayaran)
    $filter += 1;
if ($status_pengisian)
    $filter += 1;

?>
<!DOCTYPE html>
<html>
  <head>
    <title>Admin &gt; Transaksi</title>
    <?php include_once(VIEWPATH. 'includes/head.php'); ?>
    <link id="bsdp-css" href="<?php echo base_url('assets/css/bootstrap-datepicker3.css');?>" rel="stylesheet"/>
    <style type="text/css">
    #action-links>div>div{margin-bottom:15px;}#action-links>div>nav{float:right;margin-bottom:15px;}
    .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;}
    .datepicker{z-index:1151;}
    .modal-content3{position:relative;background-color:#fff;-webkit-background-clip:padding-box;background-clip:padding-box;border:1px solid #999;border:1px solid rgba(0,0,0,.2);border-radius:6px;outline:0;-webkit-box-shadow:0 3px 9px rgba(0,0,0,.5);box-shadow:0 3px 9px rgba(0,0,0,.5)}@media (min-width:768px){.modal-content3{-webkit-box-shadow:0 5px 15px rgba(0,0,0,.5);box-shadow:0 5px 15px rgba(0,0,0,.5)}}
    </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 class="active"><span>Transaksi</span></li>
      </ul>
      <div class="row">
        <div class="col-xs-6 col-sm-3 col-md-2">
          <a class="btn btn-default btn-block" style="margin-bottom: 15px;" href="<?php echo site_url('admin/transaksi/tagihan');?>"><i class="fa fa-money"></i> Tagihan</a>
        </div>
        <div class="col-xs-6 col-sm-3 col-md-2 col-md-offset-4">
          <a class="btn btn-default btn-block" style="margin-bottom: 15px;" href="<?php echo site_url('admin/transaksi/top-users/bulan-ini');?>"><i class="fa fa-user-circle-o"></i> Top Users</a>
        </div>
        <div class="col-xs-6 col-sm-3 col-md-2">
          <a class="btn btn-default btn-block" style="margin-bottom: 15px;" href="<?php echo site_url('admin/rekap-trx');?>"><i class="fa fa-list-alt"></i> Rekap</a>
        </div>
        <div class="col-xs-6 col-sm-3 col-md-2">
          <button type="button" style="margin-bottom: 15px;" class="btn btn-default btn-block" id="form-button"><i class="fa fa-filter"></i> Filter <?php echo ($filter ? ' ('.$filter.')' : '');?></button>
          <button type="button" style="margin-bottom: 15px; display: none;" class="btn btn-default btn-block pull-right" data-toggle="collapse" aria-expanded="false" aria-controls="filter_form" data-target="#filter_form"><i class="fa fa-filter"></i> Filter<?php echo ($filter ? ' ('.$filter.')' : '');?> <span class="caret"></span></button>
        </div>
      </div>
      <?php if (!$total):?>
      <div class="alert alert-info" style="margin-bottom: 15px;">Tidak ada transaksi</div>
      <?php else:?>
      <div class="progress" id="progress" style="display: none;">
        <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" style="width: 0%">0%</div>
      </div>
      <?php echo form_open(current_url(),'id="form-confirm"');?>
      <div class="panel panel-default" id="history">
        <div class="panel-heading">
          <h3><a id="dl-link" href="#" class="pull-right" title="Download <?php echo $total;?> transaksi"><i class="fa fa-download"></i></a><i class="fa fa-list"></i> Transaksi</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>ID</th>
                <th>User</th>
                <th>Server</th>
                <th>Provider</th>
                <th>Nominal</th>
                <th>No. HP / ID PLN</th>
                <th>Harga</th>
                <th>Pembayaran</th>
                <th style="width: 115px;">Tgl. Pembelian</th>
                <th class="text-center" style="width: 55px;">Status</th>
                <th class="text-center" style="width:39px"><i class="fa fa-ellipsis-h"></i></th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($results as $trx): $opsi = $trx->tr_opsi ? json_decode($trx->tr_opsi, true) : array(); ?>
              <tr id="trx-<?php echo $trx->tr_id;?>">
                <td class="text-center"><input type="checkbox" value="<?php echo $trx->tr_id;?>" <?php echo (array_key_exists('shipping', $opsi) ? 'disabled="disabled"' : 'name="trx[]" class="checkbox1"');?>/></td>
                <td><a href="<?php echo site_url('admin/transaksi/view/' . $trx->tr_id);?>"><?php echo $trx->tr_id;?></a></td>
                <td><?php if ($trx->us_username):?><a href="<?php echo site_url('admin/users/profil/' . $trx->us_id);?>"><?php echo ($trx->us_verified ? '<span class="text-success"><i class="fa fa-check-circle"></i></span> ': '') . html_escape($trx->us_name);?></a><?php endif;?></td>
                <td><?php echo html_escape(@$trx->sv_nama);?></td>
                <td><?php echo html_escape($trx->op_nama);?></td>
                <td><?php echo (array_key_exists('shipping', $opsi) ? '<i class="fa fa-truck"></i> '.(!empty($opsi['awb_number']) ? '<i class="fa fa-sticky-note-o"></i> ' : '') : '') . $trx->vo_nominal;?></td>
                <td>
                <?php if ($trx->tr_status_pembayaran == 'sukses'):?>
                <a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim?phone=' . $trx->tr_no_hp.'&amp;message='.urlencode("Terima kasih sudah melakukan pembayaran, lihat transaksi kamu di " . site_url('history/view/'. $trx->tr_id)));?>"><?php echo ($no_hp ? str_replace($no_hp,'<span class="text-danger"><b>'.$no_hp.'</b></span>', $trx->tr_no_hp) : $trx->tr_no_hp);?></a>
                <?php else:?>
                <a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim?phone=' . $trx->tr_no_hp.'&amp;message='.urlencode("Pesanan kamu belum dibayar, silakan kunjungi " . site_url('history/view/'.$trx->tr_id) . "\r\nTerima Kasih"));?>"><?php echo ($no_hp ? str_replace($no_hp,'<span class="text-danger"><b>'.$no_hp.'</b></span>', $trx->tr_no_hp) : $trx->tr_no_hp);?></a>
                <?php endif;?>
                <?php echo ($trx->tr_id_plgn ? '/'.($no_hp ? str_replace($no_hp,'<span class="text-danger"><b>'.$no_hp.'</b></span>', $trx->tr_id_plgn) : $trx->tr_id_plgn) : '');?>
                <?php /* <a data-toggle="modal" data-target="#myModal" href="<?php echo site_url('admin/sms/kirim?phone=' . $trx->tr_no_hp.'&amp;message='.urlencode($trx->tr_status_pembayaran == 'sukses' ? "Terima kasih sudah melakukan pembayaran, lihat transaksi kamu di " . site_url('history/view/'. $trx->tr_id) : "Pesanan kamu belum dibayar, silakan kunjungi " . site_url('history/view/'.$trx->tr_id) . "\r\nTerima Kasih"));?>"><?php echo ($no_hp ? str_replace($no_hp,'<span class="text-danger"><b>'.$no_hp.'</b></span>', $trx->tr_no_hp) : $trx->tr_no_hp);?></a><?php echo ($trx->tr_id_plgn ? ' / ' . $trx->tr_id_plgn : '');?> */?>
                </td>
                <td><?php echo ($trx->tr_income ? '<span style="font-weight:bold;white-space:nowrap" data-toggle="tooltip" data-title="Harga Beli: '.format_uang(($trx->us_id ? $trx->tr_harga2 : $trx->tr_harga) - $trx->tr_income).', Laba: '.format_uang($trx->tr_income).'">' : '<span style="white-space:nowrap">').format_uang2($trx->tr_harga, $trx->tr_rate, $this->payment->{$trx->tr_pembayaran}->template, $this->payment->{$trx->tr_pembayaran}->round);?></span></td>
                <td><?php echo $this->payment->{$trx->tr_pembayaran}->nama;?></td>
                <td><?php echo format_tanggal($trx->tr_tanggal);?></td>
                <td class="text-center"><?php echo status_label($trx, $exp_time, false);?></td>
                <td class="text-center">
                  <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/transaksi/edit/' . $trx->tr_id) .
                    '"><i class="fa fa-pencil"></i> Edit</a><a data-toggle="modal" data-target="#myModal" class="list-group-item" href="' .
                    site_url('admin/transaksi/hapus/' . $trx->tr_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" id="action-links">
        <div class="col-sm-4 col-md-3">
          <div class="input-group">
            <select class="form-control input-sm" name="mass_action">
              <option value="-">-- Yang ditandai --</option>
              <option value="hapus">Hapus</option>
              <option value="edit-status/pembayaran-pending">Status pembayaran pending</option>
              <option value="edit-status/pembayaran-sukses">Status pembayaran sukses</option>
              <option value="edit-status/pembayaran-refund">Status pembayaran refund</option>
              <option value="edit-status/pengisian-pending">Status pengisian pending</option>
              <option value="edit-status/pengisian-sukses">Status pengisian sukses</option>
              <option value="edit-status/pengisian-gagal">Status pengisian gagal</option>
            </select>
            <span class="input-group-btn"><button class="btn btn-primary btn-sm" type="submit">GO</button></span>
          </div>
        </div>
        <div class="col-sm-8 col-md-9"><?php echo pagination('', $start, $total, $this->system->set['list_per_page'], site_url('admin/transaksi/index/%d?id='.$id.'&amp;username='.$username.'&amp;produk='.$produk.'&amp;provider_id='.$provider_id.'&amp;voucher_id='.$voucher_id.'&amp;server='.$server.'&amp;no_hp='.urlencode($no_hp).'&amp;pembayaran='.$pembayaran.'&amp;dari_tanggal='.urlencode($dari_tanggal).'&amp;ke_tanggal='.urlencode($ke_tanggal).'&amp;status_pembayaran='.urlencode($status_pembayaran).'&amp;status_pengisian='.urlencode($status_pengisian)).'#history');?></div>
      </div>
      <?php echo form_close();?>
      <?php endif;?>
    </div>
    <div class="modal fade" id="filterModal" tabindex="-1" role="dialog" aria-labelledby="filterModal-label" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content3">
      <form method="get" action="<?php echo site_url('admin/transaksi');?>">
        <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"><i class="fa fa-filter"></i> Filter</h4></div>
        <div class="modal-body" style="padding-bottom: 0;">
          <div class="row">
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>ID Transaksi</label>
              <input class="form-control" type="number" name="id" value="<?php echo html_escape(@$id);?>"/>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Username</label>
              <input class="form-control" id="username" type="text" name="username" value="<?php echo html_escape($username);?>"/>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>No. HP / ID Pelanggan</label>
              <input class="form-control" id="no_hp" type="text" name="no_hp" value="<?php echo html_escape($no_hp);?>"/>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Jenis Produk</label>
              <select class="form-control" name="produk">
                <option value="">-- Pilih --</option>
                <?php foreach ($this->system->produk as $pkey=>$pval):?>
                <option value="<?php echo $pkey;?>"<?php echo ($pkey == @$produk ? ' selected="selected"' : '')?>><?php echo html_escape($pval->nama);?></option>
                <?php endforeach;?>
              </select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>-----</label>
              <select class="form-control" name="provider_id" disabled="disabled"></select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>-----</label>
              <select class="form-control" name="voucher_id" disabled="disabled"></select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Server</label>
              <select class="form-control" name="server">
                <option value="0">-- Pilih --</option>
                <?php foreach ($servers as $sv):?>
                <option value="<?php echo $sv['sv_id'];?>"<?php echo ($sv['sv_id'] == $server ? ' selected="selected"' : '')?>><?php echo html_escape($sv['sv_nama']);?></option>
                <?php endforeach;?>
              </select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Pembayaran</label>
              <select class="form-control" name="pembayaran">
                <option value="" <?php echo ($pembayaran == '' ? 'selected' : '');?>>-- Pilih --</option>
                <?php foreach ($this->payment->get() as $payment):?>
                <option value="<?php echo $payment->key;?>" <?php echo ($pembayaran == $payment->key ? 'selected' : '');?>><?php echo html_escape($payment->nama);?></option>
                <?php endforeach;?>
              </select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Dari Tanggal</label>
              <input class="form-control" id="dari_tanggal" type="text" name="dari_tanggal" value="<?php echo html_escape($dari_tanggal);?>" placeholder="HH-BB-TTTT" autocomplete="off"/>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Ke Tanggal</label>
              <input class="form-control" id="ke_tanggal" type="text" name="ke_tanggal" value="<?php echo html_escape($ke_tanggal);?>" placeholder="HH-BB-TTTT" autocomplete="off"/>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Status Pembayaran</label>
              <select class="form-control" name="status_pembayaran">
                <option value="" <?php echo ($status_pembayaran == '' ? 'selected' : '');?>>-- Pilih --</option>
                <option value="sukses" <?php echo ($status_pembayaran == 'sukses' ? 'selected' : '');?>>Sukses</option>
                <option value="pending" <?php echo ($status_pembayaran == 'pending' ? 'selected' : '');?>>Pending</option>
                <option value="refund" <?php echo ($status_pembayaran == 'refund' ? 'selected' : '');?>>Refund</option>
              </select>
            </div>
            <div class="col-xs-6 col-md-4" style="margin-bottom: 15px;">
              <label>Status Pengisian</label>
              <select class="form-control" name="status_pengisian">
                <option value="" <?php echo (empty($status_pengisian) ? 'selected' : '');?>>-- Pilih --</option>
                <option value="sukses" <?php echo ($status_pengisian == 'sukses' ? 'selected' : '');?>>Sukses</option>
                <option value="pending" <?php echo ($status_pengisian == 'pending' ? 'selected' : '');?>>Pending</option>
                <option value="manual" <?php echo ($status_pengisian == 'manual' ? 'selected' : '');?>>Manual</option>
                <option value="gagal" <?php echo ($status_pengisian == 'gagal' ? 'selected' : '');?>>Gagal</option>
              </select>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <a href="<?php echo site_url('admin/transaksi');?>" class="btn btn-default"><i class="fa fa-times"></i> Reset</a>
          <button type="submit" class="btn btn-primary"><i class="fa fa-check"></i> Submit</button>
        </div>
      </form>
    </div></div></div>
    <?php include_once(VIEWPATH.'includes/foot.php'); ?>
    <script src="<?php echo base_url('assets/js/bootstrap-datepicker.min.js');?>"></script>
    <script src="<?php echo base_url('assets/js/locales/bootstrap-datepicker.id.min.js');?>" charset="UTF-8"></script>
    <script type="text/javascript">
    var providerSelected = <?php echo $provider_id;?>;
    var voucherSelected = <?php echo $voucher_id;?>;
    var provider = $('[name="provider_id"]');
    var voucher = $('[name="voucher_id"]');
    var voucherLabel = "";
    function clearProvider(){provider.empty();provider.parent().find("label").text("-----");provider.attr("disabled","disabled");}function clearVoucher(){voucher.empty();voucher.parent().find("label").text("-----");voucher.attr("disabled","disabled");}function getProvider(p){$.get("<?php echo site_url('admin/transaksi/index/get-provider');?>/"+p,function(data){if(data.results.length==0){clearProvider();clearVoucher();}else{var option='<option value="">-- Pilih --</option>';var i;for(i=0;i<data.results.length;i++){if(providerSelected==data.results[i].id){getVoucher(data.results[i].id);option+='<option value="'+data.results[i].id+'" selected="selected">'+data.results[i].name+'</option>';}else{option+='<option value="'+data.results[i].id+'">'+data.results[i].name+'</option>';}}provider.html(option);provider.removeAttr("disabled");provider.parent().find("label").text(data.label.provider);voucherLabel=data.label.voucher;}}).fail(function(){clearProvider();clearVoucher();});}function getVoucher(p){$.get("<?php echo site_url('admin/transaksi/index/get-voucher');?>/"+p,function(data){if(data.length==0){clearVoucher();}else{var option='<option value="">-- Pilih --</option>';var i;for(i=0;i<data.length;i++){option+='<option value="'+data[i].id+'" '+(voucherSelected==data[i].id?'selected="selected"':'')+'>'+data[i].name+'</option>';}voucher.html(option);voucher.parent().find("label").text(voucherLabel);voucher.removeAttr("disabled");}}).fail(function(){clearVoucher();});}$(document).on("change",'[name="produk"]',function(){clearProvider();clearVoucher();var p=$('[name="produk"] option:selected').val();if(p!=""){getProvider(p);}});$(document).on("change",'[name="provider_id"]',function(){clearVoucher();var p=$('[name="provider_id"] option:selected').val();if(p!=""){getVoucher(p);}});
    <?php if ($produk):?>getProvider($('[name="produk"] option:selected').val());<?php endif;?>
    $("#form-button").click(function(){$("#filterModal").modal();return false;});
    $(document).on("submit","#form-confirm",function(){var formData=$(this).serialize();if(formData.indexOf('trx')>0){var action=$('select[name="mass_action"] option:selected').val();if (action=='-'){alert('Silakan pilih tindakan anda');return false;}$("#myModal").modal("show");$.ajax({type:"POST",url:"<?php echo site_url('admin/transaksi');?>/"+action,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;});
    <?php $tgl = explode(' ', format_tanggal(time()));?>
    $(document).on("focus","#dari_tanggal, #ke_tanggal",function(){$(this).datepicker({format:"dd-mm-yyyy",endDate:"<?php echo str_replace('/', '-', $tgl[0]);?>",todayHighlight:true,autoclose:true,language:"id"});});
    $(document).ready(function(){$('#select-all').click(function(event){if(this.checked){$('.checkbox1').each(function(){this.checked=true;})}else{$('.checkbox1').each(function(){this.checked=false;})}});});<?php if ($total):?>var dl = false;$("#dl-link").click(function(){dl=true;$("#myModal").modal("show");return false;});$(document.body).on("show.bs.modal",function(){if(dl==false)return;setTimeout(function(){var html='<form id="downloadForm" method="get" class="form-horizontal" action="#">';html+='<div class="form-group"><label class="col-sm-3 control-label">Format</label><div class="col-sm-9"><select class="form-control" name="format" required="required"><option value="xls">Excel (.xls)</option><option value="xlsx">Excel (.xlsx)</option></select></div></div>';html+='<div class="form-group" style="margin-bottom:0"><div class="col-sm-offset-3 col-sm-9"><button type="submit" class="btn btn-primary"><i class="fa fa-download"></i>Download</button>&nbsp;<button type="button" class="btn btn-default" data-dismiss="modal"><i class="fa fa-remove"></i>Batal</button></div></div>';html+='</form>';$(".modal-content").html('<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">Download Riwayat Transaksi</h4></div><div class="modal-body">'+html+'</div>');},500);});$(document.body).on("show.bs.modal",function(){dl=false;});var errors = 0;function download(page,format){$.get("<?php echo site_url('admin/transaksi/download');?>/"+format+"/ajax?username=<?php echo $username;?>&server=<?php echo $server;?>&no_hp=<?php echo urlencode($no_hp);?>&pembayaran=<?php echo $pembayaran;?>&dari_tanggal=<?php echo urlencode($dari_tanggal);?>&ke_tanggal=<?php echo urlencode($ke_tanggal);?>&status_pembayaran=<?php echo urlencode($status_pembayaran);?>&status_pengisian=<?php echo urlencode($status_pengisian);?>&page="+page,function(data){$(".progress-bar").attr("aria-valuenow",data.progress).css("width",data.progress+"%").text(data.progress+"%");if(data.next_page>0){download(data.next_page,format);}else{setTimeout(function(){$("#dl-link").show();$("#progress").hide();window.location="<?php echo site_url('admin/transaksi/download');?>/"+format;},1000);}}).fail(function(){if(errors>=20){$("#dl-link").show();$("#progress").hide();alert("Terjadi kesalahan!");}else{errors+=1;setTimeout(function(){download(page,format);},500);}});}$(document).on("submit","#downloadForm",function(){var format=$('#downloadForm [name="format"] option:selected').val();$("#dl-link").hide();$(".progress-bar").attr("aria-valuenow","0").css("width","0%").text("0%");$("#progress").show();$("[data-dismiss=modal]").trigger({type:"click"});download(1,format);return false;});<?php endif;?></script>
  </body>
</html>