<?php
defined('BASEPATH') or exit('No direct script access allowed');
$bulan = array(
    '01' => 'Januari',
    '02' => 'Februari',
    '03' => 'Maret',
    '04' => 'April',
    '05' => 'Mei',
    '06' => 'Juni',
    '07' => 'Juli',
    '08' => 'Agustus',
    '09' => 'September',
    '10' => 'Oktober',
    '11' => 'November',
    '12' => 'Desember',
    );
?>
<!DOCTYPE html>
<html>
  <head>
    <title>Admin &gt; Rekap Transaksi</title>
    <?php include_once(VIEWPATH.'includes/head.php'); ?>
  </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>Rekap Transaksi</span></li>
      </ul>
      <?php if (!$results):?>
      <div class="alert alert-warning">Tidak ada riwayat transaksi.</div>
      <?php else:?>
      <form method="get" action="<?php echo site_url('admin/rekap-trx');?>">
        <div class="row">
          <div class="col-xs-12 col-lg-4" style="margin-bottom: 15px;">
            <div class="alert alert-info" style="padding: 7px 10px;margin-bottom: 0;">Rekap <?php echo isset($waktu[2]) ? 'tanggal '.html_escape($waktu[2]) : 'bulan';?> <?php echo (isset($bulan[$waktu[1]])  ? $bulan[$waktu[1]] : '') . ' ' . html_escape($waktu[0]);?></div>
          </div>
          <div class="col-xs-6 col-lg-2" style="margin-bottom: 15px;">
            <select class="form-control" name="tanggal" data-toggle="tooltip" data-title="Tanggal">
              <option value="-"<?php echo (!isset($waktu[2]) ? ' selected="selected"' : '');?>>Semua Tanggal</option><?php for($tgl = 1; $tgl<=31; $tgl++):?><option<?php echo (isset($waktu[2]) && $waktu[2] == $tgl ? ' selected="selected"' : '');?>><?php echo substr('0'.$tgl,-2);?></option><?php endfor;?>
            </select>
          </div>
          <div class="col-xs-6 col-lg-2" style="margin-bottom: 15px;">
            <select class="form-control" name="bulan" data-toggle="tooltip" data-title="Bulan">
              <?php foreach ($bulan as $bln_no=>$bln_nama):?><option value="<?php echo $bln_no;?>"<?php echo ($waktu[1] == $bln_no ? ' selected="selected"' : '');?>><?php echo $bln_nama;?></option><?php endforeach;?>
            </select>
          </div>
          <div class="col-xs-6 col-lg-2" style="margin-bottom: 15px;">
            <select class="form-control" name="tahun" data-toggle="tooltip" data-title="Tahun">
              <option<?php echo ($waktu[0] == date('Y') ? ' selected="selected"' : '');?>><?php echo date('Y');?></option>
              <option<?php echo ($waktu[0] == (date('Y') - 1) ? ' selected="selected"' : '');?>><?php echo date('Y') - 1;?></option>
            </select>
          </div>
          <div class="col-xs-6 col-lg-2" style="margin-bottom: 15px;">
            <button class="btn btn-primary btn-block" type="submit">Tampilkan</button>
          </div>
        </div>
      </form>
      <div class="panel panel-default">
        <div class="panel-heading" style="white-space: nowrap; overflow: hidden;">
          <h3><i class="fa fa-list-alt"></i> Rekap Transaksi</h3>
        </div>
        <div class="table-responsive">
          <table class="table table-bordered table-hover">
            <thead>
              <tr>
                <th>Server</th>
                <th>Total Trx</th>
                <th>Rekap Trx</th>
                <th>Pendapatan Kotor</th>
                <th>Total Pengeluaran</th>
                <th>Pendapatan Bersih</th>
                <th style="width: 80px;text-align:center">Tindakan</th>
              </tr>
            </thead>
            <tbody>
              <?php foreach ($results as $res): ?>
              <tr>
                <td style="vertical-align: middle;"><a href="<?php echo site_url('admin/pengaturan/server/edit/'.$res['id_server']);?>" title="Edit pengaturan server <?php echo $res['nama_server'];?>"><?php echo $res['nama_server'];?></a></td>
                <td style="vertical-align: middle;"><?php echo $res['total_trx'];?></td>
                <td style="vertical-align: middle;"><?php echo $res['rekap_trx'];?></td>
                <td style="vertical-align: middle;"><?php echo format_uang($res['pendapatan_kotor']);?></td>
                <td style="vertical-align: middle;"><?php echo format_uang($res['total_pengeluaran']);?></td>
                <td style="vertical-align: middle;"><?php echo format_uang($res['pendapatan_bersih']);?></td>
                <?php if (isset($res['data']['otomax_api']) && $res['data']['otomax_api']['status'] == '1'):?>
                <td style="width: 80px;text-align:center">
                  <a class="btn btn-primary btn-xs" href="<?php echo site_url('admin/modules/otomax/login/'.$res['id_server']);?>" data-toggle="modal" data-target="#myModal" title="Login OtomaX API"><i class="fa fa-sign-in"></i></a>
                  &nbsp;
                  <a class="btn btn-default btn-xs refresh" title="Rekap ulang transaksi" data-id="<?php echo $res['id_server'];?>" data-server="<?php echo $res['nama_server'];?>"><i class="fa fa-refresh"></i></a>
                </td>
                <?php else:?>
                <td style="width: 80px;"></td>
                <?php endif;?>
              </tr>
              <?php endforeach;?>
            </tbody>
            <tfoot>
              <tr>
                <td></td>
                <td><strong><?php echo $total_trx;?></strong></td>
                <td><strong><?php echo $rekap_trx;?></strong></td>
                <td><strong><?php echo format_uang($pendapatan_kotor);?></strong></td>
                <td><strong><?php echo format_uang($total_pengeluaran);?></strong></td>
                <td><strong><?php echo format_uang($pendapatan_bersih);?></strong></td>
                <td style="width: 80px;"></td>
              </tr>
            </tfoot>
          </table>
        </div>
      </div>
      <?php endif;?>
    </div>
    <?php include_once(VIEWPATH.'includes/foot.php'); ?>
    <script>
    function refresh_trx(event, page = 1) {
      $.get("<?php echo site_url('admin/rekap-trx/rekap-ulang');?>/"+$(event).data('id')+"/<?php echo (!isset($waktu[2]) ? '01' : $waktu[2]).'-'.$waktu[1].'-'.$waktu[0];?>/"+page, function(data) {
        if (data.success != false) {
          if (data.next_page) {
            setTimeout(function(){refresh_trx(event,data.next_page)},1000);
          } else {
            $.notify(data.message, "success");
            $(event).html('<i class="fa fa-refresh"></i>');
          }
        } else {
          $.notify(data.message, "error");
          $(event).removeAttr("disabled").html('<i class="fa fa-refresh"></i>');
        }
      }).fail(function() {
        $.notify("Terjadi kesalahan", "error");
        $(event).removeAttr("disabled").html('<i class="fa fa-refresh"></i>');
      });
    }
    $(document).ready(function(){
      $("a.refresh").click(function() {
        $(this).attr("disabled","disabled").html('<i class="fa fa-refresh fa-spin"></i>');
        refresh_trx($(this));
        return false;
      });
    });
    </script>
  </body>
</html>