<?php
/* error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0); */
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>

<!-- 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 ">PEMBAYARAN SYAHRIAH <?php if(isset($_POST['dataTable'])){echo strtoupper($_POST['dataTable']); }?> </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">

<style>
  .form-control {
    height: 30px; /* Atur tinggi sesuai kebutuhan */
    /*border-radius: 5px;*/ /* Atur border-radius untuk sudut yang lebih halus */
    /*border: 1px solid #007bff;*/ /* Warna border */
    border-radius: 5px; /* Melengkungkan sudut */
    border: 2px solid #ccc;
    border-radius: 3px;
    font-size: 13px;
    background-color: white;
    padding: 4px 10px;
    box-sizing: border-box; /* Menghitung padding dalam lebar elemen */
  }
  .btn {
    border-radius: 5px; /* Sesuaikan dengan tombol */
  }
  .custom-spacing {
    margin-bottom: 2px; /* Jarak antar input 2px */
    }
    label{

    font-family: 'Arial', sans-serif; /* Ganti dengan font yang Anda suka */
    font-size: 12px; /* Ukuran font */
    font-weight: bold; /* Gaya font */
    color: #333; /* Warna font */
    margin-bottom: 5px; /* Jarak antara label dan input */
    padding: 4px 10px;
    }

</style>

<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 'koneksi.php'; 
require './kalenderhijriah.php';
$kode         = $_SESSION['kode'];
if(isset($_POST['rekening1'])){   
            
$id = $_POST['rekening1'].".".sprintf("%06d",$_POST['rekening2']);

//$id2 =$_POST['rekening'];
$bulanNumber = intval($_POST['bulan']);
/* $bulanNumber = intval($_POST['bulan']); */



$tglHitung = [
  "10" => 1,
  "11" => 2,
  "12" => 3,
  "1" => 4,
  "2" => 5,
  "3" => 6,
  "4" => 7,
  "5" => 8,
  "6" => 9,
  "7" => 10,
  "8" => 11,
  "9" => 12 // Menambahkan bulan September yang hilang
];
$BlnH = [
    1 => "10",
    2 => "11",
    3 => "12",
    4 => "01", // Adjusted to use leading zero
    5 => "02",
    6 => "03",
    7 => "04",
    8 => "05",
    9 => "06",
    10 => "07",
    11 => "08",
    12 => "09" // Ensure consistency with leading zeros for months
];
//$dataTable =$_POST['dataTable'];
$blnH = $calendar->monthName($hijri[0]);
$tglH = [
  "10" => $hijri[2]."-".$hijri[2]+1,
  "11" => $hijri[2]."-".$hijri[2]+1,
  "12" => $hijri[2]."-".$hijri[2]+1,
  "01" =>  ($hijri[2]-1)."-".$hijri[2],
  "02" => ($hijri[2]-1)."-".$hijri[2],
  "03" => ($hijri[2]-1)."-".$hijri[2],
  "04" => ($hijri[2]-1)."-".$hijri[2],
  "05" => ($hijri[2]-1)."-".$hijri[2],
  "06" => ($hijri[2]-1)."-".$hijri[2],
  "07" => ($hijri[2]-1)."-".$hijri[2],
  "08" => ($hijri[2]-1)."-".$hijri[2],
  "09" => ($hijri[2]-1)."-".$hijri[2] // Menambahkan bulan September yang hilang
];
function thN($month) {
    global $hijri; // Akses variabel global $hijri
    if ($month >= 1 && $month <= 9) {
        return $hijri[2] - 1; // Untuk bulan 1 sampai 9
    } elseif ($month >= 10 && $month <= 12) {
        return $hijri[2]; // Untuk bulan 10 sampai 12
    } else {
        return null; // Jika bulan tidak valid
    }
}

// Tahun dasar dari hijriah awal
$thnDasar = $hijri[2];

// Jika tanggal sudah dikirim, ambil tahun dari input
if (!empty($_POST['tgl'])) {
    $tglInput = $_POST['tgl'];
    $pecah = explode('-', $tglInput);

    // Ambil elemen terakhir (tahun)
    $tahunInput = trim(end($pecah));

    if (is_numeric($tahunInput)) {
        $thnDasar = (int)$tahunInput;
    }
}

//echo "Tahun dipakai: " . $thnDasar; // debug

$Thn2 = [
    1  => $thnDasar,
    2  => $thnDasar,
    3  => $thnDasar,
    4  => $thnDasar + 1,
    5  => $thnDasar + 1,
    6  => $thnDasar + 1,
    7  => $thnDasar + 1,
    8  => $thnDasar + 1,
    9  => $thnDasar + 1,
    10 => $thnDasar + 1,
    11 => $thnDasar + 1,
    12 => $thnDasar + 1
];


$TglNow2 = [
  "10" => $hijri[2]."-10-30",
  "11" => $hijri[2]."-11-30",
  "12" => $hijri[2]."-12-30",
  "01" =>  ($hijri[2]+1)."-01-30",
  "02"=> ($hijri[2]+1)."-02-30",
  "03" => ($hijri[2]+1)."-03-30",
  "04" => ($hijri[2]+1)."-04-30",
  "05" => ($hijri[2]+1)."-05-30",
  "06" => ($hijri[2]+1)."-06-30",
  "07" => ($hijri[2]+1)."-07-30",
  "08" => ($hijri[2]+1)."-08-30",
  "09" => ($hijri[2]+1)."-09-30" // Menambahkan bulan September yang hilang
];
$thnArab = $tglH[$blnH];
$tglNow = $TglNow2[$_POST['bulan']];
$query1 = "SELECT c.nim AS nim, c.nama AS nama,c.alamat AS alamat,a.id AS id,sum(a.kewajiban) AS kewajiban, sum(a.bayar) AS bayar,sum(a.bayar)-sum(a.kewajiban) AS sisa,tahun AS tahun,(SELECT no_kls 
     FROM kelas WHERE id = a.id ORDER BY tglmasehi DESC LIMIT 1) AS kls FROM bulanan a LEFT JOIN registrasi c on c.id=a.id WHERE a.id='".$id."' AND a.tglmasehi<='".$tglNow."'   GROUP BY a.id "; 
