<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title></title>

  <script type="text/javascript">
/*  https://erroz.wordpress.com/2009/03/30/fungsi-pindah-baris/
    function nextfield(ID)
{
if (event.keyCode == 13)
document.getElementById(ID).focus();
}*/
</script>
<!-- Validasi Muncul popup -->

<script language='javascript'>
  /*https://senterwebs.com/artikel-415/cara-mudah-membuat-validasi-angka-lengkap.html*/
function validAngka(a)
{
    if(!/^[0-9.]+$/.test(a.value))
    {
    a.value = a.value.substring(0,a.value.length-1000);
    }
}
</script>

<!-- Content Header (Page header) -->
<div class="content-header ">
  <div class="container-fluid">
    <div class="row mb-2">
      <div class="col-sm-12">
        <!-- <h1 class="m-0 text-dark">Judul Atas</h1> -->
      </div><!-- /.col -->
    </div><!-- /.row -->
  </div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->

<!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <div class="col-12">
            <!-- Default box -->
            <div class="card">
              <div class="card-header bg-info">
                <h3 class="card-title ">Buat UserName</h3>
                <div class="card-tools">
                  <button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
                    <i class="fas fa-minus"></i>
                  </button>
                  <button type="button" class="btn btn-tool" data-card-widget="remove" title="Remove">
                    <i class="fas fa-times"></i>
                  </button>
                </div>
              </div>
              <!-- /.card-header -->
        <div class="card-body">

          <!-- Croping Photo -->

<!-- <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.min.css">
 -->  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.min.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/croppie/2.6.2/croppie.js"></script>

 <!-- 
 https://www-devopsschool-com.translate.goog/blog/image-upload-and-crop-using-php-and-jquery/?_x_tr_sl=en&_x_tr_tl=id&_x_tr_hl=id&_x_tr_pto=tc -->

<script language="javascript" type="text/javascript">
/*https://www.diskusiweb.com/discussion/31709/entry-pake-key-enter-kok-gak-berhasil*/
var fromEnter = false ;
function changeFocus (event,nextFocus)
{
if (event.keyCode==13)
{
var obj = document.getElementsByName( nextFocus )[ 0 ] ;
if (obj)
obj.focus() ;
}
fromEnter = true ;
}
function checkSubmit ()
{
fromEnter = !fromEnter ;
return fromEnter ;
}
</script>  
<?php
include 'menu/gantipassworduser.php';
?>      
                    <?php 
                    include 'koneksi.php';
                        $querynk = "SELECT count(kode) AS nokode  FROM user where kode='".$_SESSION['kode']."' ";
                        $hasilnk = mysqli_query($koneksi, $querynk);
                        $nokode = mysqli_fetch_assoc($hasilnk);                        
                       ?>

              
             
              <form action="" class="form-simpanusername"  id="form-simpanusername">
              <div class="row">
              <div class="col-sm-6"> 

              <div class="form-group row">
              <label for="nama" class="col-sm-3">Kode Kantor</label>
              <input type="text" name="kode" id="kode" value="<?php echo $_SESSION['kode']?>" class="form-control col-sm-8" readonly>
              <!-- <select id="kode" name="kode" class="form-control select2 col-sm-8" onKeyDown="changeFocus(event,'nama_kantor')" onchange="run()" autofocus/> -->
                     <?php
                     /*include 'koneksi.php';
                      
                        $query1 = "SELECT * FROM kantor order by kode";  */  
                        /*https://kodingbagus.blogspot.com/2019/08/cara-membuat-pencarian-di-selectoption.html*/
                        /*$hasil = mysqli_query($koneksi, $query1);
                        while ($row = mysqli_fetch_array($hasil)) {
                          $ket="";
                          if(isset($_POST['kode'])){
                            $desa=trim($_POST['kode']);
                            if($desa==$row['kode'])
                            {
                              $ket="selected";
                            }
                          }
                            ?>
                          
                            <option  <?php echo $ket;?> value="<?php echo $row['kode']; ?>"><?php echo $row['kode']; ?> (<?php echo $row['nama_kantor'].")"?></option>
                            <?php
                        }*/
                        ?>
                          <!-- https://kelasprogrammer.com/menampilkan-data-berdasarkan-pilihan-combo-box-di-php/ -->
                    <!-- </select> -->
                    </div> 

                    <div class="form-group row">
              <label for="nama" class="col-sm-3">Nama Kantor</label>
              <input type="text" name="nama_kantor" id="nama_kantor" value="<?php echo $_SESSION['kantor'] ?>"class="form-control col-sm-8" readonly>
              <!-- <select id="nama_kantor" name="nama_kantor" class="form-control select2 col-sm-8" onKeyDown="changeFocus(event,'nama')"> -->
                     <?php
                     
                      // ambil data dari database
                        /*$query1 = "SELECT * FROM kantor order by kode";    
                        $hasil = mysqli_query($koneksi, $query1);
                        while ($row = mysqli_fetch_array($hasil)) {
                          $ket="";
                          if(isset($_POST['nama_kantor'])){
                            $desa=trim($_POST['nama_kantor']);
                            if($desa==$row['nama_kantor'])
                            {
                              $ket="selected";
                            }
                          }*/
                            ?>
                          
                            <!-- <option  <A?php echo $ket;?> value="<A?php echo $row['nama_kantor']; ?>"><A?php echo $row['nama_kantor']; ?></option> -->
                            <?php
                        /*}*/
                        ?>
                          <!-- https://kelasprogrammer.com/menampilkan-data-berdasarkan-pilihan-combo-box-di-php/ -->
                    <!-- </select> -->
                    </div>

                <div class="form-group row">
                <label for="nama" class="col-sm-3">Nama</label>
                <input type="text" name="nama" id="nama" class="form-control col-sm-8" onkeypress='return harusHuruf(event)' placeholder="Tulis Nama Sesuai KTP"required="" onkeyup="this.value = this.value.toUpperCase()" onkeypress='return harusHuruf(event)' onKeyDown="changeFocus(event,'jabatan')">    
              </div>
              <script>
