
shr.defineClass("shr.ats.configInfoEdit", shr.framework.Edit, {
    initalizeDOM: function () {
        shr.ats.configInfoEdit.superClass.initalizeDOM.call(this);
    },
    addRowAction: function () {
        var params = {}
        params.entityName = "com.kingdee.eas.basedata.person.app.Person"
        entityName.isQuery = "false"
        shr.callService({
            serviceName: 'getEntityInfoService',
            param: params,
            success: function (data) {

                if (waf.modelObjectData.className == 'waf.framework.dynamic.element.EditGrid') {
                    //编辑表格只能勾选分录下面的数据
                    var editGridName = waf.modelObjectData.getProp('name').value;
                    waf.editGridData = [];
                    data = getEditGridData(data, editGridName);
                };
                $('.fieldEditContainerLoading').remove();
                $('#fieldEditContainer').css('visibility', 'visible');
                $('.ui-dialog-titlebar .ui-icon-closethick').css('visibility', 'visible');
                $('.ui-dialog-buttonset').css('visibility', 'visible');
                waf.fieldData = [];
                $.each(configPage.allComponentChildren, function (i, v) {
                    var classNameList = ['waf.framework.dynamic.element.EditColumn', 'waf.framework.dynamic.element.Field', 'waf.framework.dynamic.element.Column',
                        'waf.framework.dynamic.element.SearchColumn', 'waf.framework.dynamic.element.F7Column'];
                    if (classNameList.indexOf(v.className) > -1) {
                        waf.fieldData.push(v);
                    }
                });
                doFieldData(data);
                addTreeFieldLocationListener();
                addEditGridNameTips();
                //阻止鼠标滚动穿透
                var eventType = 'wheel';
                //兼容火狐浏览器
                if (document.mozHidden !== undefined) {
                    eventType = 'DOMMouseScroll';
                }
                $('#gridZtree').on(eventType, function (e) {
                    var scrollTop = this.scrollTop,
                        scrollHeight = this.scrollHeight,
                        height = this.clientHeight;
                    var delta = (e.originalEvent.wheelDelta) ? e.originalEvent.wheelDelta : -(e.originalEvent.detail || 0);
                    if ((delta > 0 && scrollTop <= delta) || (delta < 0 && scrollHeight - height - scrollTop <= -1 * delta)) {
                        this.scrollTop = delta > 0 ? 0 : scrollHeight;
                        e.preventDefault();
                    }
                })
            }
        });



        // 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);
    }
})
//获取编辑表格数据
function getEditGridData(data, editGridName) {
    if (data && data.length > 0) {
        $.each(data, function (i, v) {
            if (v.field === editGridName) {
                waf.editGridData = v;
                return false;
            } else {
                getEditGridData(v.children, editGridName);
            };
        });
    };
    return waf.editGridData;
};
function doFieldData(data) {
    var isClass = '';
    //兼容IE
    var Ua = navigator.userAgent;
    if (Ua.indexOf('MSIE') > -1 || Ua.indexOf('rv:11.0') > -1 || Ua.indexOf('Edge') > -1) {
        isClass = 'accruateSwitchIE';
    } else {
        isClass = 'accruateSwitch';
    }

    $('#fieldEditContainer').html(
        "<div class='treeSearch-con'>" +
        "<div class='search-container'>" +
        "<img src='/shr/styles/images/set_field_location_search.png' class='search-icon'/>" +
        "<input type='text' placeholder='请输入字段标题、类型、名称' id='searchTree'/>" +
        "<div style='float:left;'><button id='tree-searchBtn' >搜索</button></div>" +
        "<div class='accurateTitle'>精确搜索</div>" +
        "<div class='accruate-container'>" +
        "<input type='checkbox' class='" + isClass + " switch-anim'>" +
        "</div>" +
        "</div>" +
        "<div class='showNumber'>" +
        "<label id='treeSearchCount' style='display:none'>" + '共查到' + '&nbsp;0&nbsp;' + "条信息</label>" +
        '<span id="treePrevSearchTarget" style="display:none;" >上一条</span>' +
        '<span id="treeNextSearchTarget" style="display:none;" >下一条</span>' +
        '</div>' +
        "</div>" +
        "<ul id='gridZtree' class='ztree'></ul>"
    );
    var $zTree = $('#gridZtree.ztree');
    waf.removeFields = [];
    //zTree配置
    var zTreeSettings = {
        view: {
            showIcon: false,
            showLine: false,
            dblClickExpand: false,
            addDiyDom: addDiyDom
        },
        check: {
            enable: true,
            chkboxType: { 'Y': '', 'N': '' },
            autoCheckTrigger: true,
            chkStyle: "checkbox"
        },
        callback: {
            onCheck: function (event, treeId, treeNode) {
                //勾选复选框后，列表字段相应修改逻辑  
                var containerId = $('#fieldEditContainer').attr('containerId');
                if (!treeNode.checked) {
                    //先进先出原则
                    var removeField = {};
                    removeField.containerId = containerId;
                    removeField.fieldId = treeNode.field;
                    if (!treeNode.field && treeNode.children.length > 0) {
                        $.each(treeNode.children, function (index, item) {
                            removeField = $.extend(true, {}, removeField);
                            removeField.fieldId = item.field;
                            waf.removeFields.push(removeField);
                        });
                    } else {
                        waf.removeFields.push(removeField);
                    };

                } else {
                    for (var i = waf.removeFields.length - 1; i >= 0; i--) {
                        value = waf.removeFields[i];
                        if (value.containerId == containerId && value.fieldId == treeNode.field) {
                            waf.removeFields.remove(value);
                        };
                    };
                };
            },
            onNodeCreated: function (event, treeId, treeNode) {
                var className = waf.modelObjectData.className;
                if (treeNode && treeNode.level == 0 && className == 'waf.framework.dynamic.element.EditGrid') {
                    //分录数据默认展开第二级树节点	
                    window.gridZtree.getZTreeObj(treeId).expandNode(treeNode, true);
                };
            }
        }
    };
    // $.fn.zTree.init($zTree, zTreeSettings, data);
    window.gridZtree.init($zTree, zTreeSettings, data);
    //字段配置数据来源提示信息
    var dataSourceTips = $('#fieldEditContainer').attr('dataSourceTips');
    if (dataSourceTips) {
        var dataSourceHtml = "<div style='float:left;width:540px;margin:-3px 0 0 5px;color:red;word-break:break-all;line-height:20px;'>" + dataSourceTips + "</div>";
        $('.ui-dialog-buttonpane.ui-widget-content').prepend(dataSourceHtml);
    };
    $('#fieldEditContainer').dialog('open');
    $('#waf-mask img').remove();
    var model = waf.modelObjectData;
    //将字段配置容器的modelId挂载到fieldEditContainer的containerId自定义属性中
    $('#fieldEditContainer').attr('containerId', model.modelId);
    var modelChildrenObjects;
    if (model.className == 'waf.framework.dynamic.element.Section' || model.className == 'waf.framework.dynamic.page.ConfigPageCanvas') {
        modelChildrenObjects = configPage.allComponentChildren.filter(function (v, i) {
            return v.className == 'waf.framework.dynamic.element.Field';
        });
    } else {
        modelChildrenObjects = model.childrenObjects;
    };
    $.each(modelChildrenObjects, function (index, value) {
        var treeId = 'gridZtree';
        var tree = window.gridZtree.getZTreeObj(treeId);
        var treeNode;
        var fieldId = value.fieldName;
        if (value.className == 'waf.framework.dynamic.element.EditColumn') {
            if (!fieldId.startsWith(editName)) {
                fieldId = editName + '.' + fieldId;
            };
        };
        //根据field选择出节点名字
        treeNode = tree.getNodeByParam('field', fieldId);

        if (treeNode) {
            tree.checkNode(treeNode, true, false);
            //如果为扩展视图，标准产品预置出厂字段不允许删除，故将其勾选状态设置为不可编辑
            if (waf.isStandardFieldList.indexOf(value.modelId) > -1 && uiViewInfo.isProductionMode) {
                tree.setChkDisabled(treeNode, true);
            };
        }
    });

};

