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

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

function initializeCalendarRequests() {
    let columns = [
        { data: "AbsenceType", name: "AbsenceType", filtertype: "text" },
        { data: "RequestDateTime", name: "RequestDateTime", filtertype: "dateTime" },
        { data: "DateTimeFrom", name: "DateTimeFrom", filtertype: "dateTime" },
        { data: "DateTimeTo", name: "DateTimeTo", filtertype: "dateTime" },
        {
            data: "WorkerComment",
            name: "WorkerComment",
            filtertype: "text",
            render: function (data, type, row) {

                if (type === 'display') {
                    if (data) {
                        if (data.length > 30) {
                            var dataPart = stringTrunc(data, 30);
                            var button = '<button type="button" class="btn btn-link request-comment-button" onclick="openWorkerNoteModal(' + row.AnQId + ')"> <i class="fa fa-comment theme-button-color"></i></button>';
                            var renderHtml = button + '<input type="hidden" id="workernoteId-' + row.AnQId + '" value="' + data + '" />';
                            return renderHtml + dataPart;
                        }
                    }

                    return data;
                }

                return data;
            }
        },
        {
            data: "LeaderComment",
            name: "LeaderComment",
            filtertype: "text",
            render: function (data, type, row) {
                if (type === 'display') {
                    if (data) {
                        if (data.length > 30) {
                            var dataPart = stringTrunc(data, 30);
                            var button = '<button type="button" class="btn btn-link request-comment-button" onclick="openLeaderNoteModal(' + row.AnQId + ')"> <i class="fa fa-comment theme-button-color"></i></button>';
                            var renderHtml = button + '<input type="hidden" id="leadernoteId-' + row.AnQId + '" value="' + data + '" />';
                            return renderHtml + dataPart;
                        }
                    }

                    return data;
                }

                return data;
            }
        },
        { data: "Leader", name: "Leader", filtertype: "text" },
        {
            data: "Status",
            name: "Status",
            filtertype: "text",
            render: function (data, type, full) {
                if (type === 'display') {
                    if (data === $("#RejectedValue").val()) {
                        return $("#Rejected").val();
                    } else if (data === $("#ApprovedValue").val()) {
                        return $("#Approved").val();
                    } else if (data === $("#NotifiedValue").val()) {
                        return $("#Notified").val();
                    } else {
                        return '<i class="fas fa-hourglass-half text-success"></i> ' + $("#InProgress").val();
                    }
                }

                return data;
            }
        }
    ];

    initializeCalendarRequestsDataTables('calendar-approve-table', 'Keywords', columns);
}

function stringTrunc(str, max, add) {
    add = add || '...';    
    return (typeof str === 'string' && str.length > max ? str.substring(0, max) + add : str);
};

function initializeCalendarRequestsDataTables(tableId, keywordsId, columns) {
    var typingTimer;
    var doneTypingInterval = 600;
    var newRequestIDs = $('#' + tableId).data("newids");

    if (keywordsId.length > 0) {
        $('#' + keywordsId).off('keyup');
        $('#' + keywordsId).on('keyup', function () {
            clearTimeout(typingTimer);
            typingTimer = setTimeout(function () { $('#' + tableId).DataTable().ajax.reload(); }, doneTypingInterval);
        });
    }

    $(".filter-select").on("change", function () {
        $('#' + tableId).DataTable().ajax.reload();
    });

    $(".selectpicker-dropdown").on("hide.bs.dropdown", function () {
        $('#' + tableId).DataTable().ajax.reload();
    });

    if ($('#' + tableId).length !== 0) {
        $dataTable = $('#' + tableId);
        $dataTable.DataTable({
            ajax: {
                url: $dataTable.attr('data-url'),
                data: function (d) {
                   d.Keywords = $('#' + keywordsId).val(),
                   d.Status = $('#Status').val(),
                   d.RequestType = $('#FilterTypeOfRequest').val(),
                   d.AbsenceTypes = $('#FilterAbsenceType').val()
                }
            },
            columns: columns,
            "createdRow": function (row, data, dataIndex) {
                if (newRequestIDs) {
                    newRequestIDs.forEach(function (id) {
                        if (data.AnQId === id)
                            $(row).addClass('side-menu-item-active').children(":first").addClass('side-menu-item-active');
                    });
                }
            }
        });
    }

    if (newRequestIDs && newRequestIDs.length > 0 && newRequestIDs[0] > 0) {
        window.localStorage.setItem('AnnouncmentDataFetched', false);
    }
}

function openWorkerNoteModal(id) {
    $('#note-modal').modal('show');
    $(".modal-footer").addClass("hidden");
    var message = $("#workernoteId-" + id).val();
    $("#item-note").val(message).attr("readonly", "readonly");
}

function openLeaderNoteModal(id) {
    $('#note-modal').modal('show');
    $(".modal-footer").addClass("hidden");
    var message = $("#leadernoteId-" + id).val();
    $("#item-note").val(message).attr("readonly", "readonly");
}

function openWFRequestModal() {
    var isExist = $("#WFAbsenceIDExist").val();
    if (isExist.toString().toLowerCase() === "false") {
        $('#wf-request-modal').modal('show');
    }
}