<?php
// Enable error reporting for debugging
error_reporting(E_ALL^(E_NOTICE|E_WARNING));
ini_set('display_errors',1);
error_reporting(0);

include_once(dirname(__FILE__) . '/../kalenderhijriah.php');
include './koneksi.php';
$klSa = [
  "1" => "Satu",
  "2" => "Dua",
  "3" => "Tiga",
  "4" => "Empat",
  "5"=>  "Lima",
  "6" => "Enam",
  "S-A" => "Sifir A",
  "S-B" => "Sifir  B"
];
$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",
  "08" =>  "Rajab",
  "09" => "Syaban" // Menambahkan bulan September yang hilang
];
?>

<div class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-12">
<h1 class="m-0 text-dark"></h1>
</div>
</div>
</div>
</div>

<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="card">
    <div class="card-header bg-info">
        <h3 class="card-title">Buat Tagihan Syahriah</h3>
    </div>
    <div class="card-body">
        <form action="" method="POST">
            <div class="col-md-6 col-sm-6 col-xs-6">
                <div class="input-group pull-right">
                    <select class="form-control col-md-2 col-sm-2 col-xs-2 select2" name="tahun" id="tahun" required>
                        <option value="<?php echo $hijri[2] . "-" . ($hijri[2] + 1); ?>"><?php echo (isset($_POST['tahun']) && $_POST['tahun'] === $hijri[2] . "-" . ($hijri[2] + 1)) ? $hijri[2] . "-" . ($hijri[2] + 1) : $hijri[2] . "-" . ($hijri[2] + 1); ?></option>
                        <option value="<?php echo ($hijri[2] - 1) . "-" . $hijri[2]; ?>"><?php echo ($hijri[2] - 1) . "-" . $hijri[2]; ?></option>
                    </select>

                    <select class="form-control col-md-2 col-sm-2 col-xs-2 select2" name="kls" id="kls" required>
                        <option value=""><?php if(isset($_POST['kls'])){ echo $klSa[$_POST['kls']]; }else{echo "PILIH---- ";}?></option>
                        <option value="S-A">Sifir-A</option>
                        <option value="S-B">Sifir-B</option>
                        <option value="1">Satu</option>
                        <option value="2">Dua</option>
                        <option value="3">Tiga</option>
                        <option value="4">Empat</option>
                        <option value="5">Lima</option>
                        <option value="6">Enam</option>
                    </select>
                    <select id="bulan" name="bulan" class="form-control col-md-2 col-sm-2 col-xs-2 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>

                   
                     <input type="text" id="kewajiban"name="kewajiban" value="<?php if(isset($_POST['kewajiban'])){ echo $_POST['kewajiban']; }else{echo "Nominal Syahriah disini...!";}?>" style="width:100px;" class="form-control col-md-4 col-sm-4 col-xs-4"sy placeholder="Nominal Syahriah disini...!" >
                    <input type="submit" name="search" class="btn btn-warning" value="Proses">
                </div>
            </div>
        </form>
        
        <form  method="POST" id="formProduk">
        <table class="table table-bordered" id="sortTable">
            <thead>
                <tr class="fonttebal">
                    <th>No</th>
                    <th>ID</th>
                    <th>NIM</th>
                    <th>Nama</th>
                    <th>Kls</th>
                    <th>Tahun</th>
                    <th>Tgl</th>
                    <th>Tgl Masehi</th>
                    
                    <th><input type="checkbox" class="check_all" align="center"/></th>
                </tr>
            </thead>
            <tbody>
            <?php

            /*function kenaikan($keterangan, $kls) {
            if ($kls == "S-A" && $keterangan == "Naik") {
                return "S-B";
            } elseif ($kls == "S-B" && $keterangan == "Naik") {
                return 1;
            } elseif (!empty($kls) && $keterangan == "Tidak Naik") {
                return $_POST['kls'];
            } else {
                return $_POST['kls'] + 1;
            }
        }*/
            $no = 1;
            $tahun1 = substr($_POST['tahun'] ?? '', 0, 4);
            $tahun2 = substr($_POST['tahun'] ?? '', 5, 4);
            $kls = $_POST['kls'] ?? '';
            $keterangan = $_POST['keterangan'] ?? '';
            $bulan2 = $_POST['bulan'] ?? '';

    
