var my_skins = ["skin-blue", "skin-black", "skin-red", "skin-yellow", "skin-purple", "skin-green"];

$(function () {
    $("#EmpClearButton").click(function () {
        $("#empNametext").val("");
        $("#empMiddleNameText").val("");
        $("#empName_text").val("");
        $("#empID_text").val("");
        $("#empContactNoText").val("");
        $("#EmployeeAutoSearchTextBox").val("");
        $('#empContactNos').attr('checked', false);
        $("#divEmployee").html('');
    });

    $("#EmpButtonSearch").click(function () {
        var count = 0;
        if ($('#empNametext').val() != '') {
            count++;
        }
        if ($("#empMiddleNameText").val() != '') {
            count++;
        }
        if ($('#empName_text').val() != '') {
            count++;
        }
        if ($("#empID_text").val() != '') {
            count++;
        }
        if ($("#empContactNoText").val() != '') {
            count++;
        }
        if ($("#EmployeeAutoSearchTextBox").val() != '') {
            count++;
        }

        if (count == 0) {
            $('#empNametext').css('border-color', 'red');
            $('#empMiddleNameText').css('border-color', 'red');
            $('#empName_text').css('border-color', 'red');
            $('#empID_text').css('border-color', 'red');
            $('#empContactNoText').css('border-color', 'red');
            $('#EmployeeAutoSearchTextBox').css('border-color', 'red');
            alert('Enter any one criteria');
            return;
        }

        $.ajax({
            url: '/Pages/BaseFramework/MyDashBoard.aspx/GetEmployeeData',
            type: 'POST',
            data: "{ FirstName:'" + $("#empNametext").val() + "',LastName: '" + $("#empName_text").val() + "',ID: '" + $("#empID_text").val() + "',MiddleName:'" + $("#empMiddleNameText").val() + "',ContactNo: '" + $("#empContactNoText").val() + "',EmployeeAutoSearch:'" + $("#EmployeeAutoSearchTextBox").val() + "'}",
            contentType: 'application/json;charset=utf-8',
            dataType: 'json',
            success: function (data) {
                $("#divEmployee").html('');
                var tableData = '<table border="1" width="100%" Id="EmpTable_"><tr><td width="20%" style="background:grey;color:#fff;">Name</td>' + '<td style="background:grey;color:#fff;">Department</td>';

                var datas = $.parseJSON(data.d);
                if (datas.Grid != undefined) {
                    return;
                }
                if (document.getElementById('empContactNos').checked)
                    tableData += '<td style="background:grey;color:#fff;">Code</td><td style="background:grey;color:#fff;">Organization</td><td style="background:grey;color:#fff;">Status</td><td style="background:grey;color:#fff;">Contact No.</td></tr>';
                else
                    tableData += '<td style="background:grey;color:#fff;">Code</td><td style="background:grey;color:#fff;">Organization</td><td style="background:grey;color:#fff;">Status</td></tr>';

                if (datas[0].Is360Permission == "True")
                    tableData = tableData.replace("</tr>", "<td style='background:grey;color:#fff;'></td></tr>");

                for (var i = 0; i < datas.length; i++) {
                    tableData += '<tr><td style="color:Black;">' + datas[i].Name + '</td><td style="color:Black;">' + datas[i].DepartmentName;
                    if (datas[0].IsEmployeeDetailFormPermission == "False") {
                        if (datas[0].Is360Permission == "False") {
                            if (document.getElementById('empContactNos').checked)
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;">' + datas[i].Code + '</td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" >' + datas[i].ContactNo + '</td></tr>';
                            else
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;">' + datas[i].Code + '</td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td></tr>';
                        }
                        else {
                            if (document.getElementById('empContactNos').checked)
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;">' + datas[i].Code + '</td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" >' + datas[i].ContactNo + '</td><td style="color:black;" ><a style="color: #03afc5 !important;" onclick=\'window.open("../../Pages/Hrm/Employee/Employee360View.aspx?EmployeeId=' + datas[i].EmployeeId + '", "Employee View", "height=600,width=900,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes");\'>360 View</a></td></tr>'
                            else
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;">' + datas[i].Code + '</td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" ><a style="color: #03afc5 !important;" onclick=\'window.open("../../Pages/Hrm/Employee/Employee360View.aspx?EmployeeId=' + datas[i].EmployeeId + '", "Employee View", "height=600,width=900,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes");\'>360 View</a></td></tr>'
                        }
                    }
                    else {
                        if (datas[0].Is360Permission == "False") {
                            if (document.getElementById('empContactNos').checked)
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;"><a style="color: #03afc5 !important;" onclick=\'EmployeeDetailFormRedirect(' + datas[i].EmployeeId + ')\'>' + datas[i].Code + '</a></td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" >' + datas[i].ContactNo + '</td></tr>';
                            else
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;"><a style="color: #03afc5 !important;" onclick=\'EmployeeDetailFormRedirect(' + datas[i].EmployeeId + ')\'>' + datas[i].Code + '</a></td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td></tr>';
                        }
                        else {
                            if (document.getElementById('empContactNos').checked)
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;"><a style="color: #03afc5 !important;" onclick=\'EmployeeDetailFormRedirect(' + datas[i].EmployeeId + ')\'>' + datas[i].Code + '</a></td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" >' + datas[i].ContactNo + '</td><td style="color:black;" ><a style="color: #03afc5 !important;" onclick=\'window.open("../../Pages/Hrm/Employee/Employee360View.aspx?EmployeeId=' + datas[i].EmployeeId + '", "Employee View", "height=600,width=900,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes");\'>360 View</a></td></tr>'
                            else
                                tableData += '</td><td style="color:Black;overflow-wrap:anywhere;"><a style="color: #03afc5 !important;" onclick=\'EmployeeDetailFormRedirect(' + datas[i].EmployeeId + ')\'>' + datas[i].Code + '</a></td><td style="color:black;">' + datas[i].Organization + '</td><td style="color:black;" >' + datas[i].StatusName + '</td><td style="color:black;" ><a style="color: #03afc5 !important;" onclick=\'window.open("../../Pages/Hrm/Employee/Employee360View.aspx?EmployeeId=' + datas[i].EmployeeId + '", "Employee View", "height=600,width=900,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes");\'>360 View</a></td></tr>'
                        }
                    }
                }
                tableData += '</table>';
                $('#divEmployee').append(tableData);
                $("#EmpTable_").addClass("table table-striped table-bordered gridviewfont");
                $("#EmpTable_").css("margin-left", "-15px")
            }
        });
    });
});

