<?php

//error_reporting(0);
include('../koneksi.php');
require '../kalenderhijriah.php';
if (isset($_GET['r_register']) AND isset($_GET['r_nik'])){
    /*https://www.duniailkom.com/tutorial-form-php-cara-mengirim-variabel-pesan-antar-halaman-php/*/
 
$r_register = $_POST['r_register'];
}
function convertnohp($phoneNumber) {
    // Hapus karakter non-numeric
    $phoneNumber = preg_replace('/[^0-9]/', '', $phoneNumber);
    // Cek jika nomor mulai dengan 0
    if (substr($phoneNumber, 0, 1) == '0') {
        // Ganti awalan 0 dengan 62
        $phoneNumber = '62' . substr($phoneNumber, 1);
    }
    return $phoneNumber;
}

/*echo "This is Date: ". date('Y-m-d', strtotime($date));
https://www.educba.com/php-strtotime/
$date = str_replace('/', '-', $date);*/
$username   = $_SESSION['username'];
$kode   = $_SESSION['kode'];
$no_bukti = "MB".$_SESSION['nokode'].date("YmdHis");
$r_register = $_POST['r_register'];
$r_tgl =  date('Y-m-d',strtotime($_POST["r_tgl"]));
$r_nik = $_POST['r_nik'];
$r_kk = $_POST['r_kk'];
$r_nim = $_POST['r_nim'];
$r_nama = $_POST['r_nama'];
$r_kelamin = $_POST['r_kelamin'];
$r_ttl = $_POST['r_ttl'];
$r_tgll =  date('Y-m-d',strtotime($_POST["r_tgll"])); /*date('Y-m-d',strtotime(*/
$r_alamat = $_POST['r_alamat'];
$r_rtrw = $_POST['r_rtrw'];
$r_desa = $_POST['k_desa'];
$r_kecamatan = $_POST['k_kecamatan'];
$r_kabupaten = $_POST['k_kabupaten'];
$r_ibu = $_POST['r_ibu'];
$r_pekerjaan = $_POST['r_pekerjaan'];
$r_nohp = convertnohp($_POST['r_nohp']);
$kelas = $_POST['r_kelas'];
$tglnow = date("Y-m-d H:i:s");
$r_biaya = str_replace(",", "", $_POST['r_biaya']);
$tglmasehi1= sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];
$tglmasehi = date('Y-m-d',strtotime($tglmasehi1));
$tahun = $hijri[2]+1;


$query = mysqli_query($koneksi, "
INSERT INTO registrasi (id, nik,kk,nim,tgl,nama,kelamin,ttl, tgll,nohp,kelas,ibu,wali,alamat,rtrw, desa, kec,kab, username,foto,tglsystem)
    SELECT '{$r_register}', '{$r_nik}', '{$r_kk}','{$r_nim}', '{$r_tgl}', '{$r_nama}', '{$r_kelamin}','{$r_ttl}', 
           '{$r_tgll}', '{$r_nohp}','{$kelas}','{$r_ibu}', '{$r_pekerjaan}', 
           '{$r_alamat}', '{$r_rtrw}', '{$r_desa}', 
           '{$r_kecamatan}', '{$r_kabupaten}', '{$username}', 
           '0', '{$tglnow}'
    WHERE NOT EXISTS (
        SELECT 1 FROM registrasi
        WHERE kode = '$kode' 
          AND id = '$r_register' 
   
    )
") or die(mysqli_error($koneksi));
$query = mysqli_query($koneksi,"INSERT kelas SET id='$r_register',nim='$r_nim',tgl='$r_tgl',no_kls='$kelas',tahun='$tahun',keterangan='Baru',tglmasehi='$tglmasehi',username='$kode' ")or die(mysqli_error($koneksi));
echo "Data Sukses Disimpan--!!!";
// Insert first entry to bukubesar
$query_bukubesar1 = mysqli_query($koneksi, "INSERT INTO bukubesar SET no_bukti='$no_bukti', tanggal='$tglmasehi', kode_akun='1.100.10', debet='$r_biaya', kredit='0', keterangan='Daftar murid baru.$r_register', kode='$kode', tglsystem='$tglnow', username='$username'") or die(mysqli_error($koneksi));
$query_bukubesar2 = mysqli_query($koneksi, "INSERT INTO bukubesar SET no_bukti='$no_bukti', tanggal='$tglmasehi', kode_akun='4.400.10', debet='0', kredit='$r_biaya', keterangan='Daftar murid baru.$r_register', kode='$kode', tglsystem='$tglnow', username='$username'") or die(mysqli_error($koneksi));


/*$query = mysqli_query($koneksi,"insert into registrasi SET id='$r_register',kode='$kode',nik='$r_nik',tgl='$r_tgl',nama='$r_nama',kelamin='$r_kelamin',ttl='$r_ttl',tgll='$r_tgll',alamat='$r_alamat',rtrw='$r_rtrw',desa='$r_desa',kec='$r_kecamatan',kab='$r_kabupaten',ibu='$r_ibu',wali='$r_pekerjaan',nohp='$r_nohp',username='$username',foto='0',tglsystem='$tglnow' ")or die(mysqli_error($koneksi));*/
/*echo "Data Sukses Disimpan--!!!";*/






?>
