<?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>Cetak Kartu Santri (HANYA SANTRI AKTIF)<small></small></h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">

              <!-- sorting data per tahun -->
              <form action="?module=<?php echo $_GET['module']; ?>" id="form_id" enctype="multipart/form-data" method="POST">

                <div class="col-md-3">
                  <div class="input-group">
                    <div class="input-group-addon">Kelas Umum Santri</div>

                    <select id="heard" class="form-control" required name="tampildata" onChange="document.getElementById('form_id').submit();">
                     
                        <option value="all">- Semua Kelas -</option>
                      <?php
                      
                      $query_kelas  = "SELECT * FROM kelas_umum ORDER BY id_kelas";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($_POST['tampildata'] == $kls['id_kelas']) {
                          echo "<option value=\"$kls[id_kelas]\" selected>$kls[jenjang_kelas]</option>";
                        } else {
                          echo "<option value=\"$kls[id_kelas]\">$kls[jenjang_kelas]</option>";
                        }
                      }
                      ?>
                    </select>

                    


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

              <?php
              if (!isset($_POST['tampildata']) or $_POST['tampildata'] == 0) {
                //bukan data sorting
                $th = "";
              } else {
                $th = "?tampildata=$_POST[tampildata]";
              }
              ?>

                  <!--akhir sorting data per tahun -->

              <form action="kartu/cetak_kartu.php" method="POST" target="_blank">
              <div class="col-md-3">
              <button onclick="submit" class='open_modal btn  btn-danger btn-xl'>Cetak</button>
              </div>
<?php
              //sort data
                  if (!isset($_POST['tampildata']) OR $_POST['tampildata']=='') {
                    //bukan data sorting
                    $query = mysqli_query($conn, "SELECT * FROM santri WHERE status_aktif='1' ORDER BY nama");
                    $th = "";
                  } elseif ($_POST['tampildata'] == 'all'){
                    $query = mysqli_query($conn, "SELECT * FROM santri WHERE status_aktif='1' ORDER BY nama");
                    $th = "";
                  }else{
                    $query = mysqli_query($conn, "SELECT * FROM santri WHERE status_aktif='1' AND kelas_sekarang=$_POST[tampildata] ORDER BY nama");
                    $th = "?th=$_POST[tampildata]";
                  }
                  //akhir sort data