document.addEventListener('keydown', function (e) {
    var path = window.location.pathname;
    var page = path.split("/").pop();
    if (page != 'StudentDetailForm.aspx' && page != 'FeeReceipt.aspx' && page != 'EmployeeDetailForm.aspx' && page != 'StockIssueStudent.aspx') {
        var tag = $(e.target).attr("id");
        var tag1 = $(e.target).attr('class');
        if (tag1 == "employeeautosearchitem" && (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40)) {
            switch (e.keyCode) {
                case 37: case 39: case 38: case 40: e.preventDefault();
            }
        }
        if ((tag == "EmployeeAutoSearchTextBox" || e.keyCode == 13) || (e.keyCode == 13 || tag1 == "employeeautosearchitem")) {
            Focus();
        }
    }
});

$(document).mouseup(function (e) {
    var container = $("#EmpAutoSearchSuggestion");
    if (!container.is(e.target) && container.has(e.target).length === 0) {
        document.getElementById("EmpAutoSearchSuggestion").hidden = true;
    }
});

function EmployeeAutoSearchNavigate(e) {
    var tag = $(event.target).attr("id");
    var tag1 = $(event.target).attr('class');
    switch (event.keyCode) {
        case 37: case 39: case 38: case 40: event.preventDefault();
    }
}

function SearchEmployee(obj) {
    if ($(obj).val().length > 1) {
        $.ajax({
            type: "POST",
            url: "/Pages/BaseFramework/MyDashBoard.aspx/SearchEmployee",
            data: '{"SearchValue":"' + $(obj).val() + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                document.getElementById("EmpAutoSearchSuggestion").hidden = false;
                document.getElementById("EmpAutoSearchSuggestion").innerHTML = data.d;
            },
            error: function (jqXHR, Status, errorMsg) {

            }
        });
    }
    else {
        document.getElementById("EmpAutoSearchSuggestion").hidden = true;
    }
}

function Focus() {
    var keyCode = window.event.keyCode;
    if (keyCode !== 38 && keyCode !== 40 && keyCode !== 13) return;

    var current = document.activeElement;
    let items = [];
    for (i = 0; i < document.getElementsByClassName('employeeautosearchitem').length; i++) {
        items[i] = document.getElementsByClassName('employeeautosearchitem')[i];
    }
    var currentIndex = items.indexOf(current);
    var newIndex;

    if (keyCode == 13) {
        selectEmployee(items[currentIndex].innerHTML, true);
    }
    else {
        if (currentIndex === -1) {
            newIndex = 0
        } else {
            if (keyCode === 38) newIndex = (currentIndex + items.length - 1) % items.length
            else newIndex = (currentIndex + 1) % items.length
        }
        items[newIndex].focus();
    }
}

function selectEmployee(val, IsEnter) {
    $("#EmployeeAutoSearchTextBox").val(val);
    var str = $("#EmployeeAutoSearchTextBox").val();
    if (!IsEnter)
        str = str.substring(0, str.length - 1);

    $("#EmployeeAutoSearchTextBox").val(str);
    document.getElementById("EmpAutoSearchSuggestion").hidden = true;
    $("#EmpButtonSearch").click();
}

function EmployeeDetailFormRedirect(EmployeeId) {
    window.location = "/Pages/Hrm/Employee/EmployeeDetailForm.aspx?EmployeeId=" + EmployeeId;
}