function addEditGridNameTips() {
    var className = waf.modelObjectData.className;
    if (className == 'waf.framework.dynamic.element.EditGrid') {
        var editGridName = waf.modelObjectData.getProp('name').value;
        if (editGridName === '') {
            $('#fieldEditContainer').html('<div style="font-size:14px;margin-top:100px;">编辑表格editGrid的name值为空，请先填写编辑表格的name值！</div>');
        } else if (waf.editGridData.length == 0) {
            $('#fieldEditContainer').html('<div style="font-size:14px;margin-top:100px;">请检查编辑表格的name值是否正确！</div>');
        };
    };
};


//树的字段添加定位监听
function addTreeFieldLocationListener() {
    var _self = this;
    $('#tree-searchBtn').unbind('click').bind('click', function (event) {
        event.preventDefault();
        searchTree();
        return false;
    });
    $('#searchTree').unbind('keydown').bind('keydown', function (event) {
        if (event.keyCode == 13) {
            searchTree();
            event.stopPropagation();
            return false;
        }
    });
    //每次加载时先解除绑定，在重新绑定方法，避免多次绑定
    $('#treePrevSearchTarget').unbind('click').bind('click', function () {
        var targetDom = _self.targetTreeNodeDom;
        if (_self.index == 0) return;
        targetDom[_self.index].css('background-color', '#EDF7FF');
        targetDom[_self.index].focus();
        _self.index--;
        targetDom[_self.index].css('background-color', '#D8E6FF');
        var leng = targetDom[_self.index][0].offsetTop - 240;
        $('#gridZtree').animate({ scrollTop: leng }, 0);
    });
    //每次加载时先解除绑定，在重新绑定方法，避免多次绑定
    $('#treeNextSearchTarget').unbind('click').bind('click', function () {
        var targetDom = _self.targetTreeNodeDom;
        if (_self.index + 1 == targetDom.length || targetDom.length == 0) {
            return;
        }
        targetDom[_self.index].css('background-color', '#EDF7FF');
        _self.index++;
        targetDom[_self.index].css('background-color', '#D8E6FF');
        var leng = targetDom[_self.index][0].offsetTop - 240;
        $('#gridZtree').animate({ scrollTop: leng }, 0);
    });
};
//树结构定位、回车时，搜索出目标字段
function searchTree() {
    var _self = this;
    var treeId = 'gridZtree';
    //window.gridZtree.getZTreeObj,$.fn.zTree.getZTreeObj()
    var tree = window.gridZtree.getZTreeObj(treeId);
    var content = $('#searchTree').val();//目标字段
    var targetCount = 0;
    _self.targetTreeNodeDom = [];
    _self.treeDom = [];
    _self.index = 0;//定位到第几项
    tree.expandAll(true);
    //模糊匹配id以 gridZtree_开头的span标签,[id^=gridZtree_]
    $('ul#gridZtree li a span').each(function () {
        var parentLiDom = $(this).closest('li');
        // if(parentLiDom.find('ul').length > 0) return true;
        if (content == '') {
            parentLiDom.css('background-color', '');
            return true;
        }
        var str = $(this).text();
        //用户输入类型和名称对应值也要搜索出来，过滤【类型:F7; 名称:entrys.employerUnit】
        var arr = str.split(';');
        if (arr.length > 1) {
            str = '';
            $.each(arr, function (index, item) {
                var items = item.split(':');
                items[1] = items[1].indexOf('】') > -1 ? items[1].substring(0, items[1].length - 1) : items[1];
                str += items[1] + ',';
            });
        }
        content = content.toLowerCase();
        str = str.toLowerCase();
        var target = new RegExp(content);
        //判断是否是精确搜索,若是精确搜索不能使用test()匹配,非精确搜索使用test()匹配
        if ($('.switch-anim').prop('checked')) {
            var str1 = str.split(',');
            if (str1.length > 1) {
                str = str1[1];
            } else {
                str = str
            }
        }
        var targetValue = $('.switch-anim').prop('checked') ? (content == str) : target.test(str);
        if (targetValue) {
            if (parentLiDom.find('ul').length > 0) {
                parentLiDom.children('a').css('background-color', '#EDF7FF');
                //用一个数组判断插入的dom节点是否重复
                if (_self.treeDom.indexOf(parentLiDom.children('a')[0]) == -1) {
                    _self.treeDom.push(parentLiDom.children('a')[0]);
                    _self.targetTreeNodeDom.push(parentLiDom.children('a'));
                    targetCount++;
                }
            } else {
                parentLiDom.children('a').css('background-color', '');
                parentLiDom.css('background-color', '#EDF7FF');
                //用一个数组判断插入的dom节点是否重复
                if (_self.treeDom.indexOf(parentLiDom[0]) == -1) {
                    _self.treeDom.push(parentLiDom[0]);
                    _self.targetTreeNodeDom.push(parentLiDom);
                    targetCount++;
                }
            }
        } else {
            parentLiDom.children('a').css('background-color', '');
            parentLiDom.css('background-color', '');
        }
    });

    if (targetCount > 0) {
        $('#treeSearchCount').css('display', '');
        $('#treePrevSearchTarget').css('display', '');
        $('#treeNextSearchTarget').css('display', '');
    } else {
        tree.expandAll(false);
        _self.targetTreeNodeDom = [];
        _self.treeDom = [];
        $('#treeSearchCount').css('display', '');
        $('#treePrevSearchTarget').css('display', '');
        $('#treeNextSearchTarget').css('display', '');
    }
    if (_self.targetTreeNodeDom.length > 0) {
        _self.targetTreeNodeDom[0].css('background-color', '#D8E6FF');
        //查询到结果，定位到第一条，获取第一条数据的offsetTop时，树结构全部展开后，再设置滚动条的位置
        setTimeout(function () {
            var leng = _self.targetTreeNodeDom[0][0].offsetTop - 240;
            $('#gridZtree').animate({ scrollTop: leng }, 0);
        }, 200);
    } else {
        $('#gridZtree').animate({ scrollTop: 0 });
    }
    $('#treeSearchCount').text('共查到 ' + targetCount + ' 条信息');
};
//树的字段添加定位监听
function addTreeFieldLocationListener() {
    var _self = this;
    $('#tree-searchBtn').unbind('click').bind('click', function (event) {
        event.preventDefault();
        searchTree();
        return false;
    });
    $('#searchTree').unbind('keydown').bind('keydown', function (event) {
        if (event.keyCode == 13) {
            searchTree();
            event.stopPropagation();
            return false;
        }
    });
    //每次加载时先解除绑定，在重新绑定方法，避免多次绑定
    $('#treePrevSearchTarget').unbind('click').bind('click', function () {
        var targetDom = _self.targetTreeNodeDom;
        if (_self.index == 0) return;
        targetDom[_self.index].css('background-color', '#EDF7FF');
        targetDom[_self.index].focus();
        _self.index--;
        targetDom[_self.index].css('background-color', '#D8E6FF');
        var leng = targetDom[_self.index][0].offsetTop - 240;
        $('#gridZtree').animate({ scrollTop: leng }, 0);
    });
    //每次加载时先解除绑定，在重新绑定方法，避免多次绑定
    $('#treeNextSearchTarget').unbind('click').bind('click', function () {
        var targetDom = _self.targetTreeNodeDom;
        if (_self.index + 1 == targetDom.length || targetDom.length == 0) {
            return;
        }
        targetDom[_self.index].css('background-color', '#EDF7FF');
        _self.index++;
        targetDom[_self.index].css('background-color', '#D8E6FF');
        var leng = targetDom[_self.index][0].offsetTop - 240;
        $('#gridZtree').animate({ scrollTop: leng }, 0);
    });
};