$(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    var table = $('.data-table').DataTable({
        processing: true,
        serverSide: true,
        responsive: true,
        ajax: url,
        columns: [
            { data: 'DT_RowIndex', name: 'DT_RowIndex' },
            { data: 'slider', name: 'slider' },
            { data: 'status', name: 'status' },
            { data: 'action', name: 'action', orderable: false, searchable: false },
        ],
        columnDefs: [
            { className: 'text-center', targets: [0, 2, 3]},
            { className: 'text-left', targets: [1]},
        ],
    });

    $("body").on("click", ".delete", function (e) {
        e.preventDefault();
        var id = $(this).data('id');

        swal({
            title: "Apakah Anda Yakin?",
            text: "Anda akan menghapus data ini!",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "Yes",
            cancelButtonText: "No",
            closeOnConfirm: false,
            closeOnCancel: false
        },
            function (isConfirm) {
                if (isConfirm) {
                    swal.close();
                    setTimeout(function () {
                        $.ajax({
                            dataType: 'json',
                            type: 'DELETE',
                            url: url + '/' + id,
                            headers: {
                                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                            },
                        }).done(function (data) {
                            table.draw();
                            swal({
                                title: "Data berhasil dihapus!",
                                type: "success",
                                timer: "3000"
                            });
                        });
                    }, 1000); // 1 second delay
                }
                else {
                    swal("Dibatalkan", "Data batal dihapus", "error");
                }
            }
        );
    });

});

$(function () {
    //iCheck for checkbox and radio inputs
    $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });
});

function printErrorMsg(msg) {
    $(".print-error-msg").find("ul").html('');
    $(".print-error-msg").css('display', 'block');
    $.each(msg, function (key, value) {
        $(".print-error-msg").find("ul").append('<li>' + value + '</li>');
    });
}
