
$(document).ready(function () {
    GetEmployeeDetail();
    GetPayrollYear();
    GetConductBatch();
});

function GetAttedanceDeatil() {
    $('#dvAttedanceGridDiv').css('text-align', 'center');

    $.ajax({
        type: "POST",
        url: "/Pages/Hrm/Employee/EmployeeView.aspx/GetAttedanceDeatil",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            window.setTimeout(function () {
                $('#Atddiv').show();
                $("#dvAttedanceGridDiv").html('');
                if (!data) {
                    $('#dvAttedanceGridDiv').html('<tr><td colspan="9">No Record found !</td><tr>');
                }
                else {
                    $(data.d).find('Attedance').each(function () {
                        $("#dvAttedanceGridDiv").append("<tr><td>" + $(this).find('NameofMonth').text() + "</td><td>" + $(this).find('TotalDays').text() + "</td>" + "</td><td>" + $(this).find('WorkingDays').text() + "</td>" + "</td><td>" + $(this).find('PresentDays').text() + "</td>" + "</td><td>" + $(this).find('AbsentDays').text() + "</td>" + "</td><td>" + $(this).find('PaidLeave').text() + "</td></tr>");
                    });
                }
            }, 500);
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetEmployeeDetail() {
    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetEmployeeDetail",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = $.parseJSON(data.d);

            if (datas.EmployeePhoto != '' && datas.EmployeePhoto != null)       // MantisId : 0035912
                $("#EmployeePhotoImg").attr('src', 'data:image/jpg;base64,' + datas.EmployeePhoto);
            else
                $("#EmployeePhotoImg").attr('src', '../../../Images/BoyNoImage.png');

            // Employee Details
            $("#NameLabel").html(datas.EmployeeName);
            $("#CodeLabel").html(datas.Code);
            $("#EmpFullNameLabel").html(datas.EmployeeName);
            $("#EmpCodeLabel").html(datas.Code);
            $("#EmpUIDnoLabel").html(datas.UIDNumber);

            //Communication
            $("#EmpEmailLabel").html(datas.PersonalEmail);
            $("#EmpContactNoLabel").html(datas.MobileNumber);
            $("#CompanyEmailLabel").html(datas.CompanyEmail);
            $("#CompanyContactNoLabel").html(datas.PhoneNoCompany);
            $("#PresentAddressLabel").html(datas.PresentAddress);
            $("#PresentCityLabel").html(datas.PresentCity);
            $("#PresentStateLabel").html(datas.PresentState);
            $("#PresentZipCodeLabel").html(datas.PresentZipCode);
            $("#PresentContactNoLabel").html(datas.PresentContactNo);
            $("#EmergencyAddressLabel").html(datas.EmergencyAddress);
            $("#EmergencyCityLabel").html(datas.EmergencyCity);
            $("#EmergencyStateLabel").html(datas.EmergencyState);
            $("#EmergencyZipCodeLabel").html(datas.EmergencyZipCode);
            $("#EmergencyContactNoLabel").html(datas.EmergencyContactNo);
            $("#PermanentAddressLabel").html(datas.PermanentAddress);
            $("#PermanentCityLabel").html(datas.PermanentCity);
            $("#PermanentStateLabel").html(datas.PermanentState);
            $("#PermanentZipCodeLabel").html(datas.PermanentZipCode);
            $("#PermanentContactNoLabel").html(datas.PermanentContactNo);

            //Job Details
            $("#DesignationLabel").html(datas.EmployeeDesignation);
            $("#QualificationLabel").html(datas.Qualification);
            $("#DepartmentLabel").html(datas.OrganizationDepartment);
            $("#DateOfJoiningLabel").html(datas.DateOfJoiningJSON);

            //var jsonDate = datas.DateOfJoining;
            //$("#DateOfJoiningLabel").html(new Date(parseInt(jsonDate.substr(6))));

            $("#EmployeeGradeLabel").html(datas.EmployeeGrade);
            $("#EmployeeGroupLabel").html(datas.EmployeeGroup);

            //Other Details
            $("#EmpDateOfBirthLabel").html(datas.DateOfBirthJSON)
            $("#EmpGenderLabel").html(datas.Gender)
            $("#EmpBloodGroupLabel").html(datas.BloodGroup)
            $("#MaritalStatusLabel").html(datas.MeritalStatus)


            $("#UIDNumberLabel").html(datas.UIDNumber)
            $("#ReligionLabel").html(datas.Religion)
            $("#CasteCategoryLabel").html(datas.CasteCategory)
            $("#FamilyCasteLabel").html(datas.FamilyCast)
            $("#EmpQualificationLabel").html(datas.Qualification)
            $("#IdentificationMarkLabel").html(datas.IdentificationMark1)
            $("#AadharNoLabel").html(datas.AdhaarNumber)

            //Bank Details
            $("#BankNameLabel").html(datas.BankName)
            $("#BankBranchLabel").html(datas.BankBranch)
            $("#IFSCLabel").html(datas.IFSCNumber)
            $("#AccountLabel").html(datas.AccountNumber)
            GetPreviousEmploymentDeatil();
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetPreviousEmploymentDeatil() {

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetPreviousEmploymentDetail",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            if (datas != "") {
                $('#EmpPrevEmployment').html('');
                $.each($.parseJSON(datas), function (i, item) {


                    var CompanyName, Duration, Designation, Lastsalary, ReasonForLeaving, JobDescription;
                    CompanyName = item.CompanyName;
                    Duration = item.Duration;
                    Designation = item.Designation;
                    Lastsalary = item.Lastsalary;
                    ReasonForLeaving = item.ReasonForLeaving;
                    JobDescription = item.JobDescription;
                    $('#EmpPrevEmployment').append('<tr><td>' + CompanyName + '</td><td>' + Duration + '</td><td>' + Designation + '</td><td>' + Lastsalary + '</td><td>' + ReasonForLeaving + '</td><td>' + JobDescription + '</td></tr>');
                });
            }
            //GetBatch();
            GetConductBatch();
            GetTaskAssignedToMe();

        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}



function GetConductBatch() {
    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetConductBatch",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            datas = $.parseJSON(datas);
            if (datas != "") {
                $('#ConductBatchDropdown').html(datas[0].Result);
            }
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetPayrollYear() {
    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetPayrollYear",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            datas = $.parseJSON(datas);
            if (datas != "") {
                //$('#PayrollYearDropdown').html('<option value="-1"> - Select - </option>');
                $('#PayrollYearDropdown').html(datas[0].Result);
            }
            //GetEmployeePayrollDetail();
            GetTaskAssignedToMe();
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

//function GetEmployeePayrollDetail() {
//    $.ajax({
//        type: "POST",
//        url: "/Pages/Hrm/Employee/Employee360View.aspx/GetEmployeePayrollDetail",
//        data: '{}',
//        contentType: "application/json;charset=utf-8",
//        dataType: "json",
//        success: function (data) {
//            var datas = "[" + (data.d) + "]";
//            datas = $.parseJSON(datas);
//            if (datas != "") {
//                $('#PayrollYearDropdown').html(datas[0].Result);
//            }
//            GetTaskAssignedToMe();
//        },
//        error: function (xhr, status, error) {

//            var err = eval("(" + xhr.responseText + ")");
//            console.log(err.Message);
//        }
//    });
//}


function GetTaskAssignedToMe() {

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetTaskAssignedToMe",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            if (datas != "") {
                $('#EmpTaskAssignedtoMe').html('');
                $.each($.parseJSON(datas), function (i, item) {
                    var Code, AssignedBy, StartDate, DueDate, Remark, Status;
                    Code = item.SourceCode;
                    AssignedBy = item.SourceName;
                    StartDate = item.AssignedDate;
                    DueDate = item.DueDate;
                    Remark = item.Remark;

                    if (item.IsCompleted = 0)
                        Status = "Pending";
                    if (item.IsCompleted = 1)
                        Status = "Completed";
                    $('#EmpTaskAssignedtoMe').append('<tr><td>' + Code + '</td><td>' + AssignedBy + '</td><td>' + StartDate + '</td><td>' + DueDate + '</td><td>' + Remark + '</td><td>' + Status + '</td></tr>');
                });
            }
            GetTaskAssignedByMe();
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetTaskAssignedByMe() {

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetTaskAssignedByMe",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            if (datas != "") {
                $('#EmpTaskAssignedbyMe').html('');
                $.each($.parseJSON(datas), function (i, item) {
                    var SourceCode, AssignedTo, AssignedDate, DueDate, Remark, Status;
                    SourceCode = item.SourceCode;
                    AssignedTo = item.SourceName;
                    AssignedDate = item.AssignedDate;
                    DueDate = item.DueDate;
                    Remark = item.Remark;
                    if (item.IsCompleted == 0)
                        Status = "Pending";
                    if (item.IsCompleted == 1)
                        Status = "Completed";
                    $('#EmpTaskAssignedbyMe').append('<tr><td>' + SourceCode + '</td><td>' + AssignedTo + '</td><td>' + AssignedDate + '</td><td>' + DueDate + '</td><td>' + Remark + '</td><td>' + Status + '</td></tr>');
                });
            }
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function SelectPayrollDetail(obj, SalaryMonth, SalaryYear, PayrollYearId) {
    debugger;
    $.ajax({

        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/SelectPayrollDetail",
        data: '{"SalaryMonth":"' + SalaryMonth + '", "SalaryYear":"' + SalaryYear + '", "PayrollYearId":"' + PayrollYearId + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = $.parseJSON(data.d)
            openpopup();
            $('#ctl00_CP_HostelMasterTabContainer_TabPanel2_MonthDetailLabel').text(" Detailed Salary of " + $(obj.closest('tr')).find("td:first").text() + " " + $(obj.closest('tr')).find("td:eq(1)").text());
            if (datas.EmpEarning == '') {
                $('#PayrollEarningDetailDiv').show();
                $('#dvEmployeePayrollEarningDetailGridDiv').html('<tr><td colspan="2">No Record found !</td><tr>');
            }
            else {
                $('#PayrollEarningDetailDiv').show();
                $('#dvEmployeePayrollEarningDetailGridDiv').html(datas.EmpEarning);
            }

            if (datas.EmpDeduction == '') {

                $('#PayrollDeductionDetailDiv').show();
                $('#dvEmployeePayrollDeductionDetailGridDiv').html('<tr><td colspan="2">No Record found !</td><tr>');
            }
            else {
                $('#PayrollDeductionDetailDiv').show();
                $('#dvEmployeePayrollDeductionDetailGridDiv').html(datas.EmpDeduction);
            }
            //$('#ctl00_CP_HostelMasterTabContainer_AttedanceTabPanel_AttendanceTypeDropDownList').empty();
            //$('#Atddiv').hide();
            //$('#Subjectatdiv').hide();
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function openpopup() {
    LoadingMsg('Loading Details', '<b>Please Wait ...</b>');
    window.setTimeout(function () {
        CloseLoading();
        $('.hls-bg').show();
        $('#dvresultpopup').show();
    }, 500);
}


function Closepopup() {
    $('.hls-bg').hide();
    $('#dvresultpopup').hide();
}


function GetConductEntryDetail() {

    var ConductBatchId = $('#ConductBatchDropdown').val();

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetConductEntryDetail",
        data: '{"ConductBatchId":"' + ConductBatchId + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";

            if (datas != "") {
                $('#ConductDiv').html('');
                $.each($.parseJSON(datas), function (i, item) {
                    var ConductDate, ConductType, Title, Remark, CreatedBy, CreatedDate, Indicator;
                    ConductDate = item.ConductDateJSON;
                    ConductType = item.ConductType;
                    Title = item.Title;
                    Remark = item.Remark;
                    CreatedBy = item.DisplayTitle;
                    CreatedDate = item.CreatedDateTimeJSON;
                    Indicator = item.ConductIndicator;
                    $('#ConductDiv').append('<tr><td>' + ConductDate + '</td><td>' + ConductType + '</td><td>' + Title + '</td><td>' + Remark + '</td><td>' + CreatedBy + '</td><td>' + CreatedDate + '</td><td>' + Indicator + '</td></tr>');
                });
            }
            if (datas == "") {
                $('#ConductDiv').html('');
                $('#ConductDiv').append('<tr><td>No Record found !</td><tr>');
            }
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetAppraisalDetail() {

}

function GetAttendanceDetail() {
    GetBatch()
}

function GetBatch() {
    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetBatch",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            datas = $.parseJSON(datas);
            if (datas != "") {
                $('#BatchDropdown').html(datas[0].Result);
            }
            var BatchId = $('#BatchDropdown').val();
            GetEmployeeLeaveDetails(BatchId);
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetEmployeeLeaveDetails(BatchId) {

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetEmployeeLeaveDetails",
        data: '{"BatchId":"' + BatchId + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            if (datas != "") {
                //alert(datas);
                $('#EmployeeLeaveDiv').html('');
                $.each($.parseJSON(datas), function (i, item) {
                    var color = "";
                    var ReportdateString = item.ReportDate.substr(6);
                    var reportdate = new Date(parseInt(ReportdateString));

                    var FromdateString = item.FromDate.substr(6);
                    var fromdate = new Date(parseInt(FromdateString));

                    var TodateString = item.ToDate.substr(6);
                    var todate = new Date(parseInt(TodateString));

                    var FromDate, ToDate, NoOfLeave, ReportDate, LeaveType, LeaveReson, Status;
                    ReportDate = reportdate.getDate() + "/" + (reportdate.getMonth() + 1) + "/" + reportdate.getFullYear();
                    FromDate = fromdate.getDate() + "/" + (fromdate.getMonth() + 1) + "/" + fromdate.getFullYear();
                    ToDate = todate.getDate() + "/" + (todate.getMonth() + 1) + "/" + todate.getFullYear();
                    NoOfLeave = item.NoOfLeave;
                    LeaveType = item.LeaveType;
                    LeaveReson = item.LeaveReason;
                    Status = item.StatusName;

                    if (item.StatusName == "Pending") color = "#ffba4cad";
                    if (item.StatusName == "Approved") color = "#00a65999";
                    if (item.StatusName == "Rejected") color = "#ff8c7de3";

                    $('#EmployeeLeaveDiv').append('<tr><td>' + ReportDate + '</td><td>' + FromDate + '</td><td>' + ToDate + '</td><td>' + NoOfLeave + '</td><td>' + LeaveType + '</td><td>' + LeaveReson + '</td><td style="background-color:' + color + '; text-align:center">' + Status + '</td></tr>');
                });
            }
            GetEmployeeAttendanceDetails(BatchId);
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetEmployeeAttendanceDetails(BatchId) {

    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetEmployeeAttendanceDetails",
        data: '{"BatchId":"' + BatchId + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = "[" + (data.d) + "]";
            if (datas != "") {
                $('#EmployeeAttendanceDiv').html('');
                $.each($.parseJSON(datas), function (i, item) {
                    var Month, WorkingDays, PresentDays, LWP, PaidLeave, UnPaidLeave;
                    Month = item.Month + '-' + item.ForYear;
                    WorkingDays = item.WorkingDays;
                    PresentDays = item.PresentDays;
                    LWP = item.LWP;
                    PaidLeave = item.PaidLeave;
                    UnPaidLeave = item.UnPaidLeave;
                    $('#EmployeeAttendanceDiv').append('<tr><td>' + Month + '</td><td>' + WorkingDays + '</td><td>' + PresentDays + '</td><td>' + LWP + '</td><td>' + PaidLeave + '</td><td>' + UnPaidLeave + '</td></tr>');
                });

            }
            //GetPayrollYear();
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function GetPayrollDetail() {

    var PayrollYearId = $('#PayrollYearDropdown').val();
    $.ajax({
        type: "POST",
        url: "/Pages/MobileView/Employee360View.aspx/GetEmployeePayrollDetail",
        data: JSON.stringify({ "PayrollYearId": parseInt(PayrollYearId) }),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            window.setTimeout(function () {
                var datas = $.parseJSON(data.d);
                if (datas.EmpGrid == '') {
                    $('#EmployeePayrollDiv').html('<tr><td colspan="9">No Record found !</td><tr>');
                }
                else {
                    $('#EmployeePayrollDiv').html(datas.EmpGrid);

                }
            }, 500);
        },
        error: function (xhr, status, error) {

            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });

}