<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];
  //saldo aman
  $saldoaman = mysqli_query($conn, "SELECT * FROM pengaturan_emoney WHERE id='1'");
  $aman = mysqli_fetch_array($saldoaman);

?>
  <!-- page content -->
  <div class="col" role="main">
    <div class="">
      <div class="clearfix"></div>

      <div class="row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <div class="x_panel">
            <div class="x_title" style="text-transform: capitalize;">

              <h2>PILIH NASABAH SANTRI <small></small></h2>

              <div class="clearfix"></div>






            </div>
            <?php
            if (isset($_GET['periode']) and $_GET['periode'] == "y") {
              $link = "?module=transaksi-santri&periode=x&bln=$bln&thn=$thn";
            } elseif (isset($_GET['periode']) and $_GET['periode'] == "x") {
              $link = "?module=transaksi-santri&periode=x&bln=$bln&thn=$thn";
            } else {
              $link = "?module=transaksi-santri";
            }
            ?>

            <div class="col-md-12 col-sm-12 col-xs-12" style="padding: 4px;">
              <form action="<?php echo $link; ?>" id="form_id" enctype="multipart/form-data" method="POST">

                <div class="col-md-3">


                  <div class="input-group">
                    <div class="input-group-addon">Status Saldo</div>
                    <select id="heard" class="form-control" required name="tampildata" onChange="document.getElementById('form_id').submit();">
                      <option value="0">Semua data</option>
                      <option value="1" <?php if ($_POST['tampildata'] == 1) {
                                          echo "selected";
                                        } ?>>Aman</option>
                      <option value="2" <?php if ($_POST['tampildata'] == 2) {
                                          echo "selected";
                                        } ?>>Menipis (<?php echo "<=" . rupiah($aman['isi']); ?>)</option>

                    </select>
                  </div>

                  <!-- eksport -->
                  <br>
                  <?php
                  //sort lunas
                  if (isset($_POST['tampildata'])) {
                    if ($_POST['tampildata'] == 0) {
                      $sort_lunas = "&tampildata=0";
                    } elseif ($_POST['tampildata'] == 1) {
                      $sort_lunas = "&tampildata=1";
                    } elseif ($_POST['tampildata'] == 2) {
                      $sort_lunas = "&tampildata=2";
                    }
                  } else {
                    $sort_lunas = "&tampildata=0";
                  }

                  // sort tahun
                  if (isset($_POST['angkatan'])) {
                    $sort_angkatan = "&angkatan=$_POST[angkatan]";
                  } else {
                    $sort_angkatan = "&angkatan=0";
                  }

                  $eksport = $sort_lunas . $sort_angkatan;
                  ?>

                  <!-- <a href="component/com_pembayaran/export_data_nasabah.php<?php // echo $link . $eksport; 
                                                                                ?>" class='open_modal btn  btn-primary btn-xs'>Eksport</a> -->




                </div>
              </form>
            </div>








            <div class="x_content">

              <table id="datatable" class="table table-bordered">
                <thead>
                  <tr style="color: black;">
                    <!--<th>Tanggal</th>-->
                    <!--<th>id transaksi</th> -->
                    <th width="15px">NO.</th>
                    <th>Id Santri</th>
                    <th>Nama Santri</th>
                    <th>Status Santri</th>
                    <th>Tanggal masuk</th>
                    <th>Saldo</th>
                    <th>Pinjaman</th>
                    <th width="100">Status Saldo</th>
                    <th>WA wali <i class='glyphicon glyphicon-envelope'></i></th>
                    <th width="100">Bayar</th>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $count = 1;
                  $total_rekap = 0;

                  $hari_ini = "$thn-$bln-01";
                  $tgl_pertama = date('Y-m-01', strtotime($hari_ini));
                  $tgl = date('Y-m-t', strtotime($hari_ini));



                  //sorting data per angkatan
                  // if (!isset($_POST['angkatan']) or $_POST['angkatan'] == 0) {
                  //   $angkatan = "";
                  // } else {
                  //   $angkatan = "AND YEAR(tanggal_masuk)=$_POST[angkatan]";
                  // }


                  //sorting data per jenis status saldo
                  if ($_POST['tampildata'] == 1) {
                    $query = mysqli_query($conn, "SELECT *,SUM(debit),SUM(kredit),(SUM(debit)-SUM(kredit)) AS sisa
                    FROM transaksi_ewallet INNER JOIN santri ON rekening=id_santri
                    GROUP BY id_santri ASC
                    HAVING SUM(debit)-SUM(kredit) >$aman[isi]");
                  } elseif ($_POST['tampildata'] == 2) {
                    $query = mysqli_query($conn, "SELECT *,SUM(debit),SUM(kredit),(SUM(debit)-SUM(kredit)) AS sisa
                    FROM santri LEFT OUTER JOIN transaksi_ewallet ON id_santri=rekening
                    GROUP BY id_santri  
                     
                    HAVING SUM(debit)-SUM(kredit) <=$aman[isi] OR SUM(debit) IS NULL AND SUM(kredit) IS NULL
                    ORDER BY nama  ASC
                    ");
                  } else {
                    $query = mysqli_query($conn, "SELECT * FROM santri  order by nama ASC ");
                  }

                  $ro = 0;




                  while ($row = mysqli_fetch_array($query)) {

                    $no++;
                  ?>

                    <tr style="background: <?php if ($no % 2 == 1) { ?>
                          #ececec;
                          <?php } else { ?>
                            white;
                            <?php } ?>">

                      <!--<td><?php //echo $row['id_transaksi'];
                              ?></td>-->
                      <td style=" color:black"><?php echo $no; ?></td>
                      <td style=" color:black"><?php echo $row['id_santri']; ?></td>
                      <td style=" color:black"><?php echo $row['nama']; ?></td>
                      <td style=" color:black"><?php
                                                $query2  = mysqli_query($conn, "SELECT * FROM jenis_status WHERE id_status='$row[statuus]' ORDER BY id_status");
                                                $st = mysqli_fetch_array($query2);
                                                echo $st['nama_status'];
                                                ?>
                      </td>
                      <td style=" color:black"><?php echo $row['tanggal_masuk'];  ?></td>
                      <td style="font-weight: bold; color:black;font-size:medium"><?php echo rupiah(saldo($row['id_santri'])); ?></td>
                      <td style="font-weight: bold; color:black;font-size:medium"><?php echo rupiah(pinjaman($row['id_santri'])); ?></td>

                      <td><?php

                          $saldoaman = mysqli_query($conn, "SELECT * FROM pengaturan_emoney WHERE id='1'");
                          $aman = mysqli_fetch_array($saldoaman);

                          if (saldo($row['id_santri']) <= $aman['isi']) {
                            echo "<font color=#f01519><b>Menipis</b></font>";
                          } else {
                            echo "<font color=#0082da><b>aman</b></font>";
                          }

                          ?></td>

                      <td><a href="https://wa.me/<?php echo $wa; ?>" target="_blank"><?php echo $row['no_hp'] . "<i class='glyphicon glyphicon-send'></i>"; ?></a></td>


                      <td>
                        <a class='open_modal btn  btn-primary btn-xs' href="?module=proses-transaksi&id=<?php echo $row['id_santri']; ?>"><i class="glyphicon glyphicon-edit"></i> Pilih</a>
                      </td>




                      <?php
                      $count++

                      ?>

                    </tr>

                  <?php } ?>





                </tbody>
              </table>
            </div>
          </div>
        </div>

      </div>
    </div>
  </div>
  <!-- /page content --><br>





<?php } ?>