<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') {
  $jenis = $_GET['type'];

?>
  <style>


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

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

              <?php if ($_GET['periode'] == 'y') {
                $bln = $_POST['bulan'];
                $thn = $_POST['tahun'];
              } elseif ($_GET['periode'] == 'x') {
                $bln = $_GET['bln'];
                $thn = $_GET['thn'];
              } else {
                $bln = date('n');
                $thn = date('Y');
              }


              ?>
              <h2>Laporan tagihan Dana awal masuk (DAM) Rumah Tahfidz Pagi<small></small></h2>

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




            </div>
            <form action="?module=lap-dam-rt-pagi" id="form_id" enctype="multipart/form-data" method="POST">

              <div class="col-md-3">
                <div class="input-group">
                  <div class="input-group-addon">Angkatan</div>
                  <select id="heard" class="form-control" required name="angkatan" onChange="document.getElementById('form_id').submit();">
                    <option value="0">Semua Tahun</option>
                    <?php $sort_th = mysqli_fetch_array(mysqli_query($conn, "SELECT MAX(YEAR(tanggal_masuk)) as max_th, MIN(YEAR(tanggal_masuk)) as min_th FROM santri")); ?>
                    <?php for ($x = $sort_th['max_th']; $x >= $sort_th['min_th']; $x--) { ?>
                      <option <?php if ($_POST['angkatan'] == $x) {
                                echo "selected";
                              } ?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
                    <?php } ?>
                  </select>
                </div>

                <div class="input-group">
                  <div class="input-group-addon">Status</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";
                                      } ?>>Lunas</option>
                    <option value="2" <?php if ($_POST['tampildata'] == 2) {
                                        echo "selected";
                                      } ?>>Bayar belum lunas</option>
                    <option value="3" <?php if ($_POST['tampildata'] == 3) {
                                        echo "selected";
                                      } ?>>Belum bayar</option>
                  </select>
                </div>
              </div>

            </form>

            <!-- 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";
              } elseif ($_POST['tampildata'] == 3) {
                $sort_lunas = "tampildata=3";
              }
            } 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_dam_rt_pagi.php?<?php echo $eksport; ?>" class='open_modal btn  btn-primary btn-xs'>Eksport</a>






            <?php
            //include 'alert.php';
            ?>


            <div class="x_content">

              <table id="datatable" class="table table-bordered">
                <thead>
                  <tr>
                    <!--<th>Tanggal</th>-->
                    <!--<th>id transaksi</th> -->
                    <th width="15px">NO.</th>
                    <th>Id Santri</th>
                    <th>Nama Santri</th>
                    <th>Kelas</th>
                    <th>Tanggal masuk</th>
                    <th>dibayar</th>
                    <th>Total Bayar</th>
                    <th>Pembayaran</th>
                    <th>Sisa</th>
                    <th width="100">Status</th>
                    <th width="100">Aksi</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]";
                    //$angkatanall untuk menampilkan seluruh data
                    $angkatanall = "WHERE YEAR(tanggal_masuk)=$_POST[angkatan]";
                  }

                  //sorting data per status lunas
                  if ($_POST['tampildata'] == 1) {
                    //data lunas
                    $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri IN(SELECT id_santri from status_lunas WHERE id_jenis='18') $angkatan
                    AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') order by nama ASC ");
                  } elseif ($_POST['tampildata'] == 2) {
                    //bayar cicil
                    $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri IN(SELECT id_santri from transaksi WHERE id_jenis='18') 
                    AND id_santri NOT IN (SELECT id_santri from status_lunas WHERE id_jenis='18') $angkatan 
                    AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') order by nama ASC ");
                  } elseif ($_POST['tampildata'] == 3) {
                    //belum bayar                 
                    $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri NOT IN(SELECT id_santri from status_lunas) 
                    AND jenis_rt IN(SELECT id from jenis_rt WHERE kategori='1') 
                    AND id_santri NOT IN (select id_santri from transaksi WHERE id_jenis='18') $angkatan order by nama ASC ");
                  } else {
                    //semua data
                    $query = mysqli_query($conn, "SELECT * FROM santri_rt $angkatanall 
                    WHERE jenis_rt IN(SELECT id FROM jenis_rt WHERE kategori='1') 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><?php echo $no; ?></td>
                      <td><?php echo $row['id_santri']; ?></td>
                      <td><?php echo $row['nama']; ?></td>
                      <td><?php $kueri  = mysqli_query($conn, "SELECT * FROM jenis_rt WHERE id='$row[jenis_rt]' order by id asc");
                          while ($data = mysqli_fetch_array($kueri)) {
                            echo "$data[nama_jenis]";
                          } ?></td>
                      <td><?php echo $row['tanggal_masuk'];  ?></td>
                      <td><?php
                          $keri = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_santri='$row[id_santri]' AND id_jenis='18' order by tanggal ASC ");
                          while ($ri = mysqli_fetch_array($keri)) {
                            $tgl = date('d/m/y', strtotime($ri['tanggal']));
                            echo "[" . $tgl . "~" . rupiah($ri['debit']) . "]<br>";
                          }
                          ?></td>

                      <td><?php
                          $total = mysqli_query($conn, "SELECT sum(debit) AS total_bayar FROM transaksi WHERE id_santri='$row[id_santri]' AND id_jenis='18'order by tanggal ASC ");
                          $total1 = mysqli_fetch_array($total);
                          echo rupiah($total1['total_bayar']);
                          ?></td>
                      <td><?php echo rupiah($row['biaya_dam']);  ?></td>
                      <td><?php $sisa = $row['biaya_dam'] - $total1['total_bayar'];
                          echo rupiah($sisa);  ?></td>
                      <td><?php
                          $tet1 = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri='$row[id_santri]' AND id_jenis='18'");
                          $tet = mysqli_fetch_array($tet1);
                          if (isset($tet['id_santri'])) {
                            echo "<font color=#0082da><b>lunas</b></font>";
                          } else {
                            echo "<font color=#f01519><b>Belum Lunas</b></font>";
                          }

                          ?></td>
                      <td>
                        <?php
                        if (isset($tet['id_santri'])) {
                          echo "<a class=\"btn  btn-success btn-xs\"  onclick=\"confirm_modal('?module=lap-dam-rt-pagi&aksi=ubahstatus&id=$row[id_santri]&type=18');\">Batal Lunas</a>";
                        } else {
                          echo "<a class=\"btn  btn-danger btn-xs\"  onclick=\"confirm_modal('?module=lap-dam-rt-pagi&aksi=ubahstatus&id=$row[id_santri]&type=18');\">Lunas</a>";
                        } ?>
                      </td>


                      <td><a class='open_modal btn  btn-primary btn-xs' href="?module=pembayaran-dam-rt-pagi&aksi=tambah_dam&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 } elseif ($_GET['aksi'] == "ubahstatus") {
  $tet1 = mysqli_query($conn, "SELECT * FROM status_lunas WHERE id_santri='$_GET[id]' AND id_jenis='$_GET[type]'");
  $tet = mysqli_fetch_array($tet1);
  if (isset($tet['id_santri'])) {
    mysqli_query($conn, "DELETE from status_lunas WHERE id_santri='$_GET[id]' AND id_jenis='$_GET[type]'");
  } else {
    mysqli_query($conn, "INSERT INTO status_lunas(id_santri,
    id_jenis) VALUES('$_GET[id]',
                   '$_GET[type]')");
    //echo "hore";
  }
  $_SESSION['pesan'] = 'Data berhasil di tambahkan';
  echo "<script language='javascript'>document.location='?module=lap-dam-rt-pagi';</script>";
}
?>
<div class="modal fade" id="modal_delete">
  <div class="modal-dialog">
    <div class="modal-content" style="margin-top:100px;">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h4 class="modal-title" style="text-align:center;">Ubah status lunas ?</h4>
      </div>
      <div class="modal-footer" style="margin:0px; border-top:0px; text-align:center;">
        <a href="#" class="btn btn-danger btn-sm" id="delete_link">Ubah</a>
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
      </div>
    </div>
  </div>
</div>