<?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 >Data <?php echo $_GET['module'];?> <small></small></h2>
                    <div class="clearfix"></div>
                  </div>
                  <?php if ($_SESSION['leveluser'] == 'admin'){ ?>
                  <a href="?module=santri&aksi=tambah_santri"  class="btn btn-success btn-sm" data-toggle="" data-target=""><i class="glyphicon glyphicon-plus"></i> Tambah</a>
                  <?php }?>
                  <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>
                          <?php if ($_SESSION['leveluser'] == 'admin'){ ?>
                          <th width="110">Aksi</th> 
                        <?php }?>
                        </tr>
                      </thead>
                      <tbody>
                      <?php 
                          $no = 0;
                          $query=mysqli_query($conn,"SELECT * FROM santri 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_status WHERE id_status='$row[statuus]' order by id_status asc");
                            while($data=mysqli_fetch_array($kueri)){
                              echo "$data[nama_status]";
                            } ?></td>
                           <?php if ($_SESSION['leveluser'] == 'admin'){ ?>
                          <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>


<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'];
  //$id = decrypt($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">
                  <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=santri&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>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 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">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'){
  
  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=santri&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>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>

                      <!--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>
                    
                    
                   
                      </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'];

 

  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,
                                  nik_ayah,
                                  nik_ibu) 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[nik_ayah]',
                                                 '$_POST[nik_ibu]'
                                                 )");

  //simpan gambar
 include 'simpan_gambar.php';
  //akhir simpan gambar



                                                 
                                                 

  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]));
     
      $query=mysqli_query($conn,"UPDATE santri 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]',
                                    statuus = '$_POST[status]' 
                                    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{

    

  }
 

     
  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 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/$id.jpg";
    $target1="images/resize_$id.jpg";
    if (file_exists($target)){
      unlink($target); //delete now
    }

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

  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>