import Common from '../../common/Common.js';
import UserStorageResource from '../../api/base/UserStorageResource.js';
import { Notify } from 'pc-component-v3';

export default {


  /**
	 * 克隆表格字段
	 * @param {表格字段} GridField 
	 */
  cloneGridField: function (GridField) {
    var GridFieldClone = {
      'fieldName': GridField.fieldName,
      'displayName': GridField.displayName,
      'visible': GridField.visible,
      'entityFieldIndex': GridField.entityFieldIndex,
      'mandatory': GridField.mandatory,
      'width': GridField.width,
      'sortNo': GridField.sortNo,
    };
    return GridFieldClone;
  },


  /**
	 * 保存表格字段的显示属性至UserStorage
	 * @param { string } windowNo 窗口编号
	 * @param { int } tabIndex 页签编号
	 * @param { Array } gridFields 表格字段
	 */
  saveGridFieldDef: function (windowNo, tabIndex, gridFields) {
    var key = 'TabGridField_' + windowNo + '_' + tabIndex;
    var userStorageDtos = [
      {
        key: key,
        value: JSON.stringify(gridFields),
      },
    ];

    return new Promise(function (resolve, reject) {
      UserStorageResource.uploadUserStorage(userStorageDtos).then(successData => {
        if (successData.errorCode == 0) {
          resolve();
        } else {
          reject(successData.errorMessage);
        }
      }, errorData => {
        reject(errorData);
      });
    });
  },



  /**
	 * 从UserStorage中恢复所有表格字段的显示属性
	 * @param {窗口编号} windowNo 
	 * @param {页签编号} tabIndex 
	 * @param {表格字段} GridField 
	 */
  restoreTabGridFieldDef: function (windowNo, tabIndex, tabGridFields) {
    var key = 'TabGridField_' + windowNo + '_' + tabIndex;
    if (tabGridFields != null) {
      UserStorageResource.uniqueByKey(key).then(successData => {
        // if(successData.errorCode != 0) {
        //   Notify.error('提示', successData.errorMessage, false);
        //   return;
        // }
        var tabGridFields1 = null;
        if (successData.data != null) {
          tabGridFields1 = JSON.parse(successData.data);
        } else {
          tabGridFields1 = null;
        }

        let index = 0;
        if (tabGridFields1 != null && tabGridFields1 != undefined) {
          tabGridFields.forEach(tabGridField => {
            tabGridFields1.forEach(tabGridFieldClone => {
              if (tabGridFieldClone != null &&
								tabGridField.fieldName == tabGridFieldClone.fieldName &&
								tabGridField.entityFieldIndex == tabGridFieldClone.entityFieldIndex) {
                tabGridField.visible = tabGridFieldClone.visible;
                if (tabGridFieldClone.sortNo != undefined) {
                  tabGridField.sortNo = tabGridFieldClone.sortNo;
                }
                tabGridField.width = tabGridFieldClone.width;
              }
              if (tabGridField.sortNo == undefined) {
                tabGridField.sortNo = index;
              }
              if (tabGridField.width == undefined) {
                tabGridField.width = 150;
              }
              index ++;
            });
          });
          tabGridFields.sort(function (item1, item2) {
            return item1.sortNo - item2.sortNo;
          });
        } else {
          tabGridFields.forEach(tabGridField => {
            if (tabGridField.sortNo == undefined) {
              tabGridField.sortNo = index;
            }
            if (tabGridField.width == undefined) {
              tabGridField.width = 150;
            }
            index ++;
          });
          tabGridFields.sort(function (item1, item2) {
            return item1.sortNo - item2.sortNo;
          });
        }
      }, errorData => {
        Common.processException(errorData);
      });
    }
  },
};