?>

              <table id="datatable" class="table table-striped table-bordered">
                <thead>
                  <tr>
                    <th>No</th>
                    <th width="15px">Pilih <input type="checkbox" onchange="checkAll(this)" name="chk[]"></th>
                    <th>ID santri</th>
                    <th>ID Junio</th>
                    <th>Nama</th>
                    <th>Kelas Umum</th>
                    <th>Jenkel</th>
                    <th>Foto</th>
                    <th>Alamat</th>
                    <th>Tanggal lahir</th>
                    <th>WA wali <i class='glyphicon glyphicon-envelope'></i></th>
                    <th>Status</th>
                    <th>Status Aktif</th>
                    <th>Tahun masuk</th>
                    <?php if ($_SESSION['leveluser'] == 'admin' or $_SESSION['leveluser'] == 'user1') { ?>
                      <th width="110">Aksi</th>
                    <?php } ?>
                  </tr>
                </thead>
                <tbody>
                  <?php
                  $no = 0;
                                   
                  while ($row = mysqli_fetch_array($query)) {
                    $no++;
                  ?>
                    <tr>
                      <td><?php echo $no; ?></td>
                      <td><input type="checkbox" name="selector[]" value="<?php echo $row['id_santri']; ?>" </td>
                      <td><?php echo $row['id_santri']; ?></td>
                      <td><?php echo $row['id_junio']; ?></td>
                      <!--<td><?php //echo $row['no_rekening'];
                              ?></td>-->
                      <td><?php echo $row['nama']; ?></td>
                      <td><?php echo namakelas($row['kelas_sekarang']); ?></td>
                      <td> <?php

                            if ($row['jenkel'] == "l") { ?>
                          <div style="font-weight:bold;">Laki-Laki</div>

                        <?php } else { ?>
                          <div style="font-weight:bold;">Perempuan</div>
                        <?php } ?>
                      </td>
                      <td>

                        <!-- foto -->
                        <?php
                        $foto = "images/" . $row['id_santri'] . ".jpg";
                        if (file_exists($foto)) { ?>
                          <div style="color:blue; font-weight:bold;">Ada</div>

                        <?php } else { ?>
                          <div style="color:crimson; font-weight:bold;">Belum</div>
                        <?php } ?>


                      </td>
                      <td><?php echo $row['alamat']; ?></td>
                      <td><?php echo $row['tanggal_lahir']; ?></td>
                      <?php $wa=substr_replace("$row[no_hp]","+62",0,1); ?>
                      <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><?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><?php $kueri  = mysqli_query($conn, "SELECT * FROM status_aktif WHERE id='$row[status_aktif]' order by id asc");
                          while ($data = mysqli_fetch_array($kueri)) {
                            echo "$data[nama]";
                          } ?></td>
                      <td><?php echo date('Y', strtotime($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>
                      </form>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>


  <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</h4>
        </div>
        <div class="modal-body">
          <form action="?module=<?php echo $_GET['module']; ?>&aksi=simpan" enctype="multipart/form-data" method="POST">
            <?php
            $query = "SELECT max(id_santri) as maxID FROM santri ";
            $hasil = mysqli_query($conn, $query);
            $data = @mysqli_fetch_array($hasil);
            $idMax = $data['maxID'];



            $noUrut = (int) substr($idMax, 1, 9);
            $noUrut++;
            $tes = 3;
            $tahun = date('y');
            $bulan = date('m');
            $rand = rand(1, 9999);
            $char = "N";
            $newID = sprintf($tahun) . sprintf($bulan) . sprintf("%04s", $rand);



            $query_2 = "SELECT max(no_rekening) as maxREK FROM santri ";
            $hasil_2 = mysqli_query($conn, $query_2);
            $data_2 = @mysqli_fetch_array($hasil_2);
            $idMax_2 = $data_2['maxREK'];

            $noUrut_2 = (int) substr($idMax_2, 6, 9);
            $noUrut_2++;
            $char_2 = $tahun;
            $newID_2 = $char_2 . sprintf("%06s", $noUrut_2);
            ?>
            <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 f>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="off" value="" id="tanggal2" name="tanggal_masuk" required>
            </div>

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

            <label for="telephone">Alamat * :</label>
            <textarea class="form-control" name="alamat" autocomplete="off" required> </textarea>

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

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

            <label>Nama Orang Tua :</label>
            <input type="text" class="form-control" autocomplete="off" name="orang_tua" required>

            <label>Status *:</label>
            <select id="heard" class="form-control" required name="status">
              <option value="" selected>- Pilih status -</option>
              <?php
              $query  = mysqli_query($conn, "SELECT * FROM jenis_status order by id_status asc");
              while ($r = mysqli_fetch_array($query)) {
                echo "<option value=\"$r[id_status]\">$r[nama_status]</option>";
              }
              ?>
            </select>




        </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>

<?php  } elseif ($_GET['aksi'] == 'edit') {
  $id = $_GET['id'];
  //editdatasantri
  $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']));
  $tgl_keluar = date('d-m-Y', strtotime($r['tgl_keluar']));
  
?>

  <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 <?php echo $_GET['module']; ?></h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=<?php echo $_GET['module']; ?>&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">ID Junio Smart :</label>
                    <input type="number" class="form-control" name="id_junio" value="<?php echo $r['id_junio']; ?>">

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

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

                    <label>Kelas Sekarang :</label>
                    <select id="heard" class="form-control" required name="kelas_sekarang">
                      <?php if ($r['kelas_sekarang'] == 0) { ?>
                        <option value="" selected>- Pilih Kelas -</option>
                      <?php
                      }
                      $query_kelas  = "SELECT * FROM kelas_umum ORDER BY id_kelas";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($r['kelas_sekarang'] == $kls['id_kelas']) {
                          echo "<option value=\"$kls[id_kelas]\" selected>$kls[jenjang_kelas]</option>";
                        } else {
                          echo "<option value=\"$kls[id_kelas]\">$kls[jenjang_kelas]</option>";
                        }
                      }
                      ?>
                    </select>

                    <label>Jenis Kelamin :</label>
                    <select id="heard" class="form-control" required name="jenkel">
                      <option value="">- Jenis kelamin -</option>
                      <option value="l" <?php if ($r['jenkel'] == "l") {
                                          echo "selected";
                                        } ?>>Laki-laki</option>
                      <option value="p" <?php if ($r['jenkel'] == "p") {
                                          echo "selected";
                                        } ?>>Perempuan</option>

                    </select>

                    <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>Status :</label>
                    <select id="heard" class="form-control" 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>Status Aktif Santri :</label>
                    <select id="heard" class="form-control" required name="status_aktif">
                      <?php if ($r['status_aktif'] == '') { ?>
                        <option value="" selected>- Pilih Status -</option>
                      <?php
                      }
                      $query2  = "SELECT * FROM status_aktif ORDER BY id";
                      $tampil2 = mysqli_query($conn, $query2);
                      while ($w = mysqli_fetch_array($tampil2)) {
                        if ($r['status_aktif'] == $w['id']) {
                          echo "<option value=\"$w[id]\" selected>$w[nama]</option>";
                        } else {
                          echo "<option value=\"$w[id]\">$w[nama]</option>";
                        }
                      }
                      ?>
                    </select>

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

                  <label for="nama">Tanggal Keluar :</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_keluar; ?>" id="tanggal4" name="tgl_keluar">
                    </div>

                    <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']; ?>">

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

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

                  </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> Baru / Pindahan :</label>
                    <select id="heard" class="form-control" required name="baruataupindahan">
                      <?php if ($r['baruataupindahan'] == 0) { ?>
                        <option value="" selected>- baruataupindahan -</option>
                      <?php
                      }
                      $query_kelas  = "SELECT * FROM baruataupindahan ORDER BY id";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($r['baruataupindahan'] == $kls['id']) {
                          echo "<option value=\"$kls[id]\" selected>$kls[baruataupindahan]</option>";
                        } else {
                          echo "<option value=\"$kls[id]\">$kls[baruataupindahan]</option>";
                        }
                      }
                      ?>
                    </select>

                    <label>Jenjang Kelas Awal Masuk :</label>
                    <select id="heard" class="form-control" required name="kelasawalmasuk">
                      <?php if ($r['kelasawalmasuk'] == 0) { ?>
                        <option value="" selected>- Pilih Kelas -</option>
                      <?php
                      }
                      $query_kelas  = "SELECT * FROM kelas_umum ORDER BY id_kelas";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($r['kelasawalmasuk'] == $kls['id_kelas']) {
                          echo "<option value=\"$kls[id_kelas]\" selected>$kls[jenjang_kelas]</option>";
                        } else {
                          echo "<option value=\"$kls[id_kelas]\">$kls[jenjang_kelas]</option>";
                        }
                      }
                      ?>
                    </select>


                    <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="alamat">Catatan Khusus :</label>
                    <textarea class="form-control" name="cttn_khusus" requerid><?php echo $r['cttn_khusus']; ?> </textarea>
                    
                    <label for="nama">Ganti Foto : (kosongkan bila tdk diganti)</label>
                    <input type="file" name="gambar" id="gambar">



                  </div>
                </div>
                <!-- kamera webcam -->
                <div class="ln_solid"></div>
                <div class="row">
                  <label class="col-md-12" for="nama">FOTO WEBCAM :</label>
                  <button class="class=form-control btn btn-flat bg-green" id="onoff" type=button onClick="ambilgambar()"><i class="glyphicon glyphicon-camera"></i></button>
                </div>
                <div id="kamera" class="row">
                  <div class="col-md-6 col-xs-12">
                    <div class="panel panel-primary">
                      <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-camera"></i> Camera Webcam</h3>
                      </div>
                      <div class="panel-body">

                        <center>
                          <div id="my_camera"></div>
                          <br><button class="class=form-control btn btn-flat bg-success" id="ganti" value="1" type=button onClick="switchkamera()"><i class="glyphicon glyphicon-refresh"></i></button>
                          <br><button class="class='form-control btn btn-flat bg-orange" type=button onClick="take_snapshot()"><i class="glyphicon glyphicon-camera"></i> Ambil Gambar</button>
                          <input type="hidden" name="webcam" class="image-tag">
                        </center>

                      </div>
                    </div>
                  </div>
                  <div class="col-md-6 col-xs-12">
                    <div class="panel panel-danger">
                      <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-camera"></i> Hasil Camera</h3>
                      </div>
                      <div class="panel-body">

                        <center>
                          <div id="results" class="img-thumbnail"><i class="glyphicon glyphicon-camera"></i>
                        </center>


                      </div>
                    </div>
                  </div>
                </div>
                <!-- akhir kamera webcam -->
                <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>
                <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'] == 'tambah_santri') {

  include 'acak_id.php';
  while ($cekid >= 1) {
    include 'acak_id.php';
  }