$Thn2 = [
  "10" => $hijri[2],
  "11" => $hijri[2],
  "12" => $hijri[2],
  "01" => ($hijri[2]+1),
  "02" => ($hijri[2]+1),
  "03" => ($hijri[2]+1),
  "04" => ($hijri[2]+1),
  "05" => ($hijri[2]+1),
  "06" => ($hijri[2]+1),
  "07" => ($hijri[2]+1),
  "08" => ($hijri[2]+1),
  "09" => ($hijri[2]+1) // Menambahkan bulan September yang hilang
];
$tglmasehi2 = sprintf('%02d', $hijri[1]). '-' .$bulan2.'-'.$Thn2[$bulan2]; 
            $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
            ];
            $thnArab = $tglH[$blnH];

            if ($tahun1 === "" || $tahun2 === "" || $kls === "") {
                echo "<tr><td colspan='9'>Semua field harus diisi.</td></tr>";
            } else {

                // Use prepared statements for security
                $stmt = $koneksi->prepare("SELECT c.no as no, c.id as id, a.nim as nim, a.nama as nama, c.no_kls as kls, a.alamat as alamat, b.desa as desa, b.kec as kecamatan, b.kab as kota FROM registrasi a LEFT JOIN alamat b ON a.desa = b.k_desa LEFT JOIN kelas c ON c.id = a.id WHERE c.tahun >= ? AND c.tahun <= ? AND c.no_kls = ? AND a.kode='101' GROUP BY a.id ORDER BY a.id ASC");
                $stmt->bind_param("iis", $tahun1, $tahun2, $kls);
                $stmt->execute();
                $result = $stmt->get_result();

                if ($result->num_rows > 0) {
                    while ($baris = $result->fetch_assoc()) {
            ?>
                        <tr class="check" id="<?php echo $baris["id"]; ?>">
                            <td><?php echo $no++; ?></td>
                            <td><?php echo $baris['id']; ?></td>
                            <td><?php echo $baris['nim']; ?></td>
                            <td><?php echo $baris['nama']; ?></td>
                            <td><?php echo $_POST['kls']; ?></td>
                            
                            
                            <td><?php echo $thnArab; ?></td>
                            <td><?php echo date('Y-m-d'); ?></td>
                           <!--  <td><?php echo sprintf('%02d', $hijri[1]) . '-' . $calendar->monthName($hijri[0]) . '-' . $hijri[2]; ?></td> -->
                            <td><?php echo $tglmasehi2; ?></td>
                            <input type="hidden" name="id[]" value="<?php echo $baris['id']; ?>"/>
                            <input type="hidden" name="kls[]" value="<?php echo $kls; ?>"/>
                            
                            <input type="hidden" name="tahun[]" value="<?php echo $thnArab; ?>"/>
                            <input type="hidden" name="kewajiban[]" value="<?php echo $_POST['kewajiban']; ?>"/>
                            <input type="hidden" name="bulan" value="<?php echo $_POST['bulan']; ?>"/>
                            <input type="hidden" name="nim[]" value="<?php echo $baris['nim']; ?>"/>
                            
                            
                            <input type="hidden" name="tglmasehi[]" value="<?php echo $Thn2[$bulan2] . '-' . $bulan2 . '-' ."01" ;  ?>"/>
                            <!-- <input type="hidden" name="tglmasehi[]" value="<?php echo $Thn2[$bulan2] . '-' . $bulan2 . '-' .sprintf('%02d', $hijri[1]) ;  ?>"/> -->
                            
                                
                            <td>
                                <input type="checkbox" name="no[]" class="chk_boxes1" value="<?php echo $baris["id"]; ?>"/>
                            </td>
                        </tr>
                    <?php
                    }
                } else {
                    echo "<tr><td colspan='9'>Data Belum Ada.</td></tr>";
                }
                $stmt->close();
            }
            ?>
            </tbody>
        </table>

       
            <button type="submit" class="tombol-simpan btn btn-info" onclick="return confirm('Yakin Data Akan Disimpan dan benar...???');">Simpan</button>
        </form>
    </div>
</div>
</div>
</div>
</div>
</section>

<script src="js/jquery-3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('.chk_boxes1').click(function(){
$(this).closest('tr').toggleClass('removeRow', this.checked);
});

$('.check_all').click(function() {
$('.chk_boxes1').prop('checked', this.checked);
$('.check').toggleClass('removeRow', this.checked);
});

$('#formProduk').submit(function(e) {
e.preventDefault(); // Mencegah pengiriman formulir default
var formData = new FormData(this);
$.ajax({
url: './simpan/simpan_master_syahriah_murid.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function(data) {
    // Asumsikan data adalah pesan sukses dari server
    alert("Data berhasil disimpan--: " + data);
},
error: function(xhr, status, error) {
    // Menangani kesalahan yang terjadi
    alert("Terjadi kesalahan: " + xhr.responseText);
}
});
});
});
</script>

<!-- <script>
$(document).ready(function(){
$('.chk_boxes1').click(function(){
$(this).closest('tr').toggleClass('removeRow', this.checked);
});

$('.check_all').click(function() {
$('.chk_boxes1').prop('checked', this.checked);
$('.check').toggleClass('removeRow', this.checked);
});

$('#formProduk').submit(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: './simpan/simpan_kenaikan_kls.php',
type: 'POST',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function(data) {
    alert(data);
    // Optionally refresh table data or redirect
}
});
});
});
</script> -->
<script type="text/javascript">
$('#kewajiban').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>

