/**
 * 专员维护考核人和评分人页面(专员) 动态列表专员操作
 */
shr.defineClass("shr.custom.ExamineEntryPFRDynamicList", shr.custom.CustomDynamicList, {
    editColumns: [],
    initalizeDOM: function () {
        this.editColumns.push("entrys.scoreleader");
        this.editColumns.push("entrys.remark");
        // this.editColumns.push("entrys.index");
        this.editColumns.push("entrys.uplastscore");
        this.editColumns.push("entrys.uplastlevel");
        var _self = this;

        _self.isSum = false
        shr.custom.ExamineEntryPFRDynamicList.superClass.initalizeDOM.call(this);
        _self.isValidationPeriodExpired();
        _self.initializeFilterAdminOrg();

    },
    synStandPerformResultAction() {
        var _self = this;
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        _self.remoteCall({
            method: 'synStandPerformResult',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                if (data) {
                    shr.showInfo({
                        message: "同步成功",
                        hideAfter: 2
                    });
                    setTimeout(function () {
                        _self.queryGrid();
                    }, 2000); // 2000 毫秒即为两秒钟
                }

            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
        });
    },
    /**
     *操作时校验周期是否锁定
     */
    verifyisLock() {
        var _self = this;
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        _self.remoteCall({
            method: 'verifyisLock',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {

            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
                setTimeout(function () {
                    location.reload();
                }, 2000);

            }
        });
    },
    //初始化过滤组织
    initializeFilterAdminOrg() {
        var _self = this;
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        _self.remoteCall({
            method: 'submitFilterAdmin',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                if (data != null) {
                    $('#f7-adminorg').shrPromptBox("setFilter", "id in(" + data + ")");
                }
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
            }
        });

    },
    // 根据动态列表配置 取业务主键
    getBillIdFieldName: function () {
        return 'periodicQuarter.id';
    },
    /**
* 描述: 导入action
*/
    importAction: function () {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                , hideAfter: 2
            });
            return;
        }
        //操作时校验是否锁定周期
        _self.verifyisLock();
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        // fastFilter
        var fastFilterItems = this.getFastFilterItems();
        var adminOrgId = fastFilterItems.adminorg.values;
        var customParam = { parentId: billId, adminOrgId: adminOrgId };
        this.doImportData('import', customParam);
    },
    /**
     * 查看累计得分
     */
    showAddupScoreAction() {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                , hideAfter: 2
            });
            return;
        }
        //获取周期季度ID
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        this.reloadPage({
            uipk: "com.kingdee.eas.custom.performancenew.app.ExamineGradePersonEntryAddupScore.list",
            billId: billId,
            //  method: 'list'id -> {BOSUuid@34323} "RpcAAAALmPOP3MLU"
        });

    },
    /**
       * 组装保存时传至服务端的数据
       */
    assembleSaveData: function (action) {
        var _self = this;
        //操作时校验是否锁定周期
        _self.verifyisLock();
        //组装需要修改的数据
        var data = [];
        var meta;
        var id, column, value, cur, element, personCode, datatype;
        var tds = $("#grid td[class*='dirty-cell']");
        var $table = $("#grid")[0];
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        for (var i = 0; i < tds.length; i++) {
            //获取父节点，以获取对应id
            meta = {};
            var cur = $(tds[i]);
            //获取输入的内容，而不是转以后的字符实体，同时对单引号处理

            fid = cur.parent().attr('id');
            element = cur.attr('aria-describedby').replace('grid_', '');
            value = $($table).getCell(fid, element);
            //value = cur.text().replace(/\'/g, "''").trim();
            datatype = _self.getColumnType(element);
            if ("promptbox" == datatype) {
                column = element.split('.')[0];
                value = value["id"];
            } else {
                column = element.split('.')[1];
            }
            //列表需要显示id列
            id = $("#" + _self.getEscapeChars(fid) + " td[aria-describedby='grid_entrys\\.id']").attr('title');
            // //校验输入内容
            // if(!_self.checkNumValid(value, datatype)){
            //     $(tds[i]).focus();
            //     $(tds[i]).css({
            //         "border" : "solid 2px red"
            //     });
            // 	throw new Error("请输入正确的格式");
            // }
            meta.fid = id;
            meta.billId = billId;
            meta.column = column;
            meta.value = value;
            meta.type = datatype;
            data.push(meta);
        }
        return data;
    },

    /**
     * 校验周期是否解除
     */
    isValidationPeriodExpired() {
        var _self = this;

        var billId = shr.getUrlRequestParam("examineGradePersonId");
        _self.remoteCall({
            method: 'isValidationPeriodExpired',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                if (data.value == 1) {
                    $("#saveDynGrid").hide();
                    $("#calculateScore").hide();
                    $("#customSubmit").hide();
                    $("#import").hide();
                    shr.showWarning({
                        message: "周期已锁定不可进行操作",
                        hideAfter: 3,
                    });
                }
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
            }
        });
    },
    /**
     * 自定义提交按钮
     */
    customSubmitAction() {
        var _self = this;
        //操作时校验是否锁定周期
        _self.verifyisLock();

        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()];
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行提交！"
            });
            return;
        }

        _self.remoteCall({
            method: 'submitFilterAdmin',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                if (data != null) {
                    $('#adminOrg').shrPromptBox("setFilter", "id in(" + data + ")");
                    $("#adminOrg").shrPromptBox("open");
                }
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
            }
        });

        $("#adminOrg").shrPromptBox("option", {
            onchange: function (e, value) {
                var dataList = [];
                var currentArray = value.current;
                // 使用each方法循环遍历数组
                $.each(currentArray, function (index, value) {
                    // 在控制台打印每个元素的索引和值
                    dataList.push(value.id)
                });

                var options = {
                    action: 'verifyIsEmpty',
                    uipk: this.uipk,
                    type: 'POST',
                    async: false,
                    param: {
                        billId: billId,
                        adminIdArray: dataList + ''
                    },
                    success: function (res) {
                        _self.verifyDiffAmount(billId, dataList)
                    },
                   
                };
                shr.remoteCall(options);


                // _self.remoteCall({
                //     method: 'customSubmit',
                //     param: {
                //         adminIdArray: dataList + "",
                //         billId: billId,
                //     },
                //     success: function (data) {
                //         if (data != null) {
                //             shr.showInfo({
                //                 message: "成功提交" + data + "个部门",
                //                 hideAfter: 2
                //             });
                //             setTimeout(function () {
                //                 _self.queryGrid();
                //             }, 2000); // 2000 毫秒即为两秒钟
                //         }
                //     }, error: function (response) {
                //         shr.showError({
                //             message: response.summary
                //             ,hideAfter: 2
                //         });
                //     }
                // });
            }
        })
    },
    /**
     * 校验奖金是否有出入
     */
    verifyDiffAmount: function (billId, dataList) {
        var self = this;
        var tableData = [];
        var options = {
            action: 'contrastDifference',
            uipk: this.uipk,
            type: 'POST',
            async: false,
            param: {
                billId: billId,
                adminIdArray: dataList + ''
            },
            success: function (res) {
                tableData = res;
            },
        };
        shr.remoteCall(options);
        if (tableData.length > 0) {
            var colNames = ['auditor.name', 'auditor.number', 'adminorg.name', 'entrys.lastscore', 'entrys.uplastscore', 'entrys.lastlevel', 'entrys.uplastlevel', 'entrys.scoreleader', 'entrys.score'];
            var colModels = self.getTipsColModels(colNames);
            $(self).cusDetailTips({
                tableData: tableData,
                failureCount: tableData.length,
                isSortable: true,
                callback: function () { self.submitHR(dataList, billId) }, //回调函数
                title: "绩效评级存在人为修改情况",
                piece: "条",
                colModels: colModels
            }).cusDetailTips("open");
        } else {
            self.submitHR(dataList, billId);
        }
    },
    submitHR(dataList, billId) {
        var _self = this;
        _self.remoteCall({
            method: 'customSubmit',
            param: {
                adminIdArray: dataList + "",
                billId: billId,
            },
            success: function (data) {
                if (data != null) {
                    shr.showInfo({
                        message: "成功提交" + data + "个部门",
                        hideAfter: 2
                    });
                    setTimeout(function () {
                        _self.queryGrid();
                    }, 2000); // 2000 毫秒即为两秒钟
                }
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
            }
        });
    },
    /**
     * 处理表格的pager
     */
    renderPager: function (page, pageSize) {
        var _self = this;
        $("#grid_periodicQuarter.id").hide();
    },
    /**
     * 计算得分
     */
    calculateScoreAction() {
        var _self = this;
        //操作时校验是否锁定周期
        _self.verifyisLock();
        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()]
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行计算得分！"
                , hideAfter: 2
            });
            return;
        }

        _self.remoteCall({
            method: 'submitFilterAdmin',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                if (data != null) {
                    $('#adminOrg').shrPromptBox("setFilter", "id in(" + data + ")");
                    $("#adminOrg").shrPromptBox("open");
                }
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    , hideAfter: 2
                });
            }
        });

        $("#adminOrg").shrPromptBox("option", {
            onchange: function (e, value) {
                var dataList = [];
                var currentArray = value.current;
                // 使用each方法循环遍历数组
                $.each(currentArray, function (index, value) {
                    // 在控制台打印每个元素的索引和值
                    dataList.push(value.id)
                });
                _self.remoteCall({
                    method: 'calculateScore',
                    param: {
                        adminIdArray: dataList + "",
                        billId: billId,
                    },
                    success: function (data) {
                        if (data != null) {
                            shr.showInfo({
                                message: "已成功计算得分",
                                hideAfter: 2
                            });
                            setTimeout(function () {
                                _self.queryGrid();
                            }, 2000); // 2000 毫秒即为两秒钟
                        }
                    }, error: function (response) {
                        shr.showError({
                            message: response.summary
                            , hideAfter: 2
                        });
                    }
                });
            }
        });
    }
})