$hasil2 = mysqli_query($koneksi, $query1);
$reg = mysqli_fetch_assoc($hasil2); 

}
   
$bulaN = [
  "10" => "Syawal",
  "11" => "Dzul Qodah",
  "12" => "Dzul Hijjah",
  "01" => "Muharram",
  "02"=>  "Shafar",
  "03" => "R. Awal",
  "04" => "R. Tsani",
  "05" => "J. Ula",
  "06" => "J Tsani",
  "07" =>  "Rajab",
  "08" => "Syaban" // Menambahkan bulan September yang hilang
];


function Rp($angka) { 
$jadi = number_format($angka,0,'.',',');
return $jadi;
}

?>

<script type="text/javascript">
function hitungPaket2() {
var nilai_paket_1 = document.getElementById('pokok').value.replaceAll(",","");
var nilai_paket_2 = document.getElementById('margin').value.replaceAll(",","");
var jumlah = Number(nilai_paket_2) + Number(nilai_paket_1);
document.getElementById('jumlah').value = jumlah.toLocaleString();



}

</script>  

<form action="" method="POST">
<div class="row">
<div class="col-sm-6">
    <div class="form-group row custom-spacing">
    <label for="nama" class="col-sm-4">Bayar Bulan</label>
    <select id="bulan" name="bulan" class="form-control col-sm-8 select2" onkeydown="changeFocus(event,'jaminan')" required>
    <option value=""><?php if(isset($_POST['bulan'])){ echo $bulaN[$_POST['bulan']]; }else{echo "PILIH---- ";}?></option>
    <option value="10">Syawal</option>
    <option value="11">Dzul Qodah</option>
    <option value="12">Dzul Hijjah</option>
    <option value="01">Muharram</option>
    <option value="02">Shafar</option>
    <option value="03">R. Awal</option>
    <option value="04">R. Tsani</option>
    <option value="05">J. Ula</option>
    <option value="06">J. Tsani</option>
    <option value="07">Rajab</option>
    <option value="08">Sya`ban</option>
</select>


<!-- <p id="selectedValue"></p>
 <select id="dataTable" name="dataTable" class="form-control col-sm-8 select2" onkeydown="changeFocus(event,'jaminan')" required onchange="showSelectedValue()">
<script>
function showSelectedValue() {
    var select = document.getElementById("dataTable");
    var selectedOption = select.options[select.selectedIndex].text;
    document.getElementById("selectedValue").innerText = "Anda memilih: " + selectedOption;
}
</script> -->

</div>
<style>
.custom-input1 {
  width: 50px;
  margin-right: 10px;
}
.custom-input2 {
  width: 290px;
  margin-right: 10px;
}
</style>  
<?php
$bulanPotong = intval($calendar->monthName($hijri[0]));
if($bulanPotong>11){
  $tHnini = $hijri[2];
}else{
  $tHnini = $hijri[2]-1;
}

?>      

<div class="form-group row ">
<label for="nama" class=" col-sm-4">No ID </label>

<!-- Tiga input rekening bersampingan -->
<input type="hidden" name="rekening1" id="rekening1" class="form-control custom-input1" placeholder="Rekening 1" value="<?php echo $kode; ?>" onKeyDown="changeFocus(event,'rekening2')" readonly/>
<input type="text" name="rekening2" id="rekening2"required="" class="form-control col-sm-8" value="<?php if(isset($_POST['rekening2'])){echo sprintf("%06d",$_POST['rekening2']); }?>"  onKeyDown="changeFocus(event,'cariid')"/> 


<label for="nama" class="col-sm-4">Tanggal</label>
<input type="text" name="tgl" id="tgl" class="tgl form-control col-sm-6" value="<?php  echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $tHnini;  ?>" max="<?= $hijri[2];  ?>" onKeyDown="changeFocus(event,'kewajiban')" autofocus />
<input type="hidden" name="thnArab" id="thnArab" class="tgl form-control col-sm-6" value="<?php  if(isset($_POST['tgl'])){echo $_POST['tgl']; }else{ echo $tHnini; } ?>"  />

<button type="submit" id="cariid" name="cariid" class="  btn btn-info"  ><i class="fas fa-search"></i></button>
</div>
</form>

<script>
function changeFocus(event, nextFieldId) {
    if (event.key === 'Enter') {
        event.preventDefault(); // Mencegah pengiriman form saat Enter ditekan
        const nextField = document.getElementById(nextFieldId);
        if (nextField) {
            nextField.focus(); // Pindah fokus ke field berikutnya
        }
    }
}
</script>

<form action=".../../simpan/simpan_bayar_Syahriah_murid.php" method="POST"  class="form-simpanangsuran" id="form-simpanangsuran">

<!-- 
<div class="form-group row custom-spacing">
<label for="nama" class="col-sm-4">Tanggal</label>
<input type="text" name="tgl" id="tgl" class="tgl form-control col-sm-8" value="<?php  echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2];  ?>" max="<?= $hijri[2];  ?>" onKeyDown="changeFocus(event,'kewajiban')" autofocus />
<input type="hidden" name="thnArab" id="thnArab" class="tgl form-control col-sm-8" value="<?php  echo $thnArab; ?>"  />
</div> -->

<?php
$barisByar = mysqli_fetch_assoc(mysqli_query($koneksi, "SELECT MAX(byr_ke)AS byr_ke FROM bulanan WHERE id='$id' "));
$no_baris = ($tglHitung[$bulanNumber]-$barisByar['byr_ke']);

$mulai= $barisByar['byr_ke']+1;
$no = $mulai;

for ($i = $mulai; $i <= $tglHitung[$bulanNumber]; $i++) {

// Format tanggal dengan leading zero jika diperlukan
    $tanggal = ($i < 12) ? "0" . $i : $i;
    
    // Ambil nilai bulan dari array $blnH sesuai dengan $bulanNumber
    $bulan2 = $BlnH[$i];
    $thn2 = $Thn2[$i]. '-' .$BlnH[$i]."-". sprintf('%02d', $hijri[1]-5);
    
    // Buat nilai yang akan ditampilkan (misalnya: "01/10" untuk 1 Oktober)
   
    
    
    
?>
    <input type="hidden" name="idM2[]" value="<?php echo htmlspecialchars($id); ?>"/>
    <input type="hidden" name="tglmasehi2[]" value="<?php echo htmlspecialchars($thn2); ?>"/>
    <input type="hidden" name="bulan2[]" value="<?php echo htmlspecialchars($bulan2); ?>" /> 
    <input type="hidden" name="kelas2[]" value="<?php echo htmlspecialchars($reg['kls']); ?>"/>
    <input type="hidden" name="tahun2[]" value="<?php echo htmlspecialchars($reg['tahun']); ?>"/>
    <input type="hidden" name="byr_ke2[]" value="<?php echo htmlspecialchars($no++); ?>"/>
    <input type="hidden" name="bayar2[]" value="<?php echo 30000; ?>"/>
    <input type="hidden" name="nim2[]" value="<?php echo htmlspecialchars($reg['nim']); ?>"/>
    <input type="hidden" name="thnArab2[]" value="<?php echo htmlspecialchars($thnArab); ?>" />
    
<?php
} 
?>
<div class="form-group row custom-spacing">
<label for="nama" class="col-sm-4">Nominal</label>
<input type="text" name="bayar" id="bayar" class="form-control col-sm-8"onkeyup="hitungPaket2()" placeholder=" Ketik Pembayaran disini...!" onKeyDown="changeFocus(event,'keterangan')" required/>
</div>


<div class="form-group row custom-spacing">
<label for="nama" class="col-sm-4">Keterangan</label>
<input type="text" name="keterangan" id="keterangan" class="form-control col-sm-8" required="" value="<?php echo "Bayar";?>"  onKeyDown="changeFocus(event,'simpanansyahriah')"/>
<input type="hidden" name="keterangan2" value="<?php echo $reg['rekening'] . "[" . $reg['nama'] . "]"; ?>"/>
</div>

</div>


<div class="col-sm-6">
<div class="form-group row custom-spacing"> 
<label for="nama" class="col-sm-3" style="margin-left: 12px;">Kwajiban</label> 
<input type="text"name="kewajiban" id="kewajiban" class="form-control col-sm-8" readonly="Kewajiban" value="<?php echo number_format($reg['kewajiban'],0); ?>" />
<input type="hidden"name="kewajiban" id="kewajiban" class="form-control col-sm-8" readonly="Kewajiban" value="<?php echo number_format($reg['kewajiban'],0); ?>" />
</div> 

<div class="form-group row custom-spacing"> 
<label for="nama" class="col-sm-3"style="margin-left: 12px;">Terbayar</label> 
<input type="text"name="bayarpokok" id="bayarpokok" class="form-control col-sm-8" readonly="bayarpokok" value="<?php echo Rp($reg['bayar']);?>"/>
<input type="hidden"name="bayarpokok" id="bayarpokok" class="form-control col-sm-8" readonly="bayarpokok" value="<?php echo Rp($reg['bayar']);?>"/>
</div>

<div class="form-group row custom-spacing"> 
<label for="nama" class="col-sm-3"style="margin-left: 12px;">Sisa</label> 
<input type="text"name="sisa" id="sisa" class="form-control col-sm-8" readonly="sisa" value="<?php echo Rp($reg['sisa']);?>"/>
</div>

<div class="form-group row custom-spacing"> 
<label for="nama" class="col-sm-3"style="margin-left: 12px;">Nama</label> 
<input type="text"name="nama" id="nama" class="form-control col-sm-8" readonly="plafond" value="<?php echo $reg['nama']." [Kls-".$reg['kls']."]" ;?>"/>
</div>
<div class="form-group row custom-spacing"> 
<label for="nama" class="col-sm-3"style="margin-left: 12px;">Alamat</label> 
<input type="text"name="alamat" id="alamat" class="form-control col-sm-8" readonly="plafond" value="<?php echo $reg['alamat'];?>"/>
</div>
 


<!-- /.card-body -->
<div class="card-footer ">
<input type="submit" name="simpanansyahriah" id="simpanansyahriah" 
class="btn btn-primary"  value="Simpan" onclick='document.geElementById("myform").submit()'>
<a href="home.php?home=<?php echo md5("pembayaran_Syahriah");?>" id="tambah" class="btn btn-success">
Tambah
</a>
<?php if($_SESSION['level']<1){ ?>
<a href="home.php?home=<?php echo md5("edit_tagihansyahriah");?>" class="btn btn-warning">
Edit
</a>
<a href="home.php?home=<?php echo md5("delete_tagihansyahriah");?>" class="btn btn-danger">
Hapus
</a>
<?php } ?>
<a class="btn btn-secondary" type="button" href="pdf/cetak_validasi_angsuran_pdf.php?rekening=<?php echo $_POST['rekening'];?>" target="_bank">Print
</a>
<a href="home.php?home=<?php echo md5("cetakangsuran");?>" class="btn btn-secondary">
Cetak
</a>
</div>
</div>

</form>

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


<!-- <style>
input:out-of-range {
border:2px solid red;
}
</style> -->
<!-- <script type="text/javascript" src="jquery.js"></script>  -->


<script type="text/javascript">
// untuk format koma
//https://cobangoding.com/tips-dan-trick/memberi-titik-otomatis-pada-angka-saat-menginput-dengan-javascript/#comment-757
//https://codepen.io/cobangoding/pen/QWOoLXN
$('#bayar').on('keyup',function(){
var angka = $(this).val();
var hasilAngka = formatRibuan(angka);
$(this).val(hasilAngka);
});
function formatRibuan(angka) {
var number_string = angka.replace(/[^\.\d]/g, '').toString(),
split = number_string.split('.'),
sisa = split[0].length % 3,
angka_hasil = split[0].substr(0, sisa),
ribuan = split[0].substr(sisa).match(/\d{3}/gi);
// tambahkan titik jika yang di input sudah menjadi angka ribuan
if (ribuan) {
separator = sisa ? ',' : '';
angka_hasil += separator + ribuan.join(',');
}
angka_hasil =
split[1] != undefined ? angka_hasil + '.' + split[1] : angka_hasil;
return angka_hasil;
}
</script> 



</head>
</html>