<?php
session_start();

if (!isset($_SESSION['user']) || $_SESSION['level'] != 'A') {
    header("Location: ../index.php");
    exit;
}

require '../config.php';

ini_set('display_errors', 1);
error_reporting(E_ALL);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    
    if (isset($_POST['masukanbos'])) {
        $cKode   = isset($_POST['cKode']) ? $_POST['cKode'] : '';
        $cNama   = isset($_POST['cNama']) ? $_POST['cNama'] : '';
        $diskon  = isset($_POST['diskon']) ? $_POST['diskon'] : '';
    
        $check = $conn->prepare("SELECT cKode FROM tbbarangdiskon WHERE cKode = ?");
        $check->bind_param("s", $cKode);
        $check->execute();
        $check->store_result();
    
        if ($check->num_rows > 0) {
            echo json_encode(["message" => "Error: Data dengan kode ini sudah ada!"]);
            exit;
        }
    
        $check->close();
    
        $query = "INSERT INTO tbbarangdiskon (cKode, Diskon) VALUES (?, ?)";
        $stmt = $conn->prepare($query);
        $stmt->bind_param("sd", $cKode, $diskon);
    
        if ($stmt->execute()) {
            echo "masukan";
            exit;
        } else {
            echo json_encode(["message" => "Error: Gagal menyimpan data!"]);
            exit;
        }
    
        $stmt->close();
    }
    
    if (isset($_POST['editbos'])) {
        $cKode   = isset($_POST['cKode']) ? $_POST['cKode'] : '';
        $cNama   = isset($_POST['cNama']) ? $_POST['cNama'] : '';
        $diskon  = isset($_POST['diskon']) ? $_POST['diskon'] : '';
    
        $query = "UPDATE tbbarangdiskon SET Diskon = ? WHERE cKode = ?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param("ds", $diskon, $cKode);
    
        if ($stmt->execute()) {
            echo "editiki";
            exit;
        } else {
            echo json_encode(["message" => "Error: Gagal menyimpan data!"]);
            exit;
        }
    
        $stmt->close();
    }
    
    
    
    if (isset($_POST['deletebos'])) {
        $cKode = $_POST['cKode'];  
    
        $query = "DELETE FROM tbbarangdiskon WHERE cKode = ?";
        $stmt = $conn->prepare($query);
        $stmt->bind_param("s", $cKode);
    
        if ($stmt->execute()) {
            echo "deleteiki"; 
            exit;
        } else {
            echo "Error: Gagal menghapus data!";  
            exit;
        }
    
        $stmt->close();  
    }
    
    
}

$search_barang = isset($_POST['search_barang']) ? trim(mysqli_real_escape_string($conn, $_POST['search_barang'])) : '';

$query_barang = "
    SELECT t.*
    FROM tbbarang t
    WHERE t.cKode NOT IN (SELECT cKode FROM tbbarangdiskon)
";

if (!empty($search_barang)) {
    $query_barang .= " AND (t.cKode LIKE '%$search_barang%' OR t.cNama LIKE '%$search_barang%')";
}

$query_barang .= " ORDER BY t.cKode ASC";
$query_barang .= !empty($search_barang) ? " LIMIT 25" : " LIMIT 50";

$stmt_barang = $conn->prepare($query_barang);
if (!$stmt_barang) {
    die("Prepare failed: " . $conn->error);
}

$stmt_barang->execute();
$result_barang = $stmt_barang->get_result();

$barangku = [];
if ($result_barang->num_rows > 0) {
    while ($row = $result_barang->fetch_assoc()) {
        $barangku[] = $row;
    }
}





$search_all = isset($_POST['search_all']) ? trim(mysqli_real_escape_string($conn, $_POST['search_all'])) : '';

$query = "
    SELECT tb.cKode, t.cNama, tb.Diskon
    FROM tbbarangdiskon tb
    JOIN tbbarang t ON tb.cKode = t.cKode
    WHERE 1=1
";

$params = [];
$types = "";

if (!empty($search_all)) {
    $query .= " AND (t.cKode LIKE ? OR t.cNama LIKE ?)";
    $params[] = "%{$search_all}%";
    $params[] = "%{$search_all}%";
    $types .= "ss";
}

$query .= " ORDER BY tb.cKode DESC";
$query .= !empty($search_all) ? " LIMIT 25" : " LIMIT 50";

$stmt = $conn->prepare($query);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();


