/**
 * 定调数据分区编辑界面JS
 */

shr.defineClass("shr.emp.EmpSalaryItemDataPartEditEx", shr.emp.EmpSalaryItemDataPartEdit, {
    initalizeDOM: function () {
        var _self = this;
        shr.emp.EmpSalaryItemDataPartEditEx.superClass.initalizeDOM.call(this);
    },
    /**
     * 渲染自定表格
     */
    redenerCustomTable: function () {
        //隐藏标品表格
        $("#salarytable-container").prev().hide()
        //标品表格数据
        var rowDatas = _self.$editGrid.getAllRowData();
        console.log(rowDatas);
        var preCmpDatas = window.parent.jsBinder.preCmpData;
        console.log(preCmpDatas);
        //
        var preMoneySum = 0;
        //年薪合计
        var yearBonusBaseSum = 0;
        //标准薪资合计
        var salaryStandardSum = 0;
        var operateState = this.getOperateState();
        //扩展
        const $thead = $("#salaryTable thead");
        const $tbody = $("#salaryTable tbody");
        //const $th = $("<tr><th>薪酬项</th><th>币别</th></tr>");
        var $th = $("<tr><th>薪酬项</th></tr>");
        $thead.append($th);
        var $tr1 = $("<tr><td class='disabled edit-cell ui-state-highlight'>调整前</td></tr>");
        $tbody.append($tr1);
        var $tr2 = $("<tr><td class='disabled edit-cell ui-state-highlight'>调整后</td></tr>");
        $tbody.append($tr2);
        for (var i = 0; i < rowDatas.length; i++) {
            var preMoney = 0;
            var amount = 0;
            var rowData = rowDatas[i];
            var salaryItem = rowData.salaryItem;
             if ("ADJ033" != salaryItem.number) {
            //表头
            var $th1 = "<th>" + salaryItem.name + "</th>";
            $th.append($th1);
            if ("ADDNEW" == operateState) {
                for (var j = 0; j < preCmpDatas.length; j++) {
                    var preCmpData = preCmpDatas[j]
                    if (salaryItem.id && salaryItem.id == preCmpData.empAdjItemId) {
                        //调整前金额
                        preMoney = parseFloat(preCmpData.money);
                        if ("ADJ021" == salaryItem.number) {
                            //标准薪资合计
                            preMoneySum += preMoney * 12;
                        } else if ("ADJ029" == salaryItem.number) {
                            //季度奖金基数 
                            preMoneySum += preMoney * 4;
                        } else if ("ADJ030" == salaryItem.number) {
                            //年终奖基数 
                            preMoneySum += preMoney;
                        }
                        break;
                    }
                }
            } else {
                //调整前金额
                preMoney = parseFloat(rowData.preAmount ? rowData.preAmount : 0);
                //调整后金额
                amount = parseFloat(rowData.amount ? rowData.amount : 0);
                if ("ADJ021" == salaryItem.number) {
                    //标准薪资合计
                    preMoneySum += preMoney * 12;
                    yearBonusBaseSum += amount * 12;
                } else if ("ADJ029" == salaryItem.number) {
                    //季度奖金基数 
                    preMoneySum += preMoney * 4;
                    yearBonusBaseSum += amount * 4;
                } else if ("ADJ030" == salaryItem.number) {
                    //年终奖基数 
                    preMoneySum += preMoney;
                    yearBonusBaseSum += amount;
                } else {
                    //累加标准薪资合计
                    //salaryStandardSum += amount;
                }
            }
            //第一行
            var $td1 = '<td role="gridcell" style="text-align:left;" class="disabled edit-cell ui-state-highlight" title="' + preMoney + '">' + preMoney + '</td>';
            $tr1.append($td1);
            //第二行
            if ("ADJ021" == salaryItem.number) {
                //标准薪资合计
                var $td2 = '<td id="after_stdSalaryTotal" style="text-align:left;" class="disabled edit-cell ui-state-highlight" title="' + amount + '">' + amount + '</td>';
            } else {
                var $td2 = '';
                if ("VIEW" != operateState) {
                    $td2 = '<td><input id="after_' + salaryItem.id + '" type="number" min="0" maxlength="200" value="' + amount + '" ctrlrole="text" name="' + salaryItem.number + '" required="true" errorshowmode="float" class="block-father input-height cursor-pointer required"></td> ';
                } else {
                    $td2 = '<td id="after_stdSalaryTotal" style="text-align:left;" class="disabled edit-cell ui-state-highlight" title="' + amount + '">' + amount + '</td>';
                }
            }
            $tr2.append($td2);
        }
            if (i == rowDatas.length - 1) {
                //最后一次遍历,处理年薪合计
                $th1 = "<th>年薪合计</th>";
                $th.append($th1);
                $td1 = '<td role="gridcell" style="text-align:left;" class="disabled edit-cell ui-state-highlight" title="' + preMoneySum + '" aria-describedby="empSalaryItemPartDataDetail_preAmount">' + preMoneySum + '</td>';
                $tr1.append($td1);
                $td2 = '<td id="after_moneySum" style="text-align:left;" class="disabled edit-cell ui-state-highlight" title="" aria-describedby="empSalaryItemPartDataDetail_preAmount">' + yearBonusBaseSum + '</td>';
                $tr2.append($td2);
            }
        }
        if ("VIEW" != operateState) {
            var fields = $('[id*="after_"]');
            fields.on("change", function (event) {
                var number = event.currentTarget.name;
                if ("ADJ021" != number && "ADJ029" != number && "ADJ030" != number) {
                    //累加标准薪资合计
                    var moneySum = _self.getAfterStdSalaryTotal()
                    $("#after_stdSalaryTotal").html(moneySum);
                }
                //计算年薪合计
                var moneySum = _self.getAfterMoneySum();
                $("#after_moneySum").html(moneySum);
            });
        }
    },

    /**
     * 组装数据
     * @param {*} action 
     * @returns 
     */
    assembleData: function (action) {
        var _self = this;
        var data = shr.assembleModel(_self.fields, _self.getWorkarea(), _self.uuid);
        var empSalaryItemPartDataDetail = data.empSalaryItemPartDataDetail;
        for (var i = 0; i < empSalaryItemPartDataDetail.length; i++) {
            var empSalaryItemPartData = empSalaryItemPartDataDetail[i];
            //薪酬项目编码
            var number = empSalaryItemPartData.salaryItem.number
            //调整后金额
            var amount = 0;
            if ("ADJ021" == number) {
                amount = _self.getAfterStdSalaryTotal();
            } else if ("ADJ033" == number) {
                amount = _self.getAfterMoneySum();
            } else {
                amount = parseFloat($("[name='" + number + "']").val() != '' ? $("[name='" + number + "']").val() : 0);
            }
            empSalaryItemPartData.amount = amount;
            //调整前金额
            var preAmount = empSalaryItemPartData.preAmount;
            var modulationRatio = "0";
            if (amount && preAmount) {
                modulationRatio = parseFloat((amount - preAmount) / preAmount).toFixed(2) * 100;
                modulationRatio = parseFloat(modulationRatio).toFixed(0);
            }
            //调幅比
            empSalaryItemPartData.modulationRatio = modulationRatio + '%';
            //顺序
            empSalaryItemPartData.index = i;
        }
        return data; index
    },
    /**
     * 获取调整后标准薪资合计金额
     */
    getAfterStdSalaryTotal: function () {
        //基本工资标准
        var baseStd = parseFloat($("[name='ADJ022']").val() != '' ? $("[name='ADJ022']").val() : 0);
        //技能工资标准
        var skillStd = parseFloat($("[name='ADJ023']").val() != '' ? $("[name='ADJ023']").val() : 0);
        //岗位补助工资标准
        var postAllowStd = parseFloat($("[name='ADJ024']").val() != '' ? $("[name='ADJ024']").val() : 0);
        //加班工资标准
        var otSalaryStd = parseFloat($("[name='ADJ025']").val() != '' ? $("[name='ADJ025']").val() : 0);
        //学历补助标准
        var eduAllowStd = parseFloat($("[name='ADJ026']").val() != '' ? $("[name='ADJ026']").val() : 0);
        //绩效工资标准
        var perfSalaryStd = parseFloat($("[name='ADJ027']").val() != '' ? $("[name='ADJ027']").val() : 0);
        //费用补助
        var costSubsidy = parseFloat($("[name='ADJ032']").val() != '' ? $("[name='ADJ032']").val() : 0);
        //年薪合计 = 标准工资合计*12+季度奖基数*4+年终奖基数
        var moneySum = baseStd + skillStd + postAllowStd + otSalaryStd + eduAllowStd + perfSalaryStd + costSubsidy;
        return moneySum;
    },
    /**
    * 获取调整后年薪合计
    */
    getAfterMoneySum: function () {
        //标准薪资合计、季度奖金基数、年终奖基数 
        var stdSalaryTotal = parseFloat($("#after_stdSalaryTotal").html());
        var qBonusBase = parseFloat($("[name='ADJ029']").val() != '' ? $("[name='ADJ029']").val() : 0);
        var yearBonusBase = parseFloat($("[name='ADJ030']").val() != '' ? $("[name='ADJ030']").val() : 0);
        //年薪合计 = 标准工资合计*12+季度奖基数*4+年终奖基数
        var moneySum = parseFloat(stdSalaryTotal) * 12 + parseFloat(qBonusBase) * 4 + parseFloat(yearBonusBase);
        return moneySum;
    },
    /**
     * 
     * @returns 
     */
    processPreCmpRowData: function () {
        var _self = this;
        shr.emp.EmpSalaryItemDataPartEditEx.superClass.processPreCmpRowData.call(this);
        _self.redenerCustomTable();
    },
})