//     function run() {
//         document.getElementById("nama").value = document.getElementById("kode").value;
//         /*https://qastack.id/programming/1085801/get-selected-value-in-dropdown-list-using-javascript*/
//     }
// </script>
              <div class="form-group row">
                <label for="nama" class="col-sm-3">Jabatan</label>
                <div class="custom-control custom-radio col-sm-3 ">
                          <input class="custom-control-input " type="radio" id="customRadio1" name="jabatan" value="Kepala" onKeyDown="changeFocus(event,'username')" checked>
                          <label for="customRadio1" class="custom-control-label">Kepala </label>
                        </div>
                        <div class="custom-control custom-radio col-sm-3">
                          <input class="custom-control-input " type="radio" id="customRadio2" name="jabatan" value="Admin" onKeyDown="changeFocus(event,'username')" >
                          <label for="customRadio2" class="custom-control-label">Admin</label>
                        </div>
                        <div class="custom-control custom-radio col-sm-3">
                          <input class="custom-control-input " type="radio" id="customRadio3" name="jabatan" value="Kasir" onKeyDown="changeFocus(event,'username')" >
                          <label for="customRadio3" class="custom-control-label">Administrator</label>
                        </div>
                      </div>
            
          </div>
                        
          <div class="col-sm-6">
              

              <div class="form-group row">
                <label for="nama" class="col-sm-3">UserName</label>
                <input type="text" name="username" id="username" class="form-control col-sm-8"  placeholder="UserName yg anda inginkan" onKeyDown="changeFocus(event,'katasandi')">      
            </div>            

            <div class="form-group row">
                <label for="nama" class="col-sm-3">Kata Sandi</label>
                <input type="text" name="katasandi" id="katasandi" required="" class="form-control col-sm-8"  onKeyDown="changeFocus(event,'level')">      
            </div>

             <div class="form-group row">
                <label for="nama" class="col-sm-3">No UserName</label>
                <input type="text" name="nokode" id="nokode" class="form-control col-sm-8" readonly value="<?php echo $_SESSION['kode'].sprintf("%03d",$nokode['nokode']+1); ?>" onKeyDown="changeFocus(event,'level')">      
            </div> 
          
            <div class="form-group row">
                <label for="nama" class="col-sm-3">Level</label>
                <div class="custom-control custom-radio col-sm-3 ">
                          <input class="custom-control-input " type="radio" id="customRadio4" name="level" value="4" onKeyDown="changeFocus(event,'simpanusername')" checked>
                          <label for="customRadio4" class="custom-control-label">Admin </label>
                        </div>
                        <div class="custom-control custom-radio col-sm-3">
                          <input class="custom-control-input " type="radio" id="customRadio5" name="level" value="5" onKeyDown="changeFocus(event,'simpanusername')" >
                          <label for="customRadio5" class="custom-control-label">Kpl</label>
                        </div>
                        <div class="custom-control custom-radio col-sm-3">
                          <input class="custom-control-input " type="radio" id="customRadio6" name="level" value="1" onKeyDown="changeFocus(event,'simpanusername')" >
                          <label for="customRadio6" class="custom-control-label">Administrator</label>
                        </div>
                      </div>
  

                </div>
              </div>
        
              
            <!-- /.card-body -->
            <div class="card-footer ">
              <?php if($_SESSION['level']<4){ ?>
              <input type="button" name="simpanusername" id="simpanusername" 
              class="btn btn-primary" value="Simpan">
              <a href="" class="btn btn-danger">
                Batal
              </a>
              <a href="home.php?home=<?php echo md5("edit_userlogin");?>" class="btn btn-danger">
                Edit
              </a>
              <a href="" class="btn btn-danger">
                Hapus
              </a>
              <?php } ?>
              <a href="#" class="btn btn-primary" data-toggle="modal" data-target="#data_Modalgantipassworduser"><i class="fa fa-user-md"></i> Ganti</a>

            </div>
            
          </form>
        </div>
        <!-- /.card -->
      </div>
      <!--/.col (left) -->
    </div>
    <!-- /.row -->
  </div><!-- /.container-fluid -->
