function initializeJobs() {
    let url = window.location.href;
    let splitted = url.split('=');
    let id = decodeURI(splitted[splitted.length - 1]);
    let isLicenseCM = $("#isLicenseCM").val();

    $dataTable = $('#mainGrid');

    $(".selectpicker-dropdown").on("hide.bs.dropdown", function () {
        $dataTable.DataTable().ajax.reload();
    });

    let dtColumns = [
        { data: "IsActive", name: "IsActive", render: $.fn.dataTable.render.toggle(true, true) },
        { data: "Name", name: "acName", filtertype: "text", render: $.fn.dataTable.render.link("Name") },
        { data: "Name2", name: "acName2", filtertype: "text", render: $.fn.dataTable.render.link("Name") }
    ];

    if (isLicenseCM === 'False') {
        dtColumns.push({ data: "NameSuperior", name: "acNameSuperior", filtertype: "text" });
        dtColumns.push({ data: "Department", name: "acDeptId", filtertype: "text" });
    }

    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
                d.Status = $("#Status").val();
            }
        },
        columns: dtColumns,
        "createdRow": function (row, data, dataIndex) {
            if (data.Name == id) {
                $(row).addClass('side-menu-item-active').children(":first").addClass('side-menu-item-active');
            }
        }
    });

    var typingTimer;
    var doneTypingInterval = 600;
    $('#Keywords').off('keyup');
    $('#Keywords').on('keyup', function () {
        clearTimeout(typingTimer);
        typingTimer = setTimeout(function () { $.fn.dataTable.reload($dataTable); }, doneTypingInterval);
    });
}

function deleteJob() {
    let deleteText = $("#ConfirmDeleteMessage").val();
    let confirmText = $("#Confirm").val();
    let cancelText = $("#Cancel").val();

    $(document).ready(function () {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "question",
            text: deleteText,
            buttons: [{
                icon: 'fa fa-times',
                text: cancelText,
                closeOnAction: true,
                type: 'ghost'
            },
            {
                icon: 'fa fa-check',
                text: confirmText,
                closeOnAction: true,
                action: async function () {
                    removeJob();
                },
                closeOnAction: true,
                type: 'primary'
            }]
        });
    });
}

function removeJob() {
    let deleteJob = $("#DeleteJob").val();
    let jobsUrl = $("#JobsUrl").val();

    $.ajax({
        url: deleteJob,
        type: 'POST',
        success: function (data) {
            if (data === "True") {
                window.location.href = jobsUrl;
            } else {
                showInUseMessage();
            }
        }
    });
}

function showInUseMessage() {
    let confirmText = $("#Confirm").val();
    let text = $("#WorkPlaceInUse").val();

    $(document).ready(function () {
        $().customModal({
            destroyOnClose: true,
            showCloseButton: true,
            type: "alert",
            text: text,
            buttons: [
            {
                icon: 'fa fa-check',
                text: confirmText,
                closeOnAction: true,
                closeOnAction: true,
                type: 'primary'
            }]
        });
    });
}