
import Common from '../common/Common.js';
/**
 * 选择控件服务类
 */
export default {

  /**
     * 加载下拉框的数据
     * @param column 列定义
     * @param infoWindowNo 查询窗口定义
     * @param tabGridField 表格字段定义
     */
  loadSelectData: function (column, infoWindowNo, tabGridField) {
    var _self = this;

    column.handsontable = {
      colHeaders: [
        tabGridField.displayName,
      ],
      columns: [
        {
          data: 'value',
        },
      ],
      autoColumnSize: true,
      getValue: function () {
        var selection = this.getSelectedLast();
        var selectedData = this.getSourceDataAtRow(selection[0]);
        if (selectedData != null && selectedData.key != null) {
          return selectedData.key;
        } else {
          return null;
        }
      },
      language: 'zh-CN',
      data: tabGridField.keyValues,
    };

    let column1 = column;
    return new Promise((resolve, reject) => {
      resolve();
    });
  },


  /**
         * 刷新选择框数据
         * @return {void} 
         */
  refreshField: function (tabGridField, windowNo, tabIndex, modelData) {
    var _self = this;
    var obj = {
      fieldName: tabGridField.fieldName,
      windowNo: windowNo,
      tabIndex: tabIndex,
      modelData: modelData,
    };

    $.ajax({
      url: Common.getApiURL('FieldResource/refreshField2'),
      async: false,
      dataType: 'json',
      type: 'post',
      contentType: 'application/json',
      data: JSON.stringify(obj),
      beforeSend: function (request) {
        Common.addTokenToRequest(request);
      },
      success: function (data) {
        if (data != undefined && data.keyValues != null) {
          tabGridField.keyValues.splice(0, tabGridField.keyValues.length);
          data.keyValues.forEach(item => {
            tabGridField.keyValues.push(item);
          });
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        Common.processException(XMLHttpRequest, textStatus, errorThrown);
      },
    });
  },
};