
shr.defineClass("shr.ats.mappingFieldEdit", shr.framework.Edit, {
    data: [],
    initalizeDOM: function () {
        shr.ats.mappingFieldEdit.superClass.initalizeDOM.call(this);
        var _this = this
        this.data = $("#entrys").jqGrid("getAllRowData");
        var method = shr.getUrlRequestParam("method", window.location.href);
        if (method === "edit" || method === "addNew") {
            var dataSource = $('#dataSource').shrPromptBox("getValue");
            if (dataSource.id == null || dataSource.id == "" || dataSource.id == undefined) {
                shr.execByFieldIdAndMethod('dataInterface', 'addRules', { 'required': true });
                $('#dataInterface').shrTextField('enable');

            } else {
                shr.execByFieldIdAndMethod('dataInterface', 'addRules', { 'required': false });
                $('#dataInterface').shrTextField('disable');
            }
            $("[class='editGrid-toolbar']").append('<button id="addAllEntry" type="button" name="addAllEntry" style="display:Initial" class=" shrbtn" data-isenableexcelpwd="false">批量新增</button>')

            $(document).on('click', '#addAllEntry', function () {
                var eSignFile = $('#eSignFile').shrPromptBox("getValue");
                var filter = "Parent1.Parent.id = '" + eSignFile.id + "'"
                $('#eField').shrPromptBox("setFilter", filter);
                _this.data = $("#entrys").jqGrid("getAllRowData");

                var thisData = [];
                for (let i = 0; i < _this.data.length; i++) {
                    thisData.push(_this.data[i].eField)
                }
                $('#eField').shrPromptBox("setValue", thisData);
                $("#eField").shrPromptBox("open");
            });
            $('#eField').shrPromptBox('option', {
                onchange: function (a, b) {
                    var lodData = [];
                    var d = $("#entrys").jqGrid("getAllRowData")
                    for (let i = 0; i < d.length; i++) {
                        lodData.push(d[i].eField)
                    }
                    var cData = b.current
                    for (let i = 0; i < cData.length; i++) {
                        var isOut = true;
                        for (let j = 0; j < lodData.length; j++) {
                            if (cData[i].id === lodData[j].id) {
                                isOut = false
                                continue;
                            }
                        }
                        if (isOut) {
                            console.log(cData[i])
                            var rowData = {};
                            rowData.eFieldMark = cData[i].templateFieldId
                            rowData.eFieldName = cData[i].templateFieldName
                            rowData.eField = cData[i]
                            $('#entrys').jqGrid('addRowData', null, rowData, 'last', null)
                        }
                    }
                }

            })
        }
        this.changeEntrys()


    },
    changeEntrys: function () {
        var _this = this
        $('#entrys').jqGrid('option', {
            afterSaveCell: function (rowid, cellname, value, iRow, iCol) {
                // 这里可以检查单元格的值是否改变，并执行相应的操作
                if (cellname === "eField") {
                    $('#entrys').jqGrid('setCell', rowid, "eFieldMark", value.templateFieldId);
                    $('#entrys').jqGrid('setCell', rowid, "eFieldName", value.templateFieldName);
                    $('#entrys').jqGrid('saveRow', rowid, false, 'clientArray');
                }
            },
            afterEditCell: function (rowid, cellname, value, iRow, iCol) {
                // 这里可以检查单元格的值是否改变，并执行相应的操作
                if (cellname === "eField") {
                    var eSignFile = $('#eSignFile').shrPromptBox("getValue");
                    var filter = "Parent1.Parent.id = '" + eSignFile.id + "'"
                    var eFieldMark = ""
                    for (let i = 0; i < _this.data.length; i++) {
                        var temp = _this.data[i].eFieldMark;
                        if (temp != null && temp != undefined && temp != "") {
                            eFieldMark += "'" + temp + "',"
                        }
                    }
                    if (eFieldMark.length > 0) {
                        eFieldMark = eFieldMark.substring(0, eFieldMark.length - 1)
                        eFieldMark = "(" + eFieldMark + ")"
                        filter += " and templateFieldId not in " + eFieldMark
                    }
                    $('#' + iRow + '_eField').shrPromptBox("setFilter", filter);
                    console.log(value)

                } else if (cellname === "dataSourceField") {
                    var dataSource = $('#dataSource').shrPromptBox("getValue");
                    $('#' + iRow + '_dataSourceField').shrPromptBox("setFilter", "Parent.id = '" + dataSource.id + "'");
                }
            }
        })
        var method = shr.getUrlRequestParam("method", window.location.href);
        if (method === "edit" || method === "addNew") {
            $('#dataSource').shrPromptBox('option', {
                onchange: function (a, b) {
                    // 这里可以检查单元格的值是否改变，并执行相应的操作
                    if (b.current == null) {
                        shr.execByFieldIdAndMethod('dataInterface', 'addRules', { 'required': true });
                        $('#dataInterface').shrTextField('enable');

                    } else {
                        shr.execByFieldIdAndMethod('dataInterface', 'addRules', { 'required': false });
                        $('#dataInterface').shrTextField('disable');
                    }
                }
            })
        }

    },
    addRowAction: function (event) {
        this.data = $("#entrys").jqGrid("getAllRowData");
        var source = event.currentTarget,
            $editGrid = this.getEditGrid(source);

        var data = this.createNewEntryModel();
        if (typeof data === 'undefined') {
            data = {};
        }

        var editGridCont = this._getEditGridCont(source);
        if (editGridCont.data('editType') == 'inline') {
            // 表格内编辑
            $editGrid.jqGrid('addRow', { data: data });
        } else {
            $editGrid.wafGrid('addForm');
        }

        var event = document.createEvent('HTMLEvents');
        event.initEvent("editComplete_" + $editGrid.attr("id"), true, true);
        event.eventType = 'message';
        document.dispatchEvent(event);
    }

})
