(function ($) {
    function getColumnIndex(columns, columnName) {
        return columns.findIndex(column => column.data === columnName);
    }

    const createdAtIndex = getColumnIndex(columns, 'created_at');
    $('#datatable').DataTable({
        serverSide: true,
        processing: true,
        ajax: $('#datatable').data('ajax'),
        language: {
            emptyTable: "Tidak ada data yang tersedia pada tabel ini.",
            processing: "Sedang memproses ...",
            search: "",
            searchPlaceholder: "Ketik untuk mencari",
            lengthMenu: `<span class="d-none d-sm-inline-block">Lihat</span><div class="form-control-select mt-2"> _MENU_ </div>`,
            zeroRecords: "Tidak ditemukan data yang sesuai",
            info: "_START_ - _END_ dari _TOTAL_",
            infoEmpty: "",
            infoFiltered: "(total _MAX_)",
        },
        columns: columns,
        pageLength: 10,
        lengthMenu: [
            [10, 25, 50, 100],
            [10, 25, 50, 100]
        ],
        order: createdAtIndex !== -1 ? [[createdAtIndex, "desc"]] : [],
    });
})(jQuery);