?>

  <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</h2>
              <div class="clearfix"></div>
            </div>
            <div class="x_content">
              <form action="?module=<?php echo $_GET['module']; ?>&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">NISN :</label>
                    <input type="number" autocomplete="off" class="form-control" name="nisn" value="">

                    <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>Status :</label>
                    <select id="heard" class="form-control" required name="status">
                      <?php if ($r['statuus'] == 0) { ?>
                        <option value="" 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>Status Aktif Santri :</label>
                    <select id="heard" class="form-control" required name="status_aktif">
                      <?php if ($r['status_aktif'] == '') { ?>
                        <option value="" selected>- Pilih Status -</option>
                      <?php
                      }
                      $query2  = "SELECT * FROM status_aktif ORDER BY id";
                      $tampil2 = mysqli_query($conn, $query2);
                      while ($w = mysqli_fetch_array($tampil2)) {
                        if ($r['status_aktif'] == $w['id']) {
                          echo "<option value=\"$w[id]\" selected>$w[nama]</option>";
                        } else {
                          echo "<option value=\"$w[id]\">$w[nama]</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="">

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

                    <label for="alamat">Riwayat Sakit :</label>
                    <textarea class="form-control" autocomplete="off" name="riwayat_sakit" requerid> </textarea>



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

                    <label for="nama">Tanggal Masuk : (*Wajib diisi sesuai 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" required>
                    </div>

                    <label> Baru / Pindahan :</label>
                    <select id="heard" class="form-control" required name="baruataupindahan">
                      <?php if ($r['baruataupindahan'] == 0) { ?>
                        <option value="" selected>- baruataupindahan -</option>
                      <?php
                      }
                      $query_kelas  = "SELECT * FROM baruataupindahan ORDER BY id";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($r['baruataupindahan'] == $kls['id']) {
                          echo "<option value=\"$kls[id]\" selected>$kls[baruataupindahan]</option>";
                        } else {
                          echo "<option value=\"$kls[id]\">$kls[baruataupindahan]</option>";
                        }
                      }
                      ?>
                    </select>

                    <label>Jenjang Kelas Awal Masuk :</label>
                    <select id="heard" class="form-control" required name="kelasawalmasuk">
                      <?php if ($r['kelasawalmasuk'] == '') { ?>
                        <option value="" selected>- Pilih Kelas -</option>
                      <?php
                      }
                      $query_kelas  = "SELECT * FROM kelas_umum ORDER BY id_kelas";
                      $tampil_kelas = mysqli_query($conn, $query_kelas);
                      while ($kls = mysqli_fetch_array($tampil_kelas)) {
                        if ($r['kelasawalmasuk'] == $w['id_kelas']) {
                          echo "<option value=\"$kls[id_kelas]\" selected>$kls[jenjang_kelas]</option>";
                        } else {
                          echo "<option value=\"$kls[id_kelas]\">$kls[jenjang_kelas]</option>";
                        }
                      }
                      ?>
                    </select>

                    <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" required>

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

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

                    <label for="alamat">Catatan Khusus :</label>
                    <textarea class="form-control" autocomplete="off" name="cttn_khusus" requerid> </textarea>



                  </div>
                </div>
                <!-- kamera webcam -->
                <div class="ln_solid"></div>
                <div class="row">
                  <label class="col-md-12" for="nama">FOTO WEBCAM :</label>
                  <button class="class=form-control btn btn-flat bg-green" id="onoff" type=button onClick="ambilgambar()"><i class="glyphicon glyphicon-camera"></i></button>
                </div>
                <div id="kamera" class="row">
                  <div class="col-md-6 col-xs-12">
                    <div class="panel panel-primary">
                      <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-camera"></i> Camera Webcam</h3>
                      </div>
                      <div class="panel-body">

                        <center>
                          <div id="my_camera"></div>
                          <br><button class="class=form-control btn btn-flat bg-success" id="ganti" value="1" type=button onClick="switchkamera()"><i class="glyphicon glyphicon-refresh"></i></button>
                          <br><button class="class='form-control btn btn-flat bg-orange" type=button onClick="take_snapshot()"><i class="glyphicon glyphicon-camera"></i> Ambil Gambar</button>
                          <input type="hidden" name="webcam" class="image-tag">
                        </center>

                      </div>
                    </div>
                  </div>
                  <div class="col-md-6 col-xs-12">
                    <div class="panel panel-danger">
                      <div class="panel-heading">
                        <h3 class="panel-title"><i class="fa fa-camera"></i> Hasil Camera</h3>
                      </div>
                      <div class="panel-body">

                        <center>
                          <div id="results" class="img-thumbnail"><i class="glyphicon glyphicon-camera"></i>
                        </center>


                      </div>
                    </div>
                  </div>
                </div>
                <!-- akhir kamera webcam -->
                <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(id_santri,
                                  nama,
                                  alamat,
                                  tempat_lahir,
                                  tanggal_lahir,
                                  
                                  statuus,
                                  tanggal_masuk,
                                  jenkel,
                                  nik_santri,
                                  no_kk,
                                  nama_ayah,
                                  nama_ibu,
                                  no_hp,
                                  nik_ayah,
                                  nik_ibu,biaya_dam,biaya_spp,biaya_dut,kelasawalmasuk,
                                  cttn_khusus,nisn,prog_kelas,baruataupindahan,status_aktif,riwayat_sakit,kelas_sekarang) VALUES('$id',
                                                 '$_POST[nama]',
                                                 '$_POST[alamat]',
                                                 '$_POST[tempat_lahir]',
                                                 '$tgl',
                                                 '$_POST[status]',
                                                 '$tgl1',
                                                 '$_POST[jenkel]',
                                                 '$_POST[nik_santri]',
                                                 '$_POST[no_kk]',
                                                 '$_POST[nama_ayah]',
                                                 '$_POST[nama_ibu]',
                                                 '$_POST[no_hp]',
                                                 '$_POST[nik_ayah]',
                                                 '$_POST[nik_ibu]',
                                                 '$_POST[dam]',
                                                 '$_POST[spp]',
                                                 '$_POST[dut]',
                                                 '$_POST[kelasawalmasuk]',
                                                 '$_POST[cttn_khusus]','$_POST[nisn]','$_POST[prog_kelas]','$_POST[baruataupindahan]','$_POST[status_aktif]',
                                                 '$_POST[riwayat_sakit]','$_POST[kelasawalmasuk]'
                                                   )");

  if ($save) {

    // Star Webcam
    $img = $_POST['webcam'];
    // End Webcam

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

      include 'simpan_gambar.php';
    } elseif ($img != "") {

      // Star Webcam
      $folderPath = "images/";
      $image_parts = explode(";base64,", $img);
      $image_type_aux = explode("image/", $image_parts[0]);
      $image_type = $image_type_aux[1];
      $image_base64 = base64_decode($image_parts[1]);
      $fileNama = $id . '.jpg';
      $file = $folderPath . $fileNama;
      file_put_contents($file, $image_base64);
      // End Webcam
      //copy hasil webcam untuk diresize small images
      $im_php = imagecreatefromjpeg('images/' . $id . '.jpg');
      $im_php = imagescale($im_php, 800);
      $im_php_inv = imagescale($im_php, 800);

      $im_width = imagesx($im_php);
      $im_height = imagesy($im_php);

      // imagefilter($im_php_inv, IMG_FILTER_NEGATE);
      imagecopy($im_php, $im_php_inv, $im_width / 2, 0, $im_width / 2, 0, $im_width / 2, $im_height);

      $new_name = 'images/resize_' . $id . '.jpg';
      imagejpeg($im_php, $new_name);
      //end copy images
    }

    //qrcode
    include "phpqrcode/qrlib.php";
    $tempdir = "images/qr/";
    if (!file_exists($tempdir))
      mkdir($tempdir);


    //isi QRCode saat discan
    $isi_teks = $id;
    //direktori dan nama logo
    // $logopath = 'dk.png';
    //namafile setelah jadi qrcode
    $namafile = $id . ".png";
    //kualitas dan ukuran qrcode
    $quality = "H";
    $ukuran = 8;
    $padding = 0;

    QRCode::png($isi_teks, $tempdir . $namafile, QR_ECLEVEL_H, $ukuran, $padding);
    $filepath = $tempdir . $namafile;
    $QR = imagecreatefrompng($filepath);



    imagepng($QR, $filepath);

    historydatasimpan($_SESSION['id'], $_SESSION['mac'], $id);
    echo "<script>ySimpan('?module=$module');</script>";
  } else {
    echo "<script>nSimpan('?module=$module');</script>";
  }







  // echo "<script language='javascript'>document.location='?module=" . $module . "';</script>";
} 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']));
  $tgl_keluar = date('Y-m-d', strtotime($_POST['tgl_keluar']));

  $query = mysqli_query($conn, "UPDATE santri SET id_junio = '$_POST[id_junio]', nama = '$_POST[nama]',
                                    alamat = '$_POST[alamat]',
                                    tempat_lahir = '$_POST[tempat_lahir]',
                                    tanggal_lahir = '$tgl',
                                    tanggal_masuk = '$tgl1',
                                    jenkel = '$_POST[jenkel]',
                                    nama_ayah = '$_POST[nama_ayah]',
                                    nama_ibu = '$_POST[nama_ibu]',
                                    no_hp = '$_POST[no_hp]',
                                    nik_ayah = '$_POST[nik_ayah]',
                                    nik_ibu = '$_POST[nik_ibu]',
                                    nik_santri = '$_POST[nik_santri]',
                                    no_kk = '$_POST[no_kk]',
                                    statuus = '$_POST[status]',
                                    biaya_dam = '$_POST[dam]',
                                    biaya_spp ='$_POST[spp]',
                                    biaya_dut = '$_POST[dut]',
                                    kelasawalmasuk ='$_POST[kelasawalmasuk]',
                                    status_aktif ='$_POST[status_aktif]',
                                    tgl_keluar ='$tgl_keluar',
                                    cttn_khusus ='$_POST[cttn_khusus]',
                                    nisn ='$_POST[nisn]',
                                    prog_kelas ='$_POST[prog_kelas]',
                                    baruataupindahan ='$_POST[baruataupindahan]',
                                    alasankeluar ='$_POST[alasankeluar]',
                                    riwayat_sakit ='$_POST[riwayat_sakit]',
                                    kelas_sekarang ='$_POST[kelas_sekarang]'
                                    WHERE id_santri = '$_POST[id]'");
  if ($query) {
    $id = $_POST['id'];

    // Star Webcam
    $img = $_POST['webcam'];
    // End Webcam

    //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';
      }
    } elseif ($img != "") {

      // Star Webcam
      $folderPath = "images/";
      $image_parts = explode(";base64,", $img);
      $image_type_aux = explode("image/", $image_parts[0]);
      $image_type = $image_type_aux[1];
      $image_base64 = base64_decode($image_parts[1]);
      $fileNama = $id . '.jpg';
      $file = $folderPath . $fileNama;
      file_put_contents($file, $image_base64);
      // End Webcam
      //copy hasil webcam untuk diresize small images
      $im_php = imagecreatefromjpeg('images/' . $id . '.jpg');
      $im_php = imagescale($im_php, 800);
      $im_php_inv = imagescale($im_php, 800);

      $im_width = imagesx($im_php);
      $im_height = imagesy($im_php);

      // imagefilter($im_php_inv, IMG_FILTER_NEGATE);
      imagecopy($im_php, $im_php_inv, $im_width / 2, 0, $im_width / 2, 0, $im_width / 2, $im_height);

      $new_name = 'images/resize_' . $id . '.jpg';
      imagejpeg($im_php, $new_name);
      //end copy images

    } else {
    }
    historydataedit($_SESSION['id'], $_SESSION['mac'], $id);
    echo "<script>ySimpan('?module=$module');</script>";
  } else {
    echo "<script>nSimpan('?module=$module');</script>";
  }


  // 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 "<script>nHapus('?module=$module');</script>";
  } else {
    $query = mysqli_query($conn, "Delete FROM santri WHERE id_santri='$id'");

    // $query = mysqli_query($conn, "Delete FROM status_lunas WHERE id_santri='$id'");


    if (!$query) {
      echo "<script>nHapus('?module=$module');</script>";
    } else {
      //hapus gambar
      $target = "images/$id.jpg";
      $target1 = "images/resize_$id.jpg";
      $target2 = "images/qr/$id.pg";
      if (file_exists($target)) {
        unlink($target); //delete now
      }

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

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

      //ganti keterangan transaksi jikasantri dihapus 
      $log = "SELECT * FROM transaksi WHERE id_santri='$_GET[id]' ";
      $hasil = mysqli_query($conn, $log);
      while ($data = mysqli_fetch_array($hasil)) {

        $logedit = $data['keterangan'] . " #Data santri dihapus " . "[" . $_SESSION['id'] . "~" . date('Y-m-d') . "]#";

        $save = mysqli_query($conn, "UPDATE transaksi SET 
      keterangan='$logedit'
      WHERE id_transaksi = '$data[id_transaksi]'") or die("GAGAL");
      }

      historydatahapus($_SESSION['id'], $_SESSION['mac'], $id);
      echo "<script>yHapus('?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</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>

<script src="build/webcam/java.js"></script>
<script src="build/webcam/webcam.min.js"></script>
<script language="JavaScript">
  document.getElementById("kamera").style.display = "none";

  function ambilgambar() {
    //aktif/nonaktif kamera
    var x = document.getElementById("kamera");
    if (x.style.display === "none") {
      x.style.display = "block";
      document.getElementById("onoff").innnerHTML =
        "ON";
      kamerabelakang();



    } else {
      x.style.display = "none";
      Webcam.reset();
    }
  }

  function kamerabelakang() {
    Webcam.reset();
    Webcam.set({
      // live preview size
      width: 230,
      height: 300,

      // device capture size
      dest_width: 460,
      dest_height: 600,

      // final cropped size
      constraints: {
        video: true,
        facingMode: "environment"
      },


      // format and quality
      image_format: 'jpeg',
      jpeg_quality: 100
    });

    Webcam.attach('#my_camera');
  }

  function kameradepan() {
    Webcam.reset();
    Webcam.set({
      // live preview size
      width: 230,
      height: 300,

      // device capture size
      dest_width: 460,
      dest_height: 600,

      // final cropped size
      constraints: {
        video: true

      },


      // format and quality
      image_format: 'jpeg',
      jpeg_quality: 100
    });

    Webcam.attach('#my_camera');
  }

  function switchkamera() {
    var x = document.getElementById("ganti");
    if (x.value == 1) {
      kameradepan();
      x.value = 0;
    } else {
      kamerabelakang();
      x.value = 1;
    }
  }


  // ambil gambar kamera
  function take_snapshot() {
    Webcam.snap(function(data_uri) {
      $(".image-tag").val(data_uri);
      document.getElementById('results').innerHTML = '<img width="230" height="300" src="' + data_uri + '">';
    });
  }
</script>


<script>
function checkAll(source) {
  checkboxes = document.querySelectorAll('input[name="selector[]"]');
  for (var i = 0; i < checkboxes.length; i++) {
    checkboxes[i].checked = source.checked;
  }
}
</script>