function initializeProductTypes() {
    $("#Keywords").attr('placeholder', $('#searchPlaceholder').val());

    var userCanAccessCategoryCud = $("#userCanAccessCategoryCud").val() === 'True';

    $dataTable = $('#mainGridTypes');
    $dataTable.DataTable({
        ajax: {
            url: $dataTable.attr('data-url'),
            data: function (d) {
                d.Keywords = $("#Keywords").val();
            }
        },
        columns: [
            { data: "Id", name: "acSetOfItem", filtertype: "text", render: $.fn.dataTable.render.link("Id") },
            { data: "Name", name: "acName", filtertype: "text", render: $.fn.dataTable.render.link("Id") },
            { data: "Type", name: "acType", filtertype: "text" }
        ]
    });

    $("select").off("change.tableReload");
    $("select").on("change.tableReload", function () {
        $.fn.dataTable.reload($dataTable);
    });

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

function initializeProductTypesEdit() {
    $('#Name').focus();

    $('.input-validation-error').parents('.form-group').addClass('has-error');
    $('.field-validation-error').addClass('text-danger');
}

function setColor(color) {
    $("#input-main-color-box").css("background-color", color);
    $("#color-code").val(color);
}

function setIdentTimeValues(time) {    
    let total = convertTimeToMinutes(time);

    $("#PrstUMTime").val($("#DurationMinute").val());
    $("#PrstTime").val(total);
}

$(function setTimeValue() {
    let totalMinutes = $("#PrstTime").val()?.replace(",", ".");
    let durationUnit = $("#PrstUMTime").val();
    let durationIndex = 1;
    if (durationUnit === $("#DurationHour").val()) {
        durationIndex = 60;
    }
    let d = new Date();
    d.setHours(0, 0, 0, 0);
    d.setMinutes(d.getMinutes() + (parseFloat(totalMinutes) * durationIndex));

    let time = moment(d.getTime()).format("HH:mm");
    $("#Time").val(time);
});

function onDeleteProductType() {
    $().customModal({
        destroyOnClose: true,
        showCloseButton: true,
        type: "question",
        text: $("#ConfirmDeleteLoc").val(),
        buttons: [
            {
                icon: 'fa fa-times',
                text: $("#NoLoc").val(),
                closeOnAction: true,
                type: 'ghost'
            },
            {
                icon: 'fa fa-check',
                text: $("#YesLoc").val(),
                action: function () {
                    let productTypeId = $("#Id").val();
                    $.ajax({
                        url: $("#DeleteProductTypeUrl").val(),
                        type: 'POST',
                        headers: {
                            'Accept': 'application/json',
                            'Content-Type': 'application/json'
                        },
                        data: JSON.stringify({ id: productTypeId }),
                        success: function (response) {
                            if (response.IsDeleted)
                                window.location = $("#productTypeIndex").val();
                            else {
                                $().customModal({
                                    destroyOnClose: true,
                                    showCloseButton: true,
                                    type: "alert",
                                    text: $("#ItemAlreadyInUseLoc").val(),
                                    buttons: [
                                        {
                                            icon: 'fa fa-check',
                                            text: $("#ConfirmLoc").val(),
                                            closeOnAction: true,
                                            type: 'primary'
                                        }
                                    ]
                                });
                            }
                        },
                        error: function (error) {
                            console.error(error);
                        }
                    });
                },
                closeOnAction: true,
                type: 'primary'
            }
        ]
    });
}