$conn->close();
?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title>Barang Diskon</title>
    <meta content="width=device-width, initial-scale=1.0, shrink-to-fit=no" name="viewport" />
    <link rel="icon" href="../assets/img/sekolah/favicon6.ico" type="image/x-icon" />
    <script src="../assets/js/plugin/webfont/webfont.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        WebFont.load({
            google: { families: ["Public Sans:300,400,500,600,700"] },
            custom: {
                families: [
                    "Font Awesome 5 Solid",
                    "Font Awesome 5 Regular",
                    "Font Awesome 5 Brands",
                    "simple-line-icons",
                ],
                urls: ["../assets/css/fonts.min.css"],
            },
            active: function () {
                sessionStorage.fonts = true;
            },
        });
    </script>
    <link rel="stylesheet" href="../assets/css/bootstrap.min.css" />
    <link rel="stylesheet" href="../assets/css/plugins.min.css" />
    <link rel="stylesheet" href="../assets/css/kaiadmin.min.css" />
    <link rel="stylesheet" href="../assets/css/demo.css" />
    
</head>
<body>
    <div class="wrapper">
        <?php $menu = "barangdiskon"; require 'menu.php'; ?>
        <div class="main-panel">
            <?php require 'header.php'; ?>

            <div class="container">
                <div class="page-inner">
                    <div class="d-flex align-items-left align-items-md-center flex-column flex-md-row pt-2 pb-4">
                        <div class="page-header">
                            <h3 class="fw-bold mb-3">Barang Diskon</h3>
                            <ul class="breadcrumbs mb-3">
                                <li class="nav-home">
                                    <a href="<?= url("/admin"); ?>">
                                        <i class="icon-home"></i>
                                    </a>
                                </li>
                                <li class="separator">
                                    <i class="icon-arrow-right"></i>
                                </li>
                            </ul>
                        </div>
                    </div>

                    
                    <form method="POST" class="d-flex align-items-center w-100 justify-content-between">
                        <div class="form-group mr-2 flex-grow-1">
                            <input type="text" class="form-control" id="search_all" name="search_all" 
                                placeholder="Cari Halaman" style="min-width: 200px;" 
                                value="<?= htmlspecialchars($search_all ?? '') ?>">
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary" style="padding: 7px 20px; width: auto;">Cari</button>
                        </div>
                    </form>
                    <style>
                        .table thead th {
                            position: sticky;
                            top: 0;
                            background-color: #fff;
                            z-index: 1;
                            white-space: nowrap;
                        }
                        .table {
                            table-layout: auto;
                            width: 100%;
                            overflow-x: auto;
                        }
                        .table td, .table th {
                            white-space: nowrap;
                            text-overflow: ellipsis;
                            overflow: hidden;
                        }
                        .table-responsive {
                            overflow-x: auto;
                            max-height: 400px;
                        }
                    </style>
                                
                    
                    <div class="table-responsive">
                        <table class="table table-bordered table-striped">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Kode Barang</th>
                                    <th>Nama Barang</th>
                                    <th>Diskon</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php if (mysqli_num_rows($result) > 0): ?>
                                <?php $no = 1; while ($row = mysqli_fetch_assoc($result)): ?>
                                    <tr class="clickable-row"
                                        data-id="<?= $row['cKode'] ?>"
                                        data-nama="<?= $row['cNama'] ?>"
                                        data-Diskon="<?= $row['Diskon'] ?>">
                                        <td><?= $no++ ?></td>
                                        <td><?= htmlspecialchars($row['cKode']) ?></td>
                                        <td><?= htmlspecialchars($row['cNama']) ?></td>
                                        <td><?= number_format($row['Diskon'], 1) ?>%</td>
                                        
                                    </tr>
                                <?php endwhile; ?>
                            <?php else: ?>
                                <tr>
                                    <td colspan="4" class="text-center">Data tidak ditemukan</td> <!-- Update colspan for the new columns -->
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>






                    <div class="form-container">
                        <form id="formAlat" method="POST" enctype="multipart/form-data">
                            <div class="row">
                                <div class="col-md-12">

                                    <div class="form-group row">
                                        <label for="cgroup" class="col-sm-3 col-form-label">Pilih Barang</label>
                                        <div class="col-sm-9">
                                            <button type="button" class="btn btn-secondary" id="toggleBtn">Buka Barang</button>
                                        </div>
                                    </div>

                                    <div class="modal fade" id="dialogModal" tabindex="-1" role="dialog" aria-labelledby="dialogModalLabel" aria-hidden="true">
                                        <div class="modal-dialog" role="document">
                                            <div class="modal-content">
                                                <div class="modal-header">
                                                    <h5 class="modal-title" id="dialogModalLabel">Pilihan Barang</h5>
                                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                        <span aria-hidden="true">&times;</span>
                                                    </button>
                                                </div>
                                                <div class="modal-body">
                                                    <div class="form-group">
                                                        <label for="searchText">Cari Barang</label>
                                                        <input type="text" class="form-control" id="searchText" placeholder="Masukkan nama barang">
                                                        <button type="button" class="btn btn-primary mt-2" id="searchBtn">Cari</button>
                                                    </div>

                                                    <div class="table-responsive" style="max-height: 400px; max-width: 600px; overflow-y: auto;">
                                                        <table class="table table-bordered mt-4" id="tableBarang">
                                                            <thead>
                                                                <tr>
                                                                    <th>No</th>
                                                                    <th>Kode Barang</th>
                                                                    <th>Nama Barang</th>
                                                                    <th>Harga</th>
                                                                </tr>
                                                            </thead>
                                                            <tbody id="barangBody">
                                                                <?php if (!empty($barangku)): ?>
                                                                    <?php foreach ($barangku as $index => $item): ?>
                                                                        <tr class="clickable-row2" data-id="<?= $item['cKode'] ?>" data-nama="<?= $item['cNama'] ?>" data-harga="<?= $item['nHrgJualPerPcs'] ?>" data-cFileImage="<?= $item['cFileImage'] ?>">
                                                                            <td><?= $index + 1 ?></td>
                                                                            <td class="clickable-column" data-column="cKode"><?= $item['cKode'] ?></td>
                                                                            <td class="clickable-column" data-column="cNama"><?= $item['cNama'] ?></td>
                                                                            <td class="clickable-column" data-column="nHrgJualPerPcs"><?= 'Rp ' . number_format($item['nHrgJualPerPcs'], 0, ',', '.') ?></td>

                                                                        </tr>
                                                                    <?php endforeach; ?>
                                                                <?php else: ?>
                                                                    <tr>
                                                                        <td colspan="3" class="text-center">Data tidak ditemukan</td>
                                                                    </tr>
                                                                <?php endif; ?>
                                                            </tbody>
                                                        </table>
                                                    </div>
                                                </div>
                                                
                                            </div>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="cKode" class="col-sm-3 col-form-label">Kode Barang</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" id="cKode" name="cKode" placeholder="Masukkan Kode Barang" required readonly>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="cNama" class="col-sm-3 col-form-label">Nama Barang</label>
                                        <div class="col-sm-9">
                                            <input type="text" class="form-control" id="cNama" name="cNama" placeholder="Masukkan Nama Barang" required readonly>
                                        </div>
                                    </div>

                                    <div class="form-group row">
                                        <label for="cdiskon" class="col-sm-3 col-form-label">Diskon (%)</label>
                                        <div class="col-sm-9">
                                            <input type="number" step="0.1" min="0" max="100" class="form-control" id="cdiskon" name="cdiskon" placeholder="Masukkan diskon dalam persen" required>
                                        </div>
                                    </div>


                                </div>
                            </div>

                            <div class="row mt-3">
                                <div class="col-12 text-center">
                                    <button type="submit" name="create" id="btn-create" class="btn btn-success" style="margin-right: 20px;">Buat</button>
                                    <button type="submit" name="edit" id="btn-edit" class="btn btn-warning" style="margin-right: 20px;">Edit</button>
                                    <button type="submit" name="delete" id="btn-delete" class="btn btn-danger" style="margin-right: 20px;">Hapus</button>
                                    <button type="reset" name="cancel" id="btn-cancel" class="btn btn-secondary">Batal</button>
                                </div>
                            </div>
                        </form>
                    </div>

                </div>
            </div>
        </div>
    </div>
    <script src="../assets/js/core/jquery-3.7.1.min.js"></script>
    <script src="../assets/js/core/popper.min.js"></script>
    <script src="../assets/js/core/bootstrap.min.js"></script>
    <script src="../assets/js/plugin/jquery-scrollbar/jquery.scrollbar.min.js"></script>
    <script src="../assets/js/plugin/chart.js/chart.min.js"></script>
    <script src="../assets/js/plugin/jquery.sparkline/jquery.sparkline.min.js"></script>
    <script src="../assets/js/plugin/chart-circle/circles.min.js"></script>
    <script src="../assets/js/plugin/datatables/datatables.min.js"></script>
    <script src="../assets/js/plugin/bootstrap-notify/bootstrap-notify.min.js"></script>
    <script src="../assets/js/plugin/jsvectormap/jsvectormap.min.js"></script>
    <script src="../assets/js/plugin/jsvectormap/world.js"></script>
    <script src="../assets/js/plugin/sweetalert/sweetalert.min.js"></script>
    <script src="../assets/js/main.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" rel="stylesheet">

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        $(document).ready(function () {
            $("#toggleBtn").click(function () {
                $("#dialogModal").modal('show');
            });

            $(".clickable-column").click(function () {
                var column = $(this).data('column');
                var itemRow = $(this).closest('tr');
                var cKode = itemRow.data('id');   
                var cNama = itemRow.data('nama'); 
                var cHarga = itemRow.data('harga'); 
                var cFileImage = itemRow.data('cfileimage');

                $("#dialogModal").modal('hide');
                $("#cKode").val(cKode);
                $("#cNama").val(cNama);
                $("#cFoto").val(cFileImage);
            });

            $("#searchBtn").click(function () {
                var searchText = $("#searchText").val().toLowerCase();

                $("#barangBody .clickable-row2").each(function () {
                    var itemName = $(this).find("td:eq(1)").text().toLowerCase(); 
                    if (itemName.includes(searchText)) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                });
            });

            $("#searchText").keypress(function (e) {
                if (e.which == 13) {
                    e.preventDefault();
                    $("#searchBtn").click();
                }
            });
        });
    </script>


    <script>
    document.addEventListener("DOMContentLoaded", function () {
        const closeMenuLinks = document.querySelectorAll(".close-menu");

        closeMenuLinks.forEach(link => {
        link.addEventListener("click", function () {
            const collapseElement = document.getElementById("dataMaster");
            if (collapseElement) {
            const bsCollapse = new bootstrap.Collapse(collapseElement, {
                toggle: true 
            });
            }
        });
        });
    });
    </script>

    <script>
        
        $(document).ready(function () {

            var currentCnomer = null;
            $("#btn-edit").hide();
            $("#btn-delete").hide();
            $("#btn-cancel").show();

            $("#btn-cancel").on("click", function () {
                $("#cKode").val(""); 
                $("#cNama").val(""); 
                $("#cFoto").val(""); 
                $("#cketerangan1").val("");
                $("#cketerangan2").val("");
                $("#cketerangan3").val("");

                $("#btn-create").show();
                $("#btn-edit").hide();
                $("#btn-delete").hide();

                $("#formAlat")[0].reset();
                $(".form-group").has("button#toggleBtn").show();

            });
 
            $(document).on("click", ".clickable-row", function () {
                var cKode = $(this).data("id");
                var cNama = $(this).data("nama");
                var diskon = $(this).data("diskon");

                $("#cKode").val(cKode);
                $("#cNama").val(cNama);
                $("#cdiskon").val(diskon);

                $("#btn-create").hide();
                $("#btn-edit").show();
                $("#btn-delete").show();
                $("#btn-cancel").show();

                $(".form-group").has("button#toggleBtn").hide();
            });


            $("#btn-create").click(function (e) {
                e.preventDefault();

                var cKode = $("#cKode").val();
                var cNama = $("#cNama").val();
                var diskon = $("#cdiskon").val();
            
                if (!cKode) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Kode Barang tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (!cNama) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Nama Barang tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (diskon === "") {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }


                var diskonValue = parseFloat(diskon);
                if (isNaN(diskonValue)) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon harus berupa angka!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (diskonValue > 100) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon tidak boleh lebih dari 100%',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (!/^\d+(\.\d{1,2})?$/.test(diskon)) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon maksimal 2 digit di belakang koma!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                var formData = new FormData();
                formData.append("masukanbos", true);
                formData.append("cKode", cKode);
                formData.append("cNama", cNama);
                formData.append("diskon", diskon);
            
                Swal.fire({
                    title: "Konfirmasi",
                    text: "Apakah Anda yakin dengan data yang Anda inputkan?",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonText: "Ya, Simpan!",
                    cancelButtonText: "Batal"
                }).then((result) => {
                    if (result.isConfirmed) {
                        $.ajax({
                            url: '',
                            type: 'POST',
                            data: formData,
                            contentType: false,
                            processData: false,
                            success: function (response) {
                                response = response.trim();
                                if (response === 'masukan') {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Berhasil!',
                                        text: 'Data berhasil diperbarui!',
                                        timer: 3000,
                                        showConfirmButton: false,
                                    }).then(() => {
                                        location.reload();
                                    });
                                } else {
                                    Swal.fire({
                                        icon: 'error',
                                        title: 'Gagal!',
                                        text: 'Terjadi kesalahan, response: ' + response,
                                    });
                                    console.log("Error Response:", response);
                                }
                            },
                            error: function (xhr, status, error) {
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Error!',
                                    text: 'Terjadi kesalahan saat memproses data. Silakan coba lagi.',
                                });
                                console.log("AJAX Error:", status, error, xhr.responseText);
                            }
                        });
                    }
                });
                
                
            });

            $("#btn-edit").click(function (e) {
                e.preventDefault();
            
                var cKode = $("#cKode").val();
                var cNama = $("#cNama").val();
                var diskon = $("#cdiskon").val();
            
                if (!cKode) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Kode Barang tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (!cNama) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Nama Barang tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (diskon === "") {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon tidak boleh kosong!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }


                var diskonValue = parseFloat(diskon);
                if (isNaN(diskonValue)) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon harus berupa angka!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (diskonValue > 100) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon tidak boleh lebih dari 100%',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
                
                if (!/^\d+(\.\d{1,2})?$/.test(diskon)) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Diskon maksimal 2 digit di belakang koma!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }
            
                var formData = new FormData();
                formData.append("editbos", true);
                formData.append("cKode", cKode);
                formData.append("cNama", cNama);
                formData.append("diskon", diskon);
            
                Swal.fire({
                    title: "Konfirmasi",
                    text: "Apakah Anda yakin dengan data yang Anda inputkan?",
                    icon: "warning",
                    showCancelButton: true,
                    confirmButtonText: "Ya, Simpan!",
                    cancelButtonText: "Batal"
                }).then((result) => {
                    if (result.isConfirmed) {
                        $.ajax({
                            url: '',
                            type: 'POST',
                            data: formData,
                            contentType: false,
                            processData: false,
                            success: function (response) {
                                response = response.trim();
                                if (response === 'editiki') {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Berhasil!',
                                        text: 'Data berhasil diperbarui!',
                                        timer: 3000,
                                        showConfirmButton: false,
                                    }).then(() => {
                                        location.reload();
                                    });
                                } else {
                                    Swal.fire({
                                        icon: 'error',
                                        title: 'Gagal!',
                                        text: 'Terjadi kesalahan, response: ' + response,
                                    });
                                    console.log("Error Response:", response);
                                }
                            },
                            error: function (xhr, status, error) {
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Error!',
                                    text: 'Terjadi kesalahan saat memproses data. Silakan coba lagi.',
                                });
                                console.log("AJAX Error:", status, error, xhr.responseText);
                            }
                        });
                    }
                });
            });
            

            $("#btn-delete").click(function (e) {
                e.preventDefault();

                var cKode = $("#cKode").val();

                if (!cKode) {
                    Swal.fire({
                        icon: 'warning',
                        title: 'Peringatan!',
                        text: 'Pilih data yang ingin dihapus!',
                        timer: 3000,
                        showConfirmButton: false,
                    });
                    return;
                }

                Swal.fire({
                    icon: 'warning',
                    title: 'Apakah Anda yakin?',
                    text: 'Data ini akan dihapus!',
                    showCancelButton: true,
                    confirmButtonText: 'Ya, hapus!',
                    cancelButtonText: 'Batal',
                }).then((result) => {
                    if (result.isConfirmed) {
                        $.ajax({
                            url: '',  
                            type: 'POST',
                            data: { deletebos: true, cKode: cKode },  
                            success: function (response) {
                                response = response.trim();
                                if (response === "deleteiki") {
                                    Swal.fire({
                                        icon: 'success',
                                        title: 'Berhasil!',
                                        text: 'Data berhasil dihapus!',
                                        timer: 3000,
                                        showConfirmButton: false,
                                    }).then(() => {
                                        location.reload(); 
                                    });
                                } else {
                                    Swal.fire({
                                        icon: 'error',
                                        title: 'Gagal!',
                                        text: 'Terjadi kesalahan, response: ' + response,
                                    });
                                    console.log("Error Response:", response);
                                }
                            },
                            error: function (xhr, status, error) {
                                Swal.fire({
                                    icon: 'error',
                                    title: 'Error!',
                                    text: 'Terjadi kesalahan saat memproses data. Silakan coba lagi.',
                                });
                                console.log("AJAX Error:", status, error, xhr.responseText);
                            }
                        });
                    }
                });
            });
        });
    </script>



</body>
</html>
