<?php

function sweetAlert($message, $type = 'success', $backurl = '') {
    if ($backurl == '') {
        $urlback = basename($_SERVER['REQUEST_URI']);
    }
    else{
        //Modif String URL for Other URL
        $urlback = $backurl;
    }
    $judul = ucfirst($type);
    echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
    echo "<script>
        Swal.fire({
            title: '$judul',
            text: '$message',
            icon: '$type'
        }).then(function() {
            window.location = '".$urlback."';
        });
    </script>";
}


function deleteAlert($tabel, $id, $backurl = '') {
    if ($backurl == '') {
        $urlback = basename($_SERVER['REQUEST_URI']);
    } else {
        $urlback = $backurl;
    }

    echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
    echo "<script>
    Swal.fire({
        title: 'Apa kamu yakin?',
        text: 'Anda tidak dapat mengembalikannya!',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Ya, Hapus!',
        cancelButtonText: 'Batalkan'
    }).then((result) => {
        if (result.isConfirmed) {
            // Melakukan permintaan AJAX untuk menghapus item
            fetch('list_data/delete.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: new URLSearchParams({
                    'tabel': '" . addslashes($tabel) . "',
                    'id': '" . addslashes($id) . "'
                })
            }).then(response => {
                if (!response.ok) {
                    throw new Error('Network response was not ok');
                }
                return response.json();
            }).then(data => {
                if (data.success) {
                    // Menyusun daftar item yang dihapus
                    let deletedItemsText = '';
                    data.deleted_items.forEach(item => {
                        deletedItemsText += item.count + ' item dari ' + item.table + '\\n';
                    });

                    Swal.fire({
                        title: 'Dihapus!',
                        text: 'Data telah dihapus: \\n' + deletedItemsText,
                        icon: 'success'
                    }).then(function() {
                        window.location = '".$urlback."';
                    });
                } else {
                    Swal.fire({
                        title: 'Error!',
                        text: data.error || 'Terjadi kesalahan saat menghapus data.',
                        icon: 'error'
                    }).then(function() {
                        window.location = '".$urlback."';
                    });
                }
            }).catch(error => {
                Swal.fire({
                    title: 'Error!',
                    text: 'Gagal mendapatkan respons dari server: ' + error.message,
                    icon: 'error'
                }).then(function() {
                    window.location = '".$urlback."';
                });
            });
        } else {
            window.location = '".$urlback."';
        }
    });
    </script>";
}

function detailAlert($data) {
    echo "<script src='https://cdn.jsdelivr.net/npm/sweetalert2@11'></script>";
    echo "<script>
        Swal.fire({
            title: 'Gagal..!',
            html: `$data`,
            icon: 'error',
            confirmButtonText: 'Oke'
        });
    </script>";
}

?>