</section>
<!-- /.content -->

<!-- <script type="text/javascript" src="jquery.js"></script> -->
<script type="text/javascript">

 $(document).ready(function(){
    $("#simpanusername").click(function(){
      var data = $('#form-simpanusername').serialize();
      
      var kode= $('#kode').val();
      var nama_kantor= $('#nama_kantor').val();
      var nama= $('#nama').val();
      var jabatan= $('#jabatan').val();
      var username= $('#username').val();
      var katasandi= $('#katasandi').val();
      var level= $('#level').val();
      
  
      if(kode== "0"||nama_kantor== "0"|| nama=="0" ||jabatan=="0"||username=="0"||katasandi=="0"||level=="0"){ 
Swal.fire({
  icon: 'error',
  title: 'Hati-hati...',
  text: 'Data masih ada yg belum diisi!',
  footer: '<a href="">Anda Perlu teliti sedikit lagi?</a>'
});
/*https://sweetalert2.github.io/*/
       /* alert ("Data Masih ada yang kosong 1");*/
        //window.location.href='/rakerugt/home.php?page=cap_realita';
      
    } else if(kode== ""||nama_kantor== ""|| nama=="" ||username==""||katasandi==""||level==""){ 

      Swal.fire(
  'Data Masih ada yang Kosong !',
  'Mohon data dilengkapi dlu !',
  'question'
);
       
    }else{

      $.ajax({
        type: 'POST',
        url: 'simpan/simpan_userlogin.php',
        data: data,
        success: function(data) {
         console.log(data)
         let timerInterval
Swal.fire({
  title: 'Alhamdulillah..! Data Tersimpan.',
  html: 'Silahkan Cek <b></b> Sendiri.',
  timer: 3000,
  timerProgressBar: true,
  didOpen: () => {
    Swal.showLoading()
    const b = Swal.getHtmlContainer().querySelector('b')
    timerInterval = setInterval(() => {
      b.textContent = Swal.getTimerLeft()
    }, 100)
  },
  willClose: () => {
    clearInterval(timerInterval)
  }
}).then((result) => {
  /* Read more about handling dismissals below */
  if (result.dismiss === Swal.DismissReason.timer) {
    console.log('I was closed by the timer')
  }
})

         /*alert("Data Sukses Disimpan");
         window.location.href='home.php?home=registrasi'; */
        },
        error: function(error){
          console.log(`error ${error}`)
         
        }
      });
    }
    });
  });
  
</script>
<script type="text/javascript">
  function cekhuruf(a) {
valid = /^[A-Za-z]{1,}$/;
return valid.test(a);
}
</script>
<script>
  /*required onkeypress='return harusHuruf(event)' tinggal pasang ke input
  https://kursuswebdesign.org/cara-membuat-validasi-input-form-hanya-text-dengan-javascript/*/
function harusHuruf(evt){
        var charCode = (evt.which) ? evt.which : event.keyCode
        if ((charCode < 65 || charCode > 90)&&(charCode < 97 || charCode > 122)&&charCode>32)
            return false;
        return true;
}
</script>

<script>
    function hanyaAngka(evt) {
      var charCode = (evt.which) ? evt.which : event.keyCode
       if (charCode > 31 && (charCode < 48 || charCode > 57))
 
        return false;
      return true;
    }
  </script>


</head>
</html>