/**
 * 专员维护考核人和评分人页面(专员) 动态列表专员操作
 */
shr.defineClass("shr.custom.ExamineEntryZYDynamicList", shr.custom.CustomDynamicList, {
    //可编辑列 数组(不区分大小写)
    editColumns: [],
    isSum: false,
    initalizeDOM: function () {
        var _self = this;
        //新增编辑列
        this.editColumns.push("entrys.scoreleader");
        this.editColumns.push("entrys.remark");
        this.editColumns.push("adminorg.name");
        this.editColumns.push("auditor.name");
        this.editColumns.push("scorer.name");
        shr.custom.ExamineEntryZYDynamicList.superClass.initalizeDOM.call(this);
        _self.initializeFilterAdminOrg();

    },
    /**
     * 催办
     */
    prassAction() {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        var _self = this;
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        _self.remoteCall({
            method: 'prass',
            param: {
                billId: billId,
            },
            async: false,
            success: function (data) {
                shr.showInfo({
                    message: data,
                    hideAfter:2
                });
            }
        });
    },
    //初始化过滤组织
    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
                });
            }
        });
    },
    /**
 * 描述: 导入action
 */
    importAction: function () {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        var billId = shr.getUrlRequestParam("examineGradePersonId");
        var customParam = { parentId: billId };
        this.doImportData('import', customParam);
    },
    /**
     * 删除
     */
    deleteAction() {

        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        var $grid = $(this.gridId);
        var selectedIds = $grid.jqGrid("getSelectedRows");
        if (selectedIds.length > 0) {
            shr.showConfirm($.shrI18n.common.tips.deleteConfirm, function () {
                _self.remoteCall({
                    method: 'customDelete',
                    param: {
                        idArray: selectedIds + "",
                    },
                    success: function (data) {
                        shr.showInfo({
                            message: "刪除成功",
                            hideAfter: 2
                        });
                        setTimeout(function () {
                            _self.queryGrid();
                        }, 2000); // 2000 毫秒即为两秒钟
                    }, error: function (response) {
                        shr.showError({
                            message: response.summary,
                            hideAfter: 2
                        });
                    }
                });

            });
        } else {
            shr.showWarning({
                message: $.shrI18n.common.tips.chooseTableDataFirst
            });
        }
    },
    /**
        * 组装保存时传至服务端的数据
        */
    assembleSaveData: function (action) {
        var _self = this;
        //组装需要修改的数据
        var data = [];
        var meta;
        var id, column, value, cur, element, personCode, datatype;
        var tds = $("#grid td[class*='dirty-cell']");
        var $table = $("#grid")[0];

        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.column = column;
            meta.value = value;
            meta.type = datatype;
            data.push(meta);
        }
        return data;
    },
    // 根据动态列表配置 取业务主键
    getBillIdFieldName: function () {
        return 'PERIODICQUARTER.id';
    },
    /**
     * 处理表格的pager
     */
    renderPager: function (page, pageSize) {
        var _self = this;
        $("#grid_periodicQuarter.id").hide();
    },
    /**
     *反启用排序
     */
    closeOrderAction() {

        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        var $grid = $(this.gridId);
        var selectedIds = $grid.jqGrid("getSelectedRows");
        if (selectedIds.length > 0) {

            _self.remoteCall({
                method: 'closeOrder',
                param: {
                    entryId: selectedIds + "",
                },
                success: function (data) {
                    shr.showInfo({
                        message: "反启用成功",
                        hideAfter: 2
                    });
                    setTimeout(function () {
                        _self.queryGrid();
                    }, 2000); // 2000 毫秒即为两秒钟
                }, error: function (response) {
                    shr.showError({
                        message: response.summary,
                        hideAfter: 2
                    });
                }
            });
        } else {
            shr.showWarning({
                message: $.shrI18n.common.tips.chooseTableDataFirst
            });
        }
    },
    /**
     * 启动排序
     */
    startOrderAction() {
        var _self = this;

        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }

        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()]

        _self.remoteCall({
            method: 'startOrder',
            param: {
                billId: billId,
            },
            success: function (data) {
                shr.showInfo({
                    message: "已启动排序",
                    hideAfter: 2
                });
                setTimeout(function () {
                    _self.queryGrid();
                }, 2000); // 2000 毫秒即为两秒钟
            }, error: function (response) {
                shr.showError({
                    message: response.summary,
                    hideAfter: 2
                });
            }
        });
    },
    /**
    * 组织维护得分表
    */
    performanceScoreOrgAction() {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        //获取周期季度ID
        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()]
        var examineGradePersonId = shr.getUrlRequestParam("examineGradePersonId");
        this.reloadPage({
            uipk: "PerformanceScore.form",
            billId: billId,
            examineGradePersonId: examineGradePersonId,
            method: 'edit'
        });
    },
    /**
     * 锁定周期
     */
    periodLockAction() {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
                ,hideAfter: 2
            });
            return;
        }
        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()]
        _self.remoteCall({
            method: 'periodLock',
            param: {
                billId: billId,
            },
            success: function (data) {
                shr.showInfo({
                    message: "已锁定周期",
                    hideAfter: 2
                });
                setTimeout(function () {
                    _self.queryGrid();
                }, 2000); // 2000 毫秒即为两秒钟
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    ,hideAfter: 2
                });
            }
        });
    },
    /**
     * 解除周期
     */
    periodUnlockAction() {
        var _self = this;
        if (_self.checkEditCell()) {
            shr.showWarning({
                message: "请先保存数据，再进行操作！"
            });
            return;
        }
        var periodicQuarterId = "periodicQuarter.id";
        var billId = $('#grid').jqGrid('getOriginalRowData')[0][periodicQuarterId.toLowerCase()]

        _self.remoteCall({
            method: 'periodUnlock',
            param: {
                billId: billId,
            },
            success: function (data) {
                shr.showInfo({
                    message: "已解除周期",
                    hideAfter: 2
                });
                setTimeout(function () {
                    _self.queryGrid();
                }, 2000); // 2000 毫秒即为两秒钟
            }, error: function (response) {
                shr.showError({
                    message: response.summary
                    ,hideAfter: 2
                });

            }
        });
    },
        /**
     * 	单元格点击事件触发
     * @param {*} rowid 
     * @param {*} colIndex 
     * @param {*} cellcontent 
     * @param {*} e 
     * @returns 
     */
        onCellSelect: function (rowid, colIndex, cellcontent, e) {
            shr.custom.ExamineEntryZYDynamicList.superClass.onCellSelect.call(this);
            // 选择的是选择框
            if (colIndex != 0) {
                var state = $("#grid").jqGrid("getCell", rowid, "entrys.orderstatuc");
                if (2!= state&&(columnName == "adminorg.name"||columnName == "auditor.name"||columnName == "scorer.name")) {
                    //考核人已提交状态单据行不能编辑
                    $("#grid").jqGrid("setGridParam", { cellEdit: false })
                } else {
                    //考核人已提交状态单据行能编辑
                    $("#grid").jqGrid("setGridParam", { cellEdit: true })
                }
                return;
            }
        },
        /**
         * 表格加载完成后操作，供子类复写
         */
        afterLoadComplete: function (ret) {
            //当考核人已提交状态 编辑列单元格背景色显示白色
            if (ret.records > 0) {
                for (let i = 0; i < ret.rows.length; i++) {
                    var rowData = ret.rows[i];
                    var state = rowData["entrys.orderstatuc"];
                    if (2!= state.value) {
                        //考核人已提交状态
                        var fid = rowData['entrys.id'];
                        var tr = $('tr[id="' + fid + '"]')
                        var tds = tr.find('.cellEditItem');
                        for (var j = 0; j < tds.length; j++) {
                            var describedby = tds[j].getAttribute('aria-describedby').split("grid_")[1];
                            if (describedby == "entrys.scoreleader" || "entrys.remark" == describedby) {
                                continue;
                            }
                            $(tds[j]).removeClass('cellEditItem')
                        }
                    }
                }
            }
        },
})