<?php  
session_start();
include './../koneksi.php'; 
include './../cek_login.php';    
ini_set('display_errors', 1);
error_reporting(E_ALL);
      
if (isset($_POST['id'])) {
    
    $checked_array = $_POST['no'];
    $dataTable = $_POST['dataTable'];
    function faktur($kodefaktur) {
        if ($kodefaktur === "bulanan") {
            return "SY";
        } else if ($kodefaktur === "iktibar") {
            return "IK";
        } else if ($kodefaktur === "imni") {
            return "IM";
        } else if ($kodefaktur === "imda1") {
            return "D1";
        } else if ($kodefaktur === "imda2") {
            return "D2";
        } else if ($kodefaktur === "imda3") {
            return "D3";
        } else if ($kodefaktur === "seragam") {
            return "SE";
        } else if ($kodefaktur === "alquran") {
            return "AQ";
        } else if ($kodefaktur === "kitab") {
            return "KI";
        } else if ($kodefaktur === "iuran") {
            return "IU";
        } else {
            return ""; // Default case
        }
    }
    
    // Hapus deklarasi $no_bukti di luar loop
    foreach ($_POST['id'] as $key => $value) {
        if (in_array($_POST['id'][$key], $checked_array)) {
            $id = $_POST['id'][$key];
            $nim = $_POST['nim'][$key];
            
            // Pindahkan pembuatan no_bukti ke sini dan tambahkan $key untuk membuat nilai unik
            $no_bukti = faktur($dataTable).$_SESSION['nokode'].date("YmdHis").sprintf("%03d",$key);
            
            $tglmasehi = $_POST['tglmasehi'][$key];
            $tgl = date('Y-m-d');
            $kls = $_POST['kls'][$key];    
            $tahun = $_POST['tahun'][$key];
            $kewajiban = str_replace(",", "", $_POST['kewajiban'][$key]);
            $username = $_SESSION['username'];
            $dataTable = $_POST['dataTable'];
            $keterangan = "Tagihan ".$dataTable;
            
            // Define bayar as a variable
            $bayar = 0;
            
            // Use prepared statements to prevent SQL injection
            $stmt = $koneksi->prepare("INSERT INTO $dataTable (no, id, nim, no_bukti, tglmasehi, tgl, no_kls, tahun, kewajiban, bayar, keterangan, username, koreksi) VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            
            if ($stmt) {
                $stmt->bind_param("ssssssssssss", $id, $nim, $no_bukti, $tglmasehi, $tgl, $kls, $tahun, $kewajiban, $bayar, $keterangan, $username, $username);
                
                if ($stmt->execute()) {
                    echo "Data Berhasil Disimpan<br>";
                    // Update registrasi
                    $updateKls = mysqli_query($koneksi, "UPDATE registrasi SET klsNow='$kls' WHERE id='$id'") or die(mysqli_error($koneksi));
                } else {
                    echo "Error executing statement: " . $stmt->error . "<br>";
                }
                // Close the statement
                $stmt->close();    
            } else {
                die(mysqli_errno($koneksi).':'.mysqli_error($koneksi));
            }
        }
    }
}
?>