import InfoWindowResource from '../api/dic/InfoWindowResource.js';

/**
 * 搜索框控件服务类
 */
export default {

  /**
     * 获取查询窗口的数据
     * @param column 列定义
     * @param infoWindowNo 查询窗口定义
     * @param tabGridField 表格字段定义
     */
  loadInfoWindowData: function (column, infoWindowNo, tabGridField) {
    var _self = this;

    column.handsontable = {
      colHeaders: [],
      columns: [],
      autoColumnSize: true,
      getValue: function () {
        var selection = this.getSelectedLast();
        var selectedData = this.getSourceDataAtRow(selection[0]);
        return selectedData.id;
      },
      language: 'zh-CN',
    };

    let column1 = column;
    return new Promise((resolve, reject) => {
      InfoWindowResource.uniqueByNo(infoWindowNo).then(response => {
        if(response.errorCode == 0){
          const infoWindowDto = response.data;
          if (infoWindowDto != null && infoWindowDto.infoGridFields != null) {
            var infoGridFields = infoWindowDto.infoGridFields;
            for (var gridIndex = 0; gridIndex < infoGridFields.length; gridIndex++) {
              var infoGridField = infoGridFields[gridIndex];
              column1.handsontable.colHeaders.push(infoGridField.name);
              var columnDef = {
                data: _self.infoWindowFieldValue(infoGridField.fieldName),
              };
              column1.handsontable.columns.push(columnDef);
            }
  
            var whereClause = tabGridField.whereClause;
  
            column1.handsontable.source = function(modelData, text){
              return _self.source(infoWindowDto, whereClause, null, modelData, text);
            };
  
            _self.source(infoWindowDto, whereClause, null, null, '').then(successData => {
              column1.handsontable.data = successData;                        
              resolve();
            }, errorData => {
              reject(errorData);
            });
  
          } else {
            reject();
          }
        }else{
          reject(response.errorMessage);
        }
      });
    });
  },



  /**
     * 查询窗口 HandsonTable 获取列的显示内容
     */
  infoWindowFieldValue: function (attr) {
    return function (row, value) {
      if (attr == 'id') {
        return row.id;
      } else {
        return row.data[attr].displayValue[0];
      }
    };
  },

  /**
     * 输入条件发生改变以后，进行数据的查询
     * 重新进行查询
     * @param {查询窗口DTO} infoWindowDto 
     * @param {额外的查询条件} whereClause 
     * @param {父页签数据} parentModelData 
     * @param {当前页签数据} modelData 
     * @param {输入的文本} text 
     */
  source: function(infoWindowDto, whereClause, parentModelData, modelData, text){
    var _self = this;

    var infoQueryParam = {
      'infoWindowNo': infoWindowDto.no,
      'start': 0,
      'length': 30,
      'isSearchWidget' : true,
      'whereClause': whereClause,
      'parentModelData': parentModelData,
      'modelData': modelData,
    };

    var values = [];

    infoWindowDto.infoFilterFields.forEach(function (item) {
      if (item.displayType == 'TextEditor' && item.constraintEnum != 'Between') {
        var value = {
          fieldName : item.fieldName,
          value1 : text,
        };
        values.push(value);
      }
    });

    infoQueryParam.infoFilterFieldValues = values;

    return new Promise((resolve, reject) => {
      InfoWindowResource.queryInfoWindowDataSimple(infoQueryParam).then(response => {
        if(response.errorCode != 0){
          reject(response.errorMessage);
          return;
        }
        const successData = response.data;
        if (successData != null && successData.dataList != null) {
          resolve(successData.dataList);
        }else{
          resolve([]);
        }
      }, errorData => {
        reject(errorData);
      });
    });
  },
};