import Common from '../../common/Common.js';
import { Uuid } from 'pc-component-v3';

export default {

  // 获取页签
  getTab: function (window, tabIndex) {
    var targetTab = window.tabs[tabIndex];
    return targetTab;
  },

  // 恢复窗口个页签中分组的可见性
  restoreWindowTabFieldGroupsVisible: function (window) {
    var _self = this;
    window.tabs.forEach(function (tab) {
      _self.restoreFieldGroupsVisible(tab.fieldGroups);
    });
  },

  // 恢复页签分组的可见性
  restoreFieldGroupsVisible: function (fieldGroups) {
    if (fieldGroups == undefined) {
      return;
    }
    for (var i = 0; i < fieldGroups.length; i++) {
      var state = localStorage.getItem('fieldGroupStatus' + fieldGroups[i].id);
      console.log('state: ' + state);
      if (state == 'false') {
        fieldGroups[i].isShow = false;
      } else {
        fieldGroups[i].isShow = true;
      }
    }
  },

  // 获取子页签
  getSubTab: function (window, parentTabName) {
    var subTabs = [];
    window.tabs.forEach(function (tab) {
      if (tab.parentTabName == parentTabName) {
        subTabs.push(tab);
      }
    });
    return subTabs;
  },

  // 转换子页签
  parseSubTab: function (window, tab) {
    var _self = this;
    var subTabs = this.getSubTab(window, tab.name);
    if (tab.subTabs == undefined) {
      tab.subTabs = [];
    }

    if (subTabs.length > 0) {
      subTabs.forEach(function (subTab) {
        tab.subTabs.push(subTab);
      });
    }

    if (tab.subTabs.length > 0) {
      tab.subTabs.forEach(function (subTab) {
        _self.parseSubTab(window, subTab);
      });
    }
  },
  // 获取oneToOne页签
  getOneToOneSubTabs: function (window, oneToOneParentTabName) {
    var subOneToOneTabs = [];
    window.tabs.forEach(function (tab) {
      if (tab.oneToOneParentTabName == oneToOneParentTabName) {
        subOneToOneTabs.push(tab);
      }
    });
    return subOneToOneTabs;
  },

  // 转换oneToOne页签
  parseOneToOneTab: function (window, tab) {
    var _self = this;
    var subOneToOneTabs = this.getOneToOneSubTabs(window, tab.name);
    if (tab.subOneToOneTabs == undefined) {
      tab.subOneToOneTabs = [];
    }

    if (subOneToOneTabs.length > 0) {
      subOneToOneTabs.forEach(function (subTab) {
        tab.subOneToOneTabs.push(subTab);
      });
    }

    if (tab.subOneToOneTabs.length > 0) {
      tab.subOneToOneTabs.forEach(function (oneToOneTab) {
        _self.parseOneToOneTab(window, oneToOneTab);
      });
    }
  },

  // 获取表格字段
  getGridField: function (tab) {
    if (tab && tab.tabGridView) {
      return tab.tabGridView.tabGridFields;
    }
    return null;
  },

  // 获取表格标题行字段
  getGridTitleField: function (tab) {
    if (tab && tab.tabGridView) {
      return tab.tabGridView.tabGridTitleFieldDtos;
    }
    return null;
  },

  // 获取看板字段
  getDashBoardField: function (tab) {
    if (tab && tab.tabDashboardView) {
      return tab.tabDashboardView.tabGridFields;
    }
    return null;
  },

  // 获取编辑字段
  getDetailField: function (tab) {
    var detailFields = [];
    if (tab && tab.tabFormView && tab.tabFormView.tabFormFields) {
      tab.tabFormView.fieldGroups = [];
      tab.tabFormView.tabFormFields.forEach(function (field) {
        if (field.groupName != null && field.groupName.length > 0) {
          var fieldGroup = null;
          for (var index = 0; index < tab.tabFormView.fieldGroups.length; index++) {
            if (tab.tabFormView.fieldGroups[index].groupName == field.groupName) {
              fieldGroup = tab.tabFormView.fieldGroups[index];
              fieldGroup.fields.push(field);
              if(field.groupNameEng != null && field.groupNameEng != ''){
                fieldGroup.groupNameEng = field.groupNameEng;
              }
              break;
            }
          }
          if (fieldGroup == null) {
            fieldGroup = {};
            fieldGroup.groupName = field.groupName;
            fieldGroup.groupNameEng = field.groupNameEng;
            fieldGroup.show = true;
            fieldGroup.fields = [];
            fieldGroup.fields.push(field);
            tab.tabFormView.fieldGroups.push(fieldGroup);
          }
        } else {
          detailFields.push(field);
        }
      });
    }
    //按sortNo从小到大排序
    detailFields.sort(function (fieldA, fieldB) {
      return fieldA.sortNo - fieldB.sortNo;
    });

    // var fieldGroups = [];
    // detailFields.forEach(function (detailField) {
    //   if (detailField.groupName != null && detailField.groupName.length > 0) {

    //   }
    // });

    return detailFields;
  },

  // 转换页签分组
  parseFieldGroup: function (tab) {
    // tab.fieldGroups.forEach(function(fieldGroup){
    // 	tab.fields.forEach(function(field){
    // 		if(field.fieldGroupId != undefined && field.fieldGroupId == fieldGroup.id){
    // 			if(fieldGroup.fields == undefined){
    // 				fieldGroup.fields = [];
    // 			}
    // 			fieldGroup.fields.push(field);
    // 		}
    // 	});
    // });
  },


  // 设置显示的属性
  // 设备编辑模式 false
  setViewDisplayAttribute: function (modelDatas, attributeName, attributeValue) {
    if (modelDatas == undefined) {
      return;
    }
    modelDatas.forEach(function (modelData) {
      modelData[attributeName] = attributeValue;
    });
  },

  // 克隆gridFieldItem
  clone: function (gridFieldItem) {
    var gridFieldItemClone = {
      'fieldName': gridFieldItem.fieldName,
      'index': gridFieldItem.index,
      'visible': gridFieldItem.visible,
      'width': gridFieldItem.width,
      'sortNo': gridFieldItem.sortNo,
      'entityFieldIndex': gridFieldItem.entityFieldIndex,
    };
    return gridFieldItemClone;
  },

  // 克隆field
  cloneField: function (field) {
    let fieldClone = {
      'id': field.id,
      'name': field.name,
      'fieldName': field.fieldName,
      'visible': field.visible,
      'index': field.index,
    };
    return fieldClone;
  },

  /**
	 * 打开新版CRUD视图界面
	 * @return {[type]} [description]
	 */
  openNewCurdViewWindow: function (windowOpenParam) {
    if (windowOpenParam != undefined) {
      window.open(Common.getRedirectUrl('#/desktop/window1/window-read/view/'
				+ windowOpenParam.windowNo
				+ '/'
				+ windowOpenParam.parentTabIndex
				+ '/'
				+ windowOpenParam.parentRecordId
				+ '?currIndex=1&totalCount=1&uuid=' + Uuid.createUUID()));
    }
  },

  /**
	 * 打开新版CRUD新建界面
	 * @return {[type]} [description]
	 */
  openNewCurdNewWindow: function (windowOpenParam) {
    if (windowOpenParam != undefined) {
      window.open(Common.getRedirectUrl('#/desktop/window1/window-edit/create/'
				+ windowOpenParam.windowNo
				+ '/'
				+ windowOpenParam.parentTabIndex
				+ '?uuid=' + Uuid.createUUID()));
    }
  },

  /**
	 * 转换FilterSchema，增加filedValue字段
	 */
  parseFilterSchema: function (window) {
    if (window == null || window.tabs == null) {
      return;
    }

    for (let i = 0; i < window.tabs.length; i++) {
      let tab = window.tabs[i];
      let filterSchema = tab.filterSchema;
      if (filterSchema == null || filterSchema.filterSchemaLines == null) {
        continue;
      }
      for (let j = 0; j < filterSchema.filterSchemaLines.length; j++) {
        let filterSchemaLine = filterSchema.filterSchemaLines[j];
        // 如果是搜索框的话，增加fieldValue字段
        if (filterSchemaLine.displayType == 'SearchBoxEditor') {
          filterSchemaLine.fieldValue = {
            id: null,
            displayValue: [],
            fieldType: 'key',
          };
        } else if (filterSchemaLine.displayType == 'MultiSearchBoxEditor'
					|| filterSchemaLine.displayType == 'Tree') {
          filterSchemaLine.fieldValue = {
            ids: [],
            displayValue: [],
            fieldType: 'key',
          };
        }
        if (filterSchemaLine.value1 == undefined) {
          filterSchemaLine.value1 = null;
        }
        if (filterSchemaLine.value2 == undefined) {
          filterSchemaLine.value2 = null;
        }
        // AND OR
        filterSchemaLine.isCompositeOr = false;
        // 大小写不敏感
        filterSchemaLine.isCaseSensitive = false;
        // 备份约束方式，清空的时候，恢复用
        filterSchemaLine.oldConstraintEnum = filterSchemaLine.constraintEnum;
      }
    }
  },

};