var gridBuilder;
$(document).ready(function () {
    gridBuilder = $(".grid").gridBuilder();
    gridBuilder.onRowChanged(function () {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var name = gridBuilder.getCellValue(rowIndex, "Name");
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        getInsurRates(code, name);
    });
    gridBuilder.onExportClick(function () {
        loadGrid("", { exportExcel: true });
    });
    loadGrid();
});
function loadGrid(gridId, objectParameter) {
    var isExportExcel = objectParameter && objectParameter.exportExcel ? true : false;
    ajax({
        gridBuilder: gridBuilder,
        url: "SalaryApi/GetContractTypes",
        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 openModal(operation) {
    var title, src, width = "600", height = "240";
    if (operation === "insert") {
        title = window.top.contractTypes;
        src = "Salary/ContractSave";
    }
    else if (operation === "edit") {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        title = window.top.editContractTypes;
        src = "Salary/ContractSave?code=" + code;
    }
    else if (operation === "detail") {
        var rowIndex = gridBuilder.getActiveRowIndex();
        if (rowIndex < 0)
            return;
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        var name = gridBuilder.getCellValue(rowIndex, "Name");
        title = window.top.contractDetails;
        src = "Salary/CoefficientContract?name=" + name + "&contractCode=" + code;
        width = "900"; height = "500";
    }
    openInModal("", src, title, width, height, false);
}
function deleteRow() {
    var rowIndex = gridBuilder.getActiveRowIndex();
    if (rowIndex < 0)
        return;
    if (confirm(window.top.deleteConfirm)) {
        var code = gridBuilder.getCellValue(rowIndex, "Code");
        deleteContractType(code);
    }
}
function deleteContractType(value) {
    ajax({
        url: "SalaryApi/DeleteContractType",
        data: { code: value },
        success: function (result) {
            refresh();
            showMsgModal(2, result.FinalMessage);
        }
    });
}
function refresh(code) {
    loadGrid();
    if (code)
        getInsurRates(code);
}
function getInsurRates(contractCode, contractName) {
    if (contractCode) {
        $("#lblContractCode").text(contractCode);
        $("#lblContractName").text(contractName);
        ajax({
            url: "SalaryApi/GetContractTypeInsuranceRates",
            data: { code: contractCode },
            success: function (result) {
                $('#divDetailInsur').empty();
                var model = result;
                var tag = "";
                for (var i = 0; i < model.length; i++) {
                    tag += "<div class='col-xs-12 no-padding data-header' style='border: 1px solid #e7e7e7; border-top: unset; line-height: 38px;'>";
                    tag += "<div class='col-xs-2 no-padding-left'>" + model[i].StartYearMonth.toString().substring(0, 4) + " " + getNameMonth(model[i].StartYearMonth.toString().substring(4)) + "</div>";
                    tag += "<div class='col-xs-2 no-padding-left'>" + model[i].EndYearMonth.toString().substring(0, 4) + " " + getNameMonth(model[i].EndYearMonth.toString().substring(4)) + "</div>";
                    tag += "<div class='col-xs-6 no-padding-left'>" + (model[i].Description ?? "") + '</div>';
                    tag += "<div class='col-xs-2 no-padding-left'>";
                    tag += "<img src='/img/edit.svg' title='" + window.top.edit + "' onclick='editCofficient(" + model[i].Code + "," + contractCode + ",\"" + contractName + "\")' style='width: 14px; cursor: pointer;'>";
                    tag += "<img src='/img/delete.svg' onclick='deleteCofficient(" + model[i].Code + ")' class='border-left' title='" + window.top.deletee + "' style='width: 27px; cursor: pointer; margin: 0 8px; padding: 0 5px;'>";
                    tag += "<img src='/img/down.svg' class='img-down' style='width: 22px;cursor: pointer;'>";
                    tag += "</div>";
                    tag += "</div>";
                    tag += "<div class='col-xs-12' style='background-color: #f0f0f0; padding: 15px; border-bottom: 1px solid #e7e7e7; display: none;'>";
                    tag += "<div class='col-xs-12 no-padding' style='font-weight: 700; margin-bottom: 10px;'>" + window.top.insurance + "</div>";
                    tag += "<div class='col-xs-12 no-padding' style='background-color: #eff1f5; line-height: 24px; border: 1px solid #ccc; border-bottom: 2px solid #ccc; font-weight: 700;'>";
                    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.name + "</div>";
                    tag += "<div class='col-xs-2 no-padding-left border-left'>" + window.top.amounts + "</div>";
                    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.unit + "</div>";
                    tag += "</div>";
                    tag += madeTagTable(model[i]);
                    tag += "<div class='col-xs-12 no-padding' style='font-weight: 700; margin-bottom: 4px; margin-top: 20px;'>" + window.top.tax + "</div>";
                    tag += "<div class='col-xs-12 no-padding'>" + (model[i].PercentOfTotal ? model[i].PercentOfTotal : "") + " " + window.top.percentOfTotal + "</div>";
                    tag += "</div>";
                }
                $('#divDetailInsur').append(tag);
                $(".img-down").click(function () {
                    if ($($(this).parent()).parent().next().is(":hidden")) {
                        $($(this).parent()).parent().next().show();
                        $(this).css('transform', 'rotate(180deg)');
                    }
                    else {
                        $($(this).parent()).parent().next().hide();
                        $(this).css('transform', 'rotate(0deg)');
                    };
                });
            }
        });
    }
}
function editCofficient(code, contractCode, contractName) {
    title = window.top.contractDetails;
    src = "Salary/CoefficientContract?name=" + contractName + "&contractCode=" + contractCode + "&code=" + code;
    openInModal("", src, title, "900", "500", false);
}
function deleteCofficient(code) {
    if (confirm(window.top.deleteConfirm)) {
        ajax({
            url: "SalaryApi/DeleteInsuranceRate",
            data: { code: code },
            success: function (result) {
                showMsgModal(2, result.FinalMessage);
                location.reload();
            }
        });
    }
}
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;
}
function madeTagTable(data) {
    var tag = "";
    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.workingDayHours + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.QtyHour + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.hoursADay + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.employeeInsuranceCoefficient + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.PersonelRate + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.percent + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.employerInsuranceCoefficient + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.EmployerRate + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.percent + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.unemployeeInsuranceCoefficient + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.UnemploymentRate + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.percent + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.numberOfChildren + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.QtyChild + " </div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top.digit + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.maximumDailyIncome + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.MaxInsurAmount + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top._rial + "</div>";
    tag += "</div>";

    tag += "<div class='col-xs-12 no-padding' style='background-color: #fff; line-height: 24px; border: 1px solid #ccc; border-top: unset;'>";
    tag += "<div class='col-xs-8 no-padding-left border-left'>" + window.top.maximumReward + "</div>";
    tag += "<div class='col-xs-2 no-padding-left border-left'>" + data.MaxInsurRewardAmount + "</div>";
    tag += "<div class='col-xs-2 no-padding-left'>" + window.top._rial + "</div>";
    tag += "</div>";
    return tag;
}
