var gridBuilderTax;
var gridBuilderInsur;
$(document).ready(function () {
    gridBuilderInsur = $("#grdIncuranceList").gridBuilder();
    gridBuilderTax = $("#grdTaxList").gridBuilder();
    gridBuilderTax.onRowChanged(function () {
        var rowIndex = gridBuilderTax.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilderTax.getCellValue(rowIndex, "Code");
        showTaxDetail(code)
    });
    gridBuilderInsur.onRowChanged(function () {
        var rowIndex = gridBuilderInsur.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var startYearMonth = gridBuilderInsur.getCellValue(rowIndex, "StartYearMonth").toString();
        var endYearMonth = gridBuilderInsur.getCellValue(rowIndex, "EndYearMonth").toString();
        var startMonth = getNameMonth(startYearMonth.substring(4));
        var endMonth = getNameMonth(endYearMonth.substring(4));
        $("#lblInsurStartYearMonth").text(startMonth + " " + startYearMonth.substring(0, 4));
        $("#lblInsurEndYearMonth").text(endMonth + " " + endYearMonth.substring(0, 4));
        $("#lblDescriptionInsur").text(gridBuilderInsur.getCellValue(rowIndex, "Description"));
        $("#lblQtyHour").text(gridBuilderInsur.getCellValue(rowIndex, "QtyHour"));
        $("#lblPersonelRate").text(gridBuilderInsur.getCellValue(rowIndex, "PersonelRate"));
        $("#lblEmployerRate").text(gridBuilderInsur.getCellValue(rowIndex, "EmployerRate"));
        $("#lblUnemploymentRate").text(gridBuilderInsur.getCellValue(rowIndex, "UnemploymentRate"));
        $("#lblQtyChild").text(gridBuilderInsur.getCellValue(rowIndex, "QtyChild"));
        $("#lblMaxInsurAmount").text(gridBuilderInsur.getCellValue(rowIndex, "MaxInsurAmount"));
        $("#lblMaxInsurRewardAmount").text(gridBuilderInsur.getCellValue(rowIndex, "MaxInsurRewardAmount"));
    });
    $(".menu-item").click(function () {
        if ($(this).hasClass('selected-menu-item'))
            return;
        $(".menu-item").removeClass("selected-menu-item");
        $(this).addClass("selected-menu-item");
        if ($(this).attr('id') == 'tax') {
            $('#div-insurance').hide();
            $('#div-tax').show();
        }
        else {
            $('#div-tax').hide();
            $('#div-insurance').show();
        }
    });
    loadGrid("grdTaxList");
    loadGrid("grdIncuranceList");
})
function loadGrid(gridId, objectParameter) {
    var gridBuilder = $("#" + gridId).gridBuilder();
    var api = gridId === "grdTaxList" ? "GetTaxRates" : "GetInsuranceRates";
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        loading: true,
        url: "SalaryApi/" + api,
        data: {
            filter: gridBuilder.getFilter(),
            sort: gridBuilder.getSortFields(),
            pageNo: isExportExcel ? -1 : gridBuilder.getCurrentPageNumber()
        },
        success: function (data) {
            if (isExportExcel)
                exportExcel(data, gridBuilder.getColumns(), gridBuilder.getColumnsCaption());
            else
                gridBuilder.loadData(data);
        }
    });
}
function refresh(grd) {
    loadGrid(grd === "grdTaxList" ? grd : "grdIncuranceList");
}
function showTaxDetail(code) {
    ajax({
        url: "SalaryApi/GetTaxRate",
        data: { code: code },
        success: function (result) {
            if (result) {
                var startMonth = getNameMonth(result.StartYearMonth.toString().substring(4));
                var endMonth = getNameMonth(result.EndYearMonth.toString().substring(4));
                $("#lblStartYearMonthInsur").text(startMonth + " " + result.StartYearMonth.toString().substring(0, 4));
                $("#lblEndYearMonthInsur").text(endMonth + " " + result.EndYearMonth.toString().substring(0, 4));
                $("#lblDescriptionTax").text(result.Description);
            }
        }
    });
    $("#divTaxDetail").empty();
    ajax({
        url: "SalaryApi/GetTaxRatesDetail",
        data: { code: code },
        success: function (result) {
            if (result.length > 0) {
                for (var i = 0; i < result.length; i++) {
                    var tag = "";
                    tag += "<div class='col-xs-12 no-padding-right-left' style='border: 1px solid #ccc; border-top: unset; line-height: 34px;'> ";
                    tag += "<div class='col-xs-3 no-padding-left'>" + result[i].FromAmount + "</div> ";
                    tag += "<div class='col-xs-3 no-padding-left'>" + result[i].ToAmount + "</div> ";
                    tag += "<div class='col-xs-6'>" + result[i].Rate + "</div>";
                    tag += "</div> ";
                    $("#divTaxDetail").append(tag);
                }
            }
        }
    });
}
function editTaxRate() {
    var rowIndex = gridBuilderTax.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var code = gridBuilderTax.getCellValue(rowIndex, "Code");
    openModal("Tax", code);
}
function openModalInsuranceEdit() {
    var rowIndex = gridBuilderInsur.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var code = gridBuilderInsur.getCellValue(rowIndex, "Code");
    openModal("Insurance", code);
}
function openModal(type, code) {
    var url = type === "Insurance" ? "Salary/CoefficientInsuSave" : "Salary/CoefficientTaxSave";
    var tittle = type === "Insurance" ? window.top.insuranceCoefficients : window.top.taxCoefficients;
    if (code)
        url += "?code=" + code;
    openInModal("", url, tittle, "950", "", false);
}
function deleteTax() {
    var rowIndex = gridBuilderTax.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var code = gridBuilderTax.getCellValue(rowIndex, "Code");
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            url: "SalaryApi/DeleteTaxRate",
            data: { code: code },
            success: function (result) {
                refresh("grdTaxList");
                showMsgModal(2, result.FinalMessage);
                closeTopModal();
            }
        });
    }
}
function deleteInsurance() {
    var rowIndex = gridBuilderInsur.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    var code = gridBuilderInsur.getCellValue(rowIndex, "Code");
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            url: "SalaryApi/DeleteInsuranceRate",
            data: { code: code },
            success: function (result) {
                refresh();
                showMsgModal(2, result.FinalMessage);
                closeTopModal();
            }
        });
    }
}
function getNameMonth(value) {
    var nameMonth = "";
    switch (value) {
        case "01":
            nameMonth = "فروردین"; break;
        case "02":
            nameMonth = "اردیبهشت"; break;
        case "03":
            nameMonth = "خرداد"; break;
        case "04":
            nameMonth = "تیر"; break;
        case "05":
            nameMonth = "مرداد"; break;
        case "06":
            nameMonth = "شهریور"; break;
        case "07":
            nameMonth = "مهر"; break;
        case "08":
            nameMonth = "آبان"; break;
        case "09":
            nameMonth = "آذر"; break;
        case "10":
            nameMonth = "دی"; break;
        case "11":
            nameMonth = "بهمن"; break;
        case "12":
            nameMonth = "اسفند"; break;
    }
    return nameMonth;
}



