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

/**
 * 过滤方案
 */
export default {

  /**
	 * 克隆过滤字段
	 * @param {过滤字段} filterField 
	 */
  cloneFilterSchemaLine: function (filterSchemaLine) {
    var filterSchemaLineClone = {
      'fieldName': filterSchemaLine.fieldName,
      'name': filterSchemaLine.name,
      'visible': filterSchemaLine.visible,
      'sortNo': filterSchemaLine.sortNo,
    };
    return filterSchemaLineClone;
  },


  /**
	 * 恢复过滤字段的属性
	 * @param {窗口} windowNo 
	 * @param {页签序号} tabIndex 
	 * @param {过滤字段} filterSchemaLine 
	 */
  restoreFilterSchemaLine: function (windowNo, tabIndex, filterSchemaLines) {
    var key = 'FilterSchemaLine_' + windowNo + '_' + tabIndex;

    return new Promise((resolve, reject) => {
      UserStorageResource.uniqueByKey(key).then(str => {
        // if(str.errorCode != 0) {
        //   Notify.error('提示', str.errorMessage, false);
        //   return;
        // }
        if (str.data != null) {
          var savedDatas = JSON.parse(str.data);

          filterSchemaLines.forEach(function (filterSchemaLine) {
            for (let index = 0; index < savedDatas.length; index++) {
              if (savedDatas[index].fieldName === filterSchemaLine.fieldName) {
                filterSchemaLine.visible = savedDatas[index].visible;
                filterSchemaLine.sortNo = savedDatas[index].sortNo;
              }
            }
          });
        }
        resolve();
      }, errorData => {
        reject(errorData);
      });
    });

  },

  /**
	 * 将更改的过滤字段字段属性保存到UserStorage
	 */
  filterSchemaLinePropertyChanged: function (windowNo, tabIndex, filterSchemaLines) {
    var key = 'FilterSchemaLine_' + windowNo + '_' + tabIndex;

    let userStorageDtos = [
      {
        key: key,
        value: JSON.stringify(filterSchemaLines),
      },
    ];

    return new Promise((resolve, reject) => {
      // 组装查询条件，然后放到后台数据库当中
      UserStorageResource.uploadUserStorage(userStorageDtos).then(successData => {
        resolve();
      }, errorData => {
        reject(errorData);
      });
    });
  },
};