$(document).ready(function () {
    submitFilterForm(); 
    if ($("#isLicenseCM").val() === "True") { setGlobalHelpUrl($("#WorkRecordsLicenseCMHelpUrl").val()) }
});

$(window).on('load', function () {
    forceDropdown();
});

function submitFilterForm() {
    $('#submit-filter-form').submit();
    fillExportForm();
    disableFilters();
}

function checkAndSubmitFilterForm(element) {
    if (element.hasClass("date-changed")) {
        submitFilterForm();
        element.removeClass("date-changed")
    }
}

$(".selectpicker-dropdown").on("hide.bs.dropdown", function () {
    submitFilterForm();
});

function fillExportForm() {

    $(".obligation-input").remove();
    $(".worker-input").remove();
    $(".department-input").remove();
    $(".empstatuses-input").remove();
    $(".workstatuses-input").remove();

    var workers = $("#Workers").val();
    var obligations = $("#WorkObligations").val();
    var departments = $("#Departments").val();
    var employmentStatuses = $("#EmploymentStatuses").val();
    var workingStatuses = $("#WorkingStatuses").val();

    var dateFrom = $("#Filter_DateFrom").val();
    var dateTo = $("#Filter_DateTo").val();

    $(".datestart-input").val(dateFrom);
    $(".dateend-input").val(dateTo);

    if (obligations) {
        for (var i = 0; i < obligations.length; i++) {
            $("#report-form").append("<input class='obligation-input' name='WorkObligations[" + i + "]' value='" + obligations[i] + "' />");
        }
    }

    if (workers) {
        for (var i = 0; i < workers.length; i++) {
            $("#report-form").append("<input class='worker-input' name='Workers[" + i + "]' value='" + workers[i] + "' />");
        }
    }

    if (departments) {
        for (var i = 0; i < departments.length; i++) {
            $("#report-form").append("<input class='department-input' name='Departments[" + i + "]' value='" + departments[i] + "' />");
        }
    }

    if (employmentStatuses) {
        for (var i = 0; i < employmentStatuses.length; i++) {
            $("#report-form").append("<input class='empstatuses-input' name='EmploymentStatuses[" + i + "]' value='" + employmentStatuses[i] + "' />");
        }
    }

    if (workingStatuses) {
        for (var i = 0; i < workingStatuses.length; i++) {
            $("#report-form").append("<input class='workstatuses-input' name='WorkingStatuses[" + i + "]' value='" + workingStatuses[i] + "' />");
        }
    }
}

function hideColumns() {
    var dateFrom = $("#Filter_DateFrom").val();
    var dateTo = $("#Filter_DateTo").val();

    if (dateFrom != dateTo) {
        $(".timeattendence-hidden-column").addClass('hidden');
    } else {
        $(".timeattendence-hidden-column").removeClass('hidden');
    }
}

function disableFilters() {
    $(".filter-row").addClass('locked-filters');
}

function enableFilters() {
    $(".filter-row").removeClass('locked-filters');
}