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


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

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

              <form action="" class="form-simpanalamat"  id="form-simpanalamat">
              <div class="row">
              <div class="col-sm-6">
                          
              <div class="form-group row">
                <label for="nama" class="col-sm-3">Alamat</label>
                <input type="text" name="k_dusun" id="k_dusun" minlength="3" maxlength="3" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'dusun')" autofocus/>
                <input type="text" name="dusun" id="dusun" class="form-control col-sm-6" onkeypress='return harusHuruf(event)' onkeyup="this.value = this.value.toUpperCase()"  onKeyDown="changeFocus(event,'k_rtrw')" > <!-- readonly -->
              </div>

              <div class="form-group row">
                <label for="nama" class="col-sm-3">RT/RW</label>
                <input type="text" name="k_rtrw" id="k_rtrw" minlength="4" maxlength="4" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'rtrw')">
                <input type="text" name="rtrw" id="rtrw" class="form-control col-sm-6" onkeyup="this.value = this.value.toUpperCase()" placeholder="Ketik RT RW" required="" onKeyDown="changeFocus(event,'k_desa')"/>
              </div>


               <div class="form-group row">
                <label for="nama" class="col-sm-3">Desa</label>
                <input type="text" name="k_desa" id="k_desa" minlength="4" maxlength="4" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'desa')">
                <input type="text" name="desa" id="desa" class="form-control col-sm-6" placeholder="Nama Desa" onkeypress='return harusHuruf(event)' onkeyup="this.value = this.value.toUpperCase()" onKeyDown="changeFocus(event,'k_kec')">      
            </div>
           
          </div>
                      

            <div class="col-sm-6">

              <div class="form-group row">
                <label for="nama" class="col-sm-3">Kecamatan</label>
                <input type="text" name="k_kec" id="k_kec" minlength="2" maxlength="2" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'kec')">
                <input type="text" name="kec" id="kec" class="form-control col-sm-6" placeholder="Nama Kecamatan" onkeypress='return harusHuruf(event)' onkeyup="this.value = this.value.toUpperCase()" onKeyDown="changeFocus(event,'k_kab')">      
            </div>
            <div class="form-group row">
                <label for="nama" class="col-sm-3">Kabupaten</label>
                <input type="text" name="k_kab" id="k_kab" minlength="2" maxlength="2" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'kab')">
                <input type="text" name="kab" id="kab" class="form-control col-sm-6" placeholder="Nama Kabupaten" onkeypress='return harusHuruf(event)' onkeyup="this.value = this.value.toUpperCase()" onKeyDown="changeFocus(event,'k_prov')">      
            </div>
            <div class="form-group row">
                <label for="nama" class="col-sm-3">Provinsi</label>
                <input type="text" name="k_prov" id="k_prov" minlength="2" maxlength="2" class="form-control col-sm-2" placeholder="Kode" onkeyup="validAngka(this)" onKeyDown="changeFocus(event,'prov')">
                <input type="text" name="prov" id="prov" class="form-control col-sm-6" placeholder="Nama Provinsi" onkeypress='return harusHuruf(event)' onkeyup="this.value = this.value.toUpperCase()" onKeyDown="changeFocus(event,'simpanalamat')">
            </div>

        </div>
      </div>
    
            <!-- /.card-body -->
            <div class="card-footer ">
              <input type="button" name="simpanalamat" id="simpanalamat" 
              class="btn btn-primary" value="Simpan">
              <a href="" class="btn btn-danger">
                Batal
              </a>
              <a href="home.php?home=<?php echo md5("edit_buatalamat");?>" class="btn btn-danger">
                Edit
              </a>
               <a href="home.php?home=<?php echo md5("lap_alamat");?>" class="btn btn-info">
                Lihat
              </a>
              <a href="" class="btn btn-danger">
                Hapus
              </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(){
    $("#simpanalamat").click(function(){
      var data = $('#form-simpanalamat').serialize();
      var k_dusun= $('#k_dusun').val();
      var dusun= $('#dusun').val();
      var k_rtrw= $('#k_rtrw').val();
      var rtrw= $('#rtrw').val();
      var k_desa= $('#k_desa').val();
      var desa= $('#desa').val();
      var k_kec= $('#k_kec').val();
      var kec= $('#kec').val();
      var k_kab= $('#k_kab').val();
      var kab= $('#kab').val();
      var k_prov= $('#k_prov').val();
      var prov= $('#prov').val();
      
 
      if(k_dusun== "0"||dusun=="0" ||k_rtrw=="0"||rtrw=="0"||k_desa=="0"||desa=="0"||k_kec=="0"||kec=="0"||k_kab=="0"||k_prov=="0"||prov=="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(k_dusun== ""||dusun=="" ||k_rtrw==""||rtrw==""||k_desa==""||desa==""||k_kec==""||kec==""||k_kab==""||k_prov==""||prov==""){ 

      Swal.fire(
  'Data Masih ada yang Kosong !',
  'Mohon data dilengkapi dlu !',
  'question'
);
        /*alert ("Data Masih ada yang kosong 2");*/
       
    }else{

      $.ajax({
        type: 'POST',
        url: 'simpan/simpan_alamat.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>

<script type="text/javascript">
  function isValidDate(dateString)
{
    // First check for the pattern
    var regex_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/;

    if(!regex_date.test(dateString))
    {
        return false;
    }

    // Parse the date parts to integers
    var parts   = dateString.split("-");
    var day     = parseInt(parts[2], 10);
    var month   = parseInt(parts[1], 10);
    var year    = parseInt(parts[0], 10);

    // Check the ranges of month and year
    if(year < 1000 || year > 3000 || month == 0 || month > 12)
    {
        return false;
    }

    var monthLength = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

    // Adjust for leap years
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    {
        monthLength[1] = 29;
    }

    // Check the range of the day
    return day > 0 && day <= monthLength[month - 1];
}
</script>
</head>
</html>