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

  //sorting data per Kelas
  if (!isset($_POST['kelas']) or $_POST['kelas'] == 0) {
    $kelas = "";
    $kelas_ex = "";
  } else {
    $kelas = "AND jenis_rt=$_POST[kelas]";
    $kelas_ex = "&kelas=$_POST[kelas]";
  }

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

  $sort = $kelas . $angkatan;



?>
  <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>Data Santri Rumah Tahfidz <small></small></h2>
              <div class="clearfix"></div>
            </div>
            <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1' or $_SESSION['leveluser'] == 'user2') { ?>
              <a href="?module=santri-rt&aksi=tambah_santri" class="btn btn-success btn-sm" data-toggle="" data-target=""><i class="glyphicon glyphicon-plus"></i> Tambah</a>
            <?php } ?>

            <div class="x_content">
              <form action="?module=santri-rt" 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_rt")); ?>
                      <?php for ($x = $sort_th['min_th']; $x <= $sort_th['max_th']; $x++) { ?>
                        <option <?php if ($_POST['angkatan'] == $x) {
                                  echo "selected";
                                } ?> value="<?php echo $x; ?>"><?php echo $x; ?></option>
                      <?php } ?>
                    </select>
                  </div>
                </div>

                <div class="col-md-3">
                  <div class="input-group">
                    <div class="input-group-addon">Kelas</div>
                    <select id="heard" class="form-control" required name="kelas" onChange="document.getElementById('form_id').submit();">
                      <option value="0">Semua Kelas</option>
                      <?php $sort_kls = mysqli_query($conn, "SELECT * FROM jenis_rt"); ?>
                      <?php while ($kls = mysqli_fetch_array($sort_kls)) { ?>
                        <option <?php if ($_POST['kelas'] == $kls['id']) {
                                  echo "selected";
                                } ?> value="<?php echo $kls['id']; ?>"><?php echo $kls['nama_jenis']; ?></option>
                      <?php } ?>
                    </select>
                  </div>
                </div>
              </form><br>

              <a href="component/com_santri_rt/export_santri.php?<?php echo $kelas_ex . $angkatan_ex; ?>" class='open_modal btn  btn-primary btn-xs'>Eksport</a>

              <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>Kelas</th>
                    <th>Tanggal Masuk</th>
                    <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                      <th width="110">Aksi</th>
                    <?php } ?>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                  $query = mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri != '' $kelas $angkatan ORDER BY nama");
                  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_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>
                      <?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=edit&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'] ?>   ');"><i class="glyphicon glyphicon-trash"></i> Hapus</a></td>
                      <?php } ?>
                    </tr>
                  <?php } ?>
                </tbody>
              </table>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>




