<?php
include '../../config/conn.php';
if ($_GET['aksi'] == '') { ?>
  <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>PEMBAYARAN DANA AWAL MASUK (DAM) <small></small></h2>
              <div class="clearfix"></div>
            </div>

            <form method="post" action="index.php?module=pembayaran-dam&aksi=tambah_dam">
              <label for="id">Scan barcode Id Santri :</label>
              <input type="text" size="15px" name="id" autofocus onchange="this.form.submit();">
            </form>



            <div class="divider-dashed"></div>

            <h2 align="center">PILIH DATA SANTRI</h2>
            <div class="divider-dashed"></div>
            <div class="x_content">
              <table id="datatable" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th width="50">No</th>
                    <th>ID santri</th>
                    <!--<th>No Rekening</th>-->
                    <th>Nama</th>
                    <th>Alamat</th>
                    <th>Tanggal lahir</th>
                    <th>Status</th>

                    <th width="110">Aksi</th>

                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT * FROM santri ORDER BY id_santri");
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><?php echo $row['id_santri']; ?></td>
                      <!--<td><?php //echo $row['no_rekening'];
                              ?></td>-->
                      <td><?php echo $row['nama']; ?></td>
                      <td><?php echo $row['alamat']; ?></td>
                      <td><?php echo $row['tanggal_lahir']; ?></td>
                      <td><?php $kueri  = mysqli_query($conn, "SELECT * FROM jenis_status WHERE id_status='$row[statuus]' order by id_status asc");
                          while ($data = mysqli_fetch_array($kueri)) {
                            echo "$data[nama_status]";
                          } ?></td>

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

                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>




<?php  } elseif ($_GET['aksi'] == 'tambah_dam') {
  if ($_GET['id'] == "") {
    $idd = $_POST['id'];
  } else {
    $idd = $_GET['id'];
  }
  $id = $idd;
  $query = mysqli_query($conn, "SELECT * FROM santri WHERE id_santri='$id'");
  $r = mysqli_fetch_array($query);
  $tgl = date('d-m-Y', strtotime($r['tanggal_lahir']));
  $tgl1 = date('d-m-Y', strtotime($r['tanggal_masuk']));
?>

  <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" style="padding-bottom:50px;">
            <div class="x_title" style="text-transform: capitalize;">
              <h2>Data <?php echo $_GET['module']; ?></h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <div class="row">
                <div class="col-md-6">
                  <label for="id">ID santri :</label>
                  <input type="text" class="form-control" disabled value="<?php echo $r['id_santri']; ?>">
                  <input type="hidden" class="form-control" name="id" value="<?php echo $r['id_santri']; ?>">

                  <label for="nama">Nama :</label>
                  <input type="text" class="form-control" name="nama" disabled value="<?php echo $r['nama']; ?>">

                  <label for="nama">Tempat Lahir :</label>
                  <input type="text" class="form-control" name="tempat_lahir" disabled value="<?php echo $r['tempat_lahir']; ?>">

                  <label for="nama">Tanggal Lahir :</label>

                  <div class="input-group">
                    <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                    <input type="text" class="form-control " autocomplete="no" disabled value="<?php echo $tgl; ?>" id="tanggal" name="tanggal_lahir">
                  </div>

                </div>
                <div class="col-md-6">
                  <label>Status :</label>
                  <select id="heard" class="form-control" disabled required name="status">
                    <?php if ($r['statuus'] == 0) { ?>
                      <option value="0" selected>- Pilih Status -</option>
                    <?php
                    }
                    $query2  = "SELECT * FROM jenis_status ORDER BY id_status";
                    $tampil2 = mysqli_query($conn, $query2);
                    while ($w = mysqli_fetch_array($tampil2)) {
                      if ($r['statuus'] == $w['id_status']) {
                        echo "<option value=\"$w[id_status]\" selected>$w[nama_status]</option>";
                      } else {
                        echo "<option value=\"$w[id_status]\">$w[nama_status]</option>";
                      }
                    }
                    ?>
                  </select>

                  <label for="nama">Tanggal Masuk :</label>

                  <div class="input-group">
                    <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                    <input type="text" class="form-control " autocomplete="no" disabled value="<?php echo $tgl1; ?>" id="tanggal2" name="tanggal_masuk">
                  </div>


                  <label for="alamat">Alamat :</label>
                  <textarea class="form-control" name="alamat" disabled requerid><?php echo $r['alamat']; ?> </textarea>

                  <label for="nama">Nama Orang Tua :</label>
                  <input type="text" class="form-control" name="orang_tua" disabled value="<?php echo $r['nama_ayah']; ?>">


                </div>
              </div>
              <div class="ln_solid"></div>
              <div class="form-group">
                <button type="button" class="btn btn-default btn-sm" onclick=self.history.back()>Batal</button>

                <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#modalAdd"><i class="glyphicon glyphicon-plus"></i> Tambah Pembayaran</a>


                <!--<button type="submit" onclick="return confirm('Apakah yakin akan diedit?')" class="btn btn-success btn-sm">Simpan</button><br>-->
              </div>
            </div>
            <!--tampil data pembayaran-->
            <table id="datatable" class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th width="10px">No</th>
                  <th width="110px">Kode Transaksi</th>
                  <th>Tanggal</th>
                  <!--<th>total bayar</th>-->
                  <?php
                  $meri = mysqli_query($conn, "SELECT SUM(debit) AS total FROM transaksi WHERE id_jenis='14' AND id_santri='$id'");
                  $ow = mysqli_fetch_array($meri);
                  ?>

                  <th>Jumlah<br>Total: Rp.<?php echo rupiah($ow['total']); ?></th>
                  <th>Jenis Kas</th>
                  <th>Keterangan</th>
                  <th>Cetak</th>
                  <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>

                    <th width="110px">Aksi</th>
                  <?php } ?>
                </tr>
              </thead>
              <tbody>
                <?php
                $no = 0;

                $idd = $_GET['id'];
                $query = mysqli_query($conn, "SELECT * FROM transaksi WHERE id_jenis='14' AND id_santri='$id' ORDER BY tanggal DESC");
                while ($row = mysqli_fetch_array($query)) {
                  $no++;
                ?>
                  <tr>
                    <td><?php echo $no; ?></td>
                    <td><?php echo $row['id_transaksi']; ?></td>
                    <td><?php
                        echo "$row[tanggal]";
                        ?></td>
                    <td><?php echo "Rp." . rupiah($row['debit']); ?></td>
                    <td>
                      <!-- jenis kas -->
                      <?php
                      echo jenkas($row['jenis_kas']);
                      ?>
                    </td>
                    <td><?php echo $row['keterangan']; ?></td>
                    <td width="50px"><a class="btn btn-primary btn-xs" onclick="window.open('component/com_pembayaran/cetak.php?id_tran=<?php echo $row['id_transaksi']; ?>','nama window','width=1000,height=500,toolbar=no,location=no,directories=no,status=no,menubar=no, scrollbars=no,resizable=yes,copyhistory=no')"><i class="glyphicon glyphicon-print"></i></a></td>
                    <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                      <td><a class='open_modal btn  btn-default btn-xs' href="?module=<?php echo $_GET['module']; ?>&aksi=form_edit&id_tran=<?php echo $row['id_transaksi']; ?>&id=<?php echo $row['id_santri']; ?>"><i class="glyphicon glyphicon-edit"></i> Edit</a>
                        <a class="btn  btn-danger btn-xs" onclick="confirm_modal('?module=<?php echo $_GET['module']; ?>&aksi=hapus&id=<?php echo $row['id_santri']; ?>&id_tran=<?php echo $row['id_transaksi']; ?>');"><i class="glyphicon glyphicon-trash"></i> Hapus</a>
                      </td>
                    <?php } ?>
                  </tr>
                <?php } ?>

              </tbody>
            </table>

            <?php include 'tagihan.php'; ?>


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

  <!--form tambah pembayaran-->
  <div class="modal fade" id="modalAdd" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <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" id="myModalLabel">Tambah Data Pembayaran</h4>
        </div>
        <div class="modal-body">
          <form action="?module=<?php echo "pembayaran-dam"; ?>&aksi=simpan" enctype="multipart/form-data" method="POST">
            <?php
            $query = "SELECT max(id_transaksi) as maxID FROM transaksi ";
            $hasil = mysqli_query($conn, $query);
            $data = @mysqli_fetch_array($hasil);
            $rand = rand(100, 999);
            $urut = date('ymdHi');

            $char = $urut . $rand;
            $newID = $char;
            ?>
            <label for="id">ID Transaksi * :</label>
            <input type="text" class="form-control" disabled value="<?php echo id_transaksi(); ?>">
            <input type="hidden" class="form-control" name="id" value="<?php echo id_transaksi(); ?>">
            <label for="id">ID Santri * :</label>
            <input type="text" class="form-control" disabled value="<?php echo $id; ?>">
            <input type="hidden" class="form-control" name="id_santri" value="<?php echo $id; ?>">

            <label f>Tanggal pembayaran * :</label>
            <div class="input-group">
              <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
              <input type="text" class="form-control " disabled autocomplete="off" value="<?php echo date('d-m-Y'); ?>" id="tanggal4" required>
              <input type="hidden" value="<?php echo date('d-m-Y'); ?>" name="tanggal">

            </div>

            <label>Jumlah pembayaran *:</label>
            <input type="number" class="form-control" autocomplete="off" name="jumlah_bayar" required>

            <label>Jenis Pembayaran * :</label>
            <select id="heard" class="form-control" required name="jenis_kas">

              <option value="" selected>- Pilih Jenis -</option>
              <?php

              $query2  = "SELECT * FROM jenis_kas ORDER BY id";
              $tampil2 = mysqli_query($conn, $query2);
              while ($w = mysqli_fetch_array($tampil2)) {
                echo "<option value=\"$w[id]\">$w[nama]</option>";
              }
              ?>
            </select>

            <label for="alamat">Keterangan :</label>
            <textarea class="form-control" name="keterangan"></textarea>


        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Batal</button>
          <button type="submit" class="btn btn-success btn-sm">Simpan</button>
        </div>
        </form>
      </div>
    </div>
  </div>
  <!--untuk edit data dam-->
<?php } elseif ($_GET['aksi'] == 'form_edit') { ?>
  <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">

            <form action="?module=<?php echo "pembayaran-dam"; ?>&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
              <?php


              $query = "SELECT * FROM transaksi WHERE id_transaksi='$_GET[id_tran]' ";
              $hasil = mysqli_query($conn, $query);
              $data = mysqli_fetch_array($hasil);

              ?>
              <label for="id">ID Transaksi * :</label>
              <input type="text" class="form-control" disabled value="<?php echo $_GET['id_tran'];
                                                                      ?>">
              <input type="hidden" class="form-control" name="id" value="<?php echo $_GET['id_tran']; ?>">
              <label for="id">ID Santri * :</label>
              <input type="text" class="form-control" disabled value="<?php echo $data['id_santri']; ?>">
              <input type="hidden" class="form-control" name="id_santri" value="<?php echo $data['id_santri']; ?>">

              <label f>Tanggal pembayaran * :</label>
              <div class="input-group">
                <div class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></div>
                <input type="text" class="form-control " autocomplete="off" value="<?php echo date('d-m-Y', strtotime($data['tanggal'])); ?>" id="tanggal4" name="tanggal" required>
              </div>

              <label>Jumlah pembayaran *:</label>
              <input type="number" class="form-control" autocomplete="off" name="jumlah_bayar" value="<?php echo $data['debit']; ?>" required>

              <label>Jenis Pembayaran * :</label>
              <select id="heard" class="form-control" required name="jenis_kas">

                <option value="" selected>- Pilih Jenis -</option>
                <?php

                $query2  = "SELECT * FROM jenis_kas ORDER BY id";
                $tampil2 = mysqli_query($conn, $query2);
                while ($w = mysqli_fetch_array($tampil2)) {
                ?>
                  <option value="<?php echo $w['id']; ?>" <?php if ($data['jenis_kas'] == $w['id']) {
                                                            echo "selected";
                                                          } ?>><?php echo $w['nama']; ?></option>";
                <?php
                }
                ?>
              </select>

              <label for="alamat">Keterangan :</label>
              <textarea class="form-control" name="keterangan"><?php echo $data['keterangan']; ?></textarea>


          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default btn-sm" onclick=self.history.back()>Batal</button>
            <button type="submit" class="btn btn-success btn-sm">Simpan</button>
          </div>
          </form>

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





<?php } elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $tgl = date('Y-m-d', strtotime($_POST['tanggal']));
  $save = mysqli_query($conn, "INSERT INTO transaksi(id_transaksi,
                                  id_santri,
                                  tanggal,
                                  debit,
                                  kredit,
                                  keterangan,id_jenis,id_pegawai,jenis_kas) VALUES('$_POST[id]',
                                                 '$_POST[id_santri]',
                                                 '$tgl',
                                                 '$_POST[jumlah_bayar]',
                                                 '0',
                                                 '$_POST[keterangan]','14','$_SESSION[id]','$_POST[jenis_kas]')");



  $jen = '14';
  $id_san = $_POST['id_santri'];
  // echo "<script language='javascript'>document.location='?module=pembayaran-dam&aksi=tambah_dam&id=$id_san';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=pembayaran-dam&aksi=tambah_dam&id=$id_san');</script>";
  } else {
    historysimpan($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jen);
    echo "<script>ySimpan('?module=pembayaran-dam&aksi=tambah_dam&id=$id_san');</script>";
  }
} elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $id_san = $_POST['id_santri'];
  $tgl1 = date('Y-m-d', strtotime($_POST['tanggal']));

  //log edit 
  $query = "SELECT * FROM transaksi WHERE id_transaksi='$_POST[id]' ";
  $hasil = mysqli_query($conn, $query);
  $data = mysqli_fetch_array($hasil);


  $logedit = $data['log_edit'] . "[" . $_SESSION['id'] . "~" . date('Y-m-d H:i:s') . "]";

  $save = mysqli_query($conn, "UPDATE transaksi SET tanggal = '$tgl1',
                                    debit = '$_POST[jumlah_bayar]',
                                    keterangan = '$_POST[keterangan]',
                                    jenis_kas = '$_POST[jenis_kas]',
                                    log_edit='$logedit'
                                    WHERE id_transaksi = '$_POST[id]'") or die("GAGAL");

  $jen = '14';
  // echo "<script language='javascript'>document.location='?module=pembayaran-dam&aksi=tambah_dam&id=$id_san';</script>";
  if (!$save) {
    echo "<script>nSimpan('?module=pembayaran-dam&aksi=tambah_dam&id=$id_san');</script>";
  } else {
    historyedit($_POST['id'], $_SESSION['id'], $_SESSION['mac'], $jen);
    echo "<script>ySimpan('?module=pembayaran-dam&aksi=tambah_dam&id=$id_san');</script>";
  }
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  $query = mysqli_query($conn, "DELETE FROM transaksi WHERE id_santri='$id' AND id_transaksi='$_GET[id_tran]'");
  // echo "< language='javascript'>document.location='?module=pembayaran-dam&aksi=tambah_dam&id=$_GET[id]';</>";
  $jen = '14';
  if (!$query) {
    echo "<script>nHapus('?module=pembayaran-dam&aksi=tambah_dam&id=$id');</script>";
  } else {
    historyhapus($_GET['id_tran'], $_SESSION['id'], $_SESSION['mac'], $jen);
    echo "<script>yHapus('?module=pembayaran-dam&aksi=tambah_dam&id=$id');</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;">Apakah anda yakin menghapus data ini ?</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">Hapus</a>
        <button type="button" class="btn btn-success btn-sm" data-dismiss="modal">Batal</button>
      </div>
    </div>
  </div>
</div>