<?php
defined('BASEPATH') or exit('No direct script access allowed');
function set_trx($matches) {
  if ($matches[1] == 'Deposit')
    return $matches[1] . $matches[2] . '#<a href="'.site_url('admin/riwayat-deposit/view/'.$matches[3]).'">'.$matches[3].'</a>';
  else
    return $matches[1] . $matches[2] . '#<a href="'.site_url('admin/transaksi/view/'.$matches[3]).'">'.$matches[3].'</a>';
}
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Mutasi Saldo</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">nav{margin-bottom: 15px;float: right;}</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>Mutasi Saldo</span></li>
      </ul>
      <div class="row">
        <div class="col-sm-4 col-md-3" id="sidebar">
          <div class="panel panel-default">
            <div class="panel-heading">
              <h3><a class="pull-right" style="color: inherit;" href="#"><i class="fa fa-chevron-up"></i></a> <i class="fa fa-filter"></i> Filter</h3>
            </div>
            <div class="panel-body c">
              <form method="GET" action="<?php echo site_url('admin/riwayat-saldo');?>">
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Username"><span style="width: 13px; display: block;"><i class="fa fa-user-o"></i></span></span>
                    <input type="text" class="form-control" name="username" value="<?php echo $username != 'all' ? html_escape($username) : '';?>" placeholder="Username" maxlength="16" />
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Dari tanggal"><i class="fa fa-calendar"></i></span>
                    <input type="text" class="form-control" name="from_date" id="from_date" value="<?php echo html_escape($from_date);?>" placeholder="HH-BB-TTTT" maxlength="10" autocomplete="off"/>
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Ke tanggal"><i class="fa fa-calendar"></i></span>
                    <input type="text" class="form-control" name="to_date" id="to_date" value="<?php echo html_escape($to_date);?>" placeholder="HH-BB-TTTT" maxlength="10" autocomplete="off"/>
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Keterangan"><span style="width: 13px; display: block;"><i class="fa fa-sticky-note-o"></i></span></span>
                    <input type="text" class="form-control" name="keterangan" value="<?php echo html_escape($keterangan);?>" placeholder="Keterangan" minlength="3" maxlength="32" />
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Jumlah"><span style="width: 13px; display: block;"><i class="fa fa-money"></i></span></span>
                    <input type="number" class="form-control" name="jumlah" value="<?php echo $jumlah;?>" placeholder="0" min="1" max="9999999999"/>
                  </div>
                </div>
                <div class="form-group">
                  <div class="input-group">
                    <span class="input-group-addon" data-toggle="tooltip" title="Jenis"><span style="width: 13px; display: block;"><i class="fa fa-exchange"></i></span></span>
                    <select class="form-control" name="type">
                      <option value="all" <?php echo $type == 'all' ? 'selected="selected"' : '';?>>-- Semua --</option>
                      <option value="kredit" <?php echo $type == 'kredit' ? 'selected="selected"' : '';?>>Kredit</option>
                      <option value="debet" <?php echo $type == 'debet' ? 'selected="selected"' : '';?>>Debet</option>
                    </select>
                  </div>
                </div>
                <div>
                  <button type="submit" class="btn btn-primary btn-block">Tampilkan</button>
                </div>
              </form>
            </div>
          </div>
          <div class="panel panel-default">
            <div class="panel-heading"><h3><a class="pull-right" style="color: inherit;" href="#"><i class="fa fa-chevron-up"></i></a> <i class="fa fa-credit-card"></i> Total Mutasi</h3></div>
          <ul class="list-group c">
            <li class="list-group-item"><strong>Kredit</strong> <span class="badge"><?php echo format_uang2($total_kredit);?></span></li>
            <li class="list-group-item"><strong>Debet</strong> <span class="badge"><?php echo format_uang2($total_debet);?></span></li>
          </ul>
          </div>
        </div>
        <div class="col-sm-8 col-md-9">
          <?php if (!$total):?>
          <div class="alert alert-warning">Tidak ada mutasi saldo.</div>
          <?php else:?>
          <?php echo form_open(site_url('admin/riwayat-saldo/hapus/'.$username),'id="form-confirm"');?>
          <div class="panel panel-default" id="history">
            <div class="panel-heading">
              <h3><i class="fa fa-money"></i> Mutasi Saldo</h3>
            </div>
            <div class="table-responsive">
              <table class="table table-hover">
                <thead>
                  <tr>
                    <th class="text-center" style="width: 40px;"><input type="checkbox" id="select-all"/></th>
                    <th>User</th>
                    <th>Tanggal</th>
                    <th>Keterangan</th>
                    <th>Jumlah</th>
                    <th style="width: 90px;">Saldo Akhir</th>
                  </tr>
                </thead>
                <tbody>
                  <?php foreach ($results as $res):?>
                  <tr>
                    <td class="text-center"><input type="checkbox" class="checkbox1" name="mutasi_id[]" value="<?php echo $res->id;?>"/></td>
                    <td><?php if ($res->us_username):?><a href="<?php echo site_url('admin/users/profil/'.$res->us_id);?>"><?php echo html_escape($res->us_name);?></a><?php endif;?></td>
                    <td style="white-space: nowrap;"><?php echo format_tanggal($res->tanggal);?></td>
                    <td><?php echo preg_replace_callback("/(Trx|Deposit)(.*?)#([0-9]{1,12})/i", "set_trx", $res->info);?></td>
                    <td>
                    <?php if ($res->kredit): ?>
                    <a href="<?php echo site_url('admin/users/edit-saldo/'.$res->us_id).'?jumlah=-'.$res->kredit.'&amp;keterangan='.urlencode('Koreksi > '.$res->info);?>" data-toggle="modal" data-target="#myModal"><span class="text-success" data-toggle="tooltip" title="Kredit">+<?php echo format_uang($res->kredit);?><span></a>
                    <?php else: ?>
                    <a href="<?php echo site_url('admin/users/edit-saldo/'.$res->us_id).'?jumlah='.$res->debet.'&amp;keterangan='.urlencode('Refund > '.$res->info);?>" data-toggle="modal" data-target="#myModal"><span class="text-danger" data-toggle="tooltip" title="Debet">-<?php echo format_uang($res->debet);?><span></a>
                    <?php endif;?>
                    </td>
                    <td><?php echo $res->saldo_akhir ? format_uang($res->saldo_akhir) : '';?></td>
                  </tr>
                  <?php endforeach;?>
                </tbody>
              </table>
            </div>
          </div>
          <div class="row" style="margin-bottom: 5px;">
            <div class="col-xs-12 col-sm-4 col-md-3">
              <button class="btn btn-danger btn-block btn-sm" style="margin-bottom: 15px;" type="submit" name="delete" value="1"><i class="fa fa-trash"></i> Hapus yang ditandai</button>
            </div>
            <div class="col-xs-12 col-sm-8 col-md-9">
              <?php echo preg_replace('/\/([0-9]+)#history/','/$1?from_date='.urlencode($from_date).'&amp;to_date='.urlencode($to_date).'&amp;keterangan='.urlencode($keterangan).'&amp;jumlah='.$jumlah.'&amp;type='.$type.'#history', pagination(site_url('admin/riwayat-saldo/index/'.$username),$start,$total,$this->system->set['list_per_page'],'/%d#history'));?>
            </div>
          </div>
          <?php echo form_close();?>
          <?php endif;?>
        </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">
      $(".panel > .panel-heading > h3 > a").click(function () {
        var panelBody = $(this).parents(".panel").find(".c");
        if (panelBody.hasClass('hidden')) {
          panelBody.removeClass('hidden');
          $(this).html('<i class="fa fa-chevron-up"></i>');
        } else {
          panelBody.addClass('hidden');
          $(this).html('<i class="fa fa-chevron-down"></i>');
        }
        return false;
      });
      $('#select-all').click(function(event) {
        if(this.checked) {
          $('.checkbox1').each(function() {
            this.checked = true;
          });
        } else {
          $('.checkbox1').each(function() {
            this.checked = false;
          });
        }
      });
      $(document).on("submit","#form-confirm", function() {
      var formData = $(this).serialize();
      if (formData.indexOf('mutasi_id') > 0){
        $("#myModal").modal("show");
        $.ajax({
          type: "POST",
          url: $(this).attr("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()));?>
      $("#from_date").datepicker({format:"dd-mm-yyyy",endDate:"<?php echo str_replace('/', '-', $tgl[0]);?>",todayHighlight:true,autoclose:true,language:"id"});
      $("#to_date").datepicker({format:"dd-mm-yyyy",endDate:"<?php echo str_replace('/', '-', $tgl[0]);?>",todayHighlight:true,autoclose:true,language:"id"});
    </script>
  </body>
</html>