<?php  } elseif ($_GET['aksi'] == 'edit') {
  $id = $_GET['id'];
  //$id = decrypt($idd);
  $query = mysqli_query($conn, "SELECT * FROM santri_rt 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">
            <div class="x_title" style="text-transform: capitalize;">
              <h2>Edit Data Santri Rumah Tahfidz</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=santri-rt&aksi=simpan_edit" enctype="multipart/form-data" method="POST">
                <div class="row">
                  <div class="col-md-12">
                    <?php include 'foto.php'; ?>
                  </div>
                  <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" value="<?php echo $r['nama']; ?>" />

                    <label for="nama">Tempat Lahir :</label>
                    <input type="text" class="form-control" name="tempat_lahir" 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" value="<?php echo $tgl; ?>" id="tanggal" name="tanggal_lahir">
                    </div>

                    <label>Kelas :</label>
                    <select id="heard" class="form-control" required name="jenis_rt">
                      <?php if ($r['jenis_rt'] == 0) { ?>
                        <option value="" selected>- Pilih Kelas -</option>
                      <?php
                      }
                      $query2  = "SELECT * FROM jenis_rt ORDER BY id";
                      $tampil2 = mysqli_query($conn, $query2);
                      while ($w = mysqli_fetch_array($tampil2)) {
                        if ($r['jenis_rt'] == $w['id']) {
                          echo "<option value=\"$w[id]\" selected>$w[nama_jenis]</option>";
                        } else {
                          echo "<option value=\"$w[id]\">$w[nama_jenis]</option>";
                        }
                      }
                      ?>
                    </select>

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

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

                  </div>
                  <div class="col-md-6">
                    <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" value="<?php echo $tgl1; ?>" id="tanggal2" name="tanggal_masuk">
                    </div>


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

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

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


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

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

                    <label for="nama">NO. HP :</label>
                    <input type="text" autocomplete="off" class="form-control" name="no_hp" value="<?php echo $r['no_hp']; ?>" />

                    <label style="color: darkred;">Biaya DAM(Dana Awal Masuk) :</label>
                    <input type="number" class="form-control" autocomplete="off" name="dam" value="<?php echo $r['biaya_dam']; ?>" required>

                    <label style="color: darkred;">Biaya SPP :</label>
                    <input type="number" class="form-control" autocomplete="off" name="spp" value="<?php echo $r['biaya_spp']; ?>" required>

                    <label style="color: darkred;">Biaya DUT(Daftar Ulang Tahunan) :</label>
                    <input type="number" class="form-control" autocomplete="off" name="dut" value="<?php echo $r['biaya_dut']; ?>" required>


                    <label for="nama">Ganti Foto : (kosongkan bila tdk diganti)</label>
                    <input type="file" name="gambar" id="gambar">

                  </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>
                  <button type="submit" name="upload" onclick="return confirm('Apakah yakin akan diedit?')" class="btn btn-success btn-sm">Simpan</button><br>
                </div><br><br>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  <script type="text/javascript">
    var uploadField = document.getElementById("gambar");
    uploadField.onchange = function() {
      if (this.files[0].size > 4000000) { // ini untuk ukuran 4mb, 1000000 untuk 1 MB.
        alert("Maaf. File Terlalu Besar ! Maksimal Upload 4 MB");
        this.value = "";
      };
    };
  </script>


<?php } elseif ($_GET['aksi'] == 'tambah_santri') {
  $cekid = 1;
  while ($cekid > 0) {
    $tahun = date('y');
    $bulan = date('m');
    $hari = date('d');
    $rand = rand(1, 99);
    $char = "rt";
    $newID = $char . sprintf($tahun) . sprintf($bulan) . sprintf($hari) . sprintf($rand);
    $cekid = mysqli_num_rows(mysqli_query($conn, "SELECT * FROM santri_rt WHERE id_santri='$newID'"));
  }

?>

  <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>TAMBAH DATA SANTRI RUMAH TAHFIDZ</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=santri-rt&aksi=simpan" enctype="multipart/form-data" method="POST">
                <div class="row">
                  <div class="col-md-6">
                    <label for="id">ID santri :</label>
                    <input type="text" class="form-control" disabled value="<?php echo $newID; ?>" />
                    <input type="hidden" class="form-control" name="id" value="<?php echo $newID; ?>" />

                    <label for="nama">Nama :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nama" value="" />

                    <label>Jenis Kelamin :</label>
                    <select id="heard" class="form-control" required name="jenkel">
                      <option value="" selected>- Jenis kelamin -</option>
                      <option value="l">Laki-laki</option>
                      <option value="p">Perempuan</option>

                    </select>

                    <label for="nama">Tempat Lahir :</label>
                    <input type="text" autocomplete="off" class="form-control" name="tempat_lahir" value="" />

                    <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="off" value="" id="tanggal" name="tanggal_lahir">
                    </div>

                    <label>Kelas :</label>
                    <select id="heard" class="form-control" required name="jenis_rt">
                      <?php if ($r['statuus'] == 0) { ?>
                        <option value="" selected>- Pilih Kelas -</option>
                      <?php
                      }
                      $query2  = "SELECT * FROM jenis_rt ORDER BY id";
                      $tampil2 = mysqli_query($conn, $query2);
                      while ($w = mysqli_fetch_array($tampil2)) {
                        echo "<option value=\"$w[id]\">$w[nama_jenis]</option>";
                      }
                      ?>
                    </select>

                    <!--data baru-->

                    <label for="nama">NIK :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nik_santri" value="" />

                    <label for="nama">NO. KK :</label>
                    <input type="text" autocomplete="off" class="form-control" name="no_kk" value="" />





                  </div>
                  <div class="col-md-6">

                    <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" autocomplete="off" class="form-control " autocomplete="off" value="" id="tanggal2" name="tanggal_masuk">
                    </div>

                    <label for="nama">FOTO :</label>
                    <input type="file" class="form-control" name="gambar" id="gambar" value="">

                    <label for="nama">Nama Ayah :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nama_ayah" value="">

                    <label for="nama">NIK Ayah :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nik_ayah" value="" />

                    <label for="nama">Nama Ibu :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nama_ibu" value="" />

                    <label for="nama">NIK Ibu :</label>
                    <input type="text" autocomplete="off" class="form-control" name="nik_ibu" value="" />

                    <label for="alamat">Alamat Lengkap :</label>
                    <textarea class="form-control" autocomplete="off" name="alamat" requerid> </textarea>

                    <label for="nama">No. HP :</label>
                    <input type="text" autocomplete="off" class="form-control" name="no_hp" value="" />

                    <label style="color: darkred;">Biaya DAM(Dana Awal Masuk) :</label>
                    <input type="number" class="form-control" autocomplete="off" name="dam" value="" required>

                    <label style="color: darkred;">Biaya SPP :</label>
                    <input type="number" class="form-control" autocomplete="off" name="spp" value="" required>

                    <label style="color: darkred;">Biaya DUT(Daftar Ulang Tahunan) :</label>
                    <input type="number" class="form-control" autocomplete="off" name="dut" value="" required>



                  </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>
                  <button type="submit" onclick="return confirm('Apakah yakin akan disimpan?')" class="btn btn-success btn-sm">Simpan</button><br>
                </div><br><br>
              </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

  <script type="text/javascript">
    var uploadField = document.getElementById("gambar");
    uploadField.onchange = function() {
      if (this.files[0].size > 4000000) { // ini untuk ukuran 4mb, 1000000 untuk 1 MB.
        alert("Maaf. File Terlalu Besar ! Maksimal Upload 4 MB");
        this.value = "";
      };
    };
  </script>
<?php

} elseif ($_GET['aksi'] == 'simpan') {
  $module = $_GET['module'];
  $tanggal = $_POST['tanggal_lahir'];
  $tgl = date('Y-m-d', strtotime($tanggal));
  $tanggal1 = $_POST['tanggal_masuk'];
  $tgl1 = date('Y-m-d', strtotime($tanggal1));
  $id = $_POST['id'];



  $save = mysqli_query($conn, "INSERT INTO santri_rt(id_santri,
                                  nama,
                                  alamat,
                                  tempat_lahir,
                                  tanggal_lahir,
                                  jenis_rt,
                                  tanggal_masuk,
                                  jenkel,
                                  nik_santri,
                                  no_kk,
                                  nama_ayah,
                                  nama_ibu,
                                  nik_ayah,
                                  nik_ibu,
                                  no_hp,
                                  biaya_spp,
                                  biaya_dam,
                                  biaya_dut) VALUES('$id',
                                                 '$_POST[nama]',
                                                 '$_POST[alamat]',
                                                 '$_POST[tempat_lahir]',
                                                 '$tgl',
                                                 '$_POST[jenis_rt]',
                                                 '$tgl1',
                                                 '$_POST[jenkel]',
                                                 '$_POST[nik_santri]',
                                                 '$_POST[no_kk]',
                                                 '$_POST[nama_ayah]',
                                                 '$_POST[nama_ibu]',
                                                 '$_POST[nik_ayah]',
                                                 '$_POST[nik_ibu]',
                                                 '$_POST[no_hp]',
                                                 '$_POST[spp]',
                                                 '$_POST[dam]',
                                                 '$_POST[dut]'
                                                 )");

  if (!$save) {
    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
  } else {

    //simpan gambar
    $cekfoto = $_FILES['gambar']['name'];
    if ($cekfoto != "") {
      include 'simpan_gambar.php';
    } else {
    }
    //akhir simpan gambar
    historydatasimpan($_SESSION['id'], $_SESSION['mac'], $id);
    echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
  }

  // echo $_POST['dut'] . $_POST['spp'] . $_POST['dam'];
} elseif ($_GET['aksi'] == 'simpan_edit') {
  $module = $_GET['module'];
  $tanggal = $_POST['tanggal_lahir'];
  $password   = md5($_POST['password']);
  $tgl = date('Y-m-d', strtotime($tanggal));
  $tgl1 = date('Y-m-d', strtotime($_POST['tanggal_masuk']));

  $query = mysqli_query($conn, "UPDATE santri_rt SET nama = '$_POST[nama]',
                                    alamat = '$_POST[alamat]',
                                    tempat_lahir = '$_POST[tempat_lahir]',
                                    tanggal_lahir = '$tgl',
                                    tanggal_masuk = '$tgl1',
                                    nama_ayah = '$_POST[nama_ayah]',
                                    nama_ibu = '$_POST[nama_ibu]',
                                    nik_ayah = '$_POST[nik_ayah]',
                                    nik_ibu = '$_POST[nik_ibu]',
                                    nik_santri = '$_POST[nik_santri]',
                                    no_kk = '$_POST[no_kk]',
                                    no_hp = '$_POST[no_hp]',
                                    biaya_dam = '$_POST[dam]',
                                    biaya_spp ='$_POST[spp]',
                                    biaya_dut = '$_POST[dut]',
                                    jenis_rt = '$_POST[jenis_rt]' 
                                    WHERE id_santri = '$_POST[id]'");
  $id = $_POST['id'];

  //CEK GANTI FOTO ATAU TIDAK
  $cekfoto = $_FILES['gambar']['name'];
  if ($cekfoto != "") {

    if ($query) {
      //hapus gambar
      $target = "images/$id.jpg";
      $target1 = "images/resize_$id.jpg";
      if (file_exists($target)) {
        unlink($target); //delete now
      }

      if (file_exists($target1)) {
        unlink($target1); //delete now
      }

      include 'simpan_gambar.php';
    }
  } else {
  }


  historydataedit($_SESSION['id'], $_SESSION['mac'], $id);
  echo "<script language='javascript'>document.location='?module=" . $module . "&aksi=edit&id=" . $id . "';</script>";
} elseif ($_GET['aksi'] == 'hapus') {
  $module = $_GET['module'];
  $id = $_GET['id'];
  //$id = decrypt($idd);
  if ($id == "") {
    echo "gagal hapus";
  } else {
    $query = mysqli_query($conn, "Delete FROM santri_rt WHERE id_santri='$id'");
    //$query=mysqli_query($conn,"Delete FROM transaksi WHERE id_santri='$id'");
    //$query=mysqli_query($conn,"Delete FROM status_lunas WHERE id_santri='$id'");

    //hapus gambar
    $target = "images/rt/$id.jpg";
    $target1 = "images/rt/resize_$id.jpg";
    if (file_exists($target)) {
      unlink($target); //delete now
    }

    if (file_exists($target1)) {
      unlink($target1); //delete now
    }

    if (!$query) {
      echo "GAGAL HAPUS!!";
    } else {
      historydatahapus($_SESSION['id'], $_SESSION['mac'], $id);
      echo "<script language='javascript'>document.location='?module=" . $module . "';</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 ?<br>Menghapus data santri akan menghapus data transaksi juga</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>