/**
 * TableColumn,表单项，应用数据集配置，返回默认表现形式编辑器配置
 * @author cls
 * @date 2021-03-09
 */

import { StringUtil } from "@birt/funclib/StringUtil.js";
import { $CONST } from "@birt/funclib/ConstUtil";
import { $ViewStyle } from "@birt/funclib/ViewStyle"

export const FormTableEditor = {
  /**
   * 根据字段配置，返回默认编辑器表现形式
   * @param {*} fieldConf {} 字段配置
   * @param {*} childConf {} 表单项或网格列孩子节点配置
   * @return {}
   */
  getEditorByFieldConf: function (fieldConf, childConf, controlName) {
    if (fieldConf) {
      let editor = this._getBindFieldCompt(fieldConf, controlName);
      // 临时方案，当创建列不相等时，出有特殊已封装列对象,如：HcTableColumnCheckbox，直接返回
      if (editor.controlName != controlName) {
        return editor;
      }
      let cmpt = this._getEditorCompt(fieldConf, childConf, controlName);
      cmpt.controlId = "_" + StringUtil.randomString(6);
      if (!childConf._isFilterEditor && (fieldConf.readonly || childConf.readonly)) {
        cmpt.readonly = true;
      }
      editor.children.push(cmpt);
      if (this._isTable(controlName)) {
        editor.slot = "scope";
      }
      return editor;
    }
    return {};
  },
  /**
   * 根据字段配置，返回过滤编辑器
   * @param {*} fieldConf 
   * @param {*} childConf 
   * @param {*} controlName 
   * @returns 
   */
  getFilterByFieldConf: function (fieldConf, childConf, controlName) {
    if (fieldConf) {
      let filterEditor = this._getEditorCompt(fieldConf, Object.assign({ _isFilterEditor: true }, childConf), controlName);
      filterEditor.controlId = "_" + StringUtil.randomString(6);
      if (this._isTable(controlName)) {
        filterEditor.slot = "header";
        filterEditor.placeholder = fieldConf.label;
      }
      return filterEditor;
    }
  },
  /**
   * 根据字段配置，返回默认显示Dom Json表现形式
   * @param {*} fieldConf 
   * @param {*} childConf 
   * @param {*} controlName 
   */
  getViewByFieldConf: function (fieldConf, childConf, controlName) {
    if (fieldConf) {
      let fieldCompt = this._getBindFieldCompt(fieldConf, controlName);
      if (this._isTable(controlName) && fieldCompt.children.length > 0) {
        fieldCompt.slot = "scope";
      }
      return fieldCompt;
    }
    return {}
  },
  // 判断是否表格列
  _isTable(controlName) {
    return controlName == "HcTableColumn";
  },
  // 获取绑定字段配置
  _getBindFieldCompt(fieldConf, controlName) {
    let fieldCompt = {
      controlName: controlName,
      controlId: "_" + StringUtil.randomString(6),
      label: fieldConf.label,
      children: []
    }
    if (this._isTable(controlName)) {
      fieldCompt = this._getTableFieldCompt(fieldConf, fieldCompt);
    }
    return fieldCompt;
  },
  // 获取表格绑定字段配置
  _getTableFieldCompt(fieldConf, fieldCompt) {
    if (fieldConf.dbtype == "dbBoolean") {
      fieldCompt.controlName = "HcTableColumnCheckbox";
      fieldCompt.showCheckAll = false;
    } else if (fieldConf.dbtype == "dbIntBoolean") {
      fieldCompt.controlName = "HcTableColumnCheckbox";
      fieldCompt.showCheckAll = false;
      fieldCompt.trueLabel = 1;
      fieldCompt.falseLabel = 0;
    }
    return fieldCompt;
  },
  // 获取编辑控件
  _getEditorCompt(fieldConf, childConf, controlName) {
    // 默认编辑器 包含 if (fieldConf.dbtype == "dbString") 
    let editorId = "INPUT";
    let lookupType = fieldConf.lookup && fieldConf.lookup.type;
    let viewStyle = childConf.viewStyle;
    let vsId = viewStyle && viewStyle.viewStyleId;

    // 存在表现形式配置，优先应用
    if (vsId && $ViewStyle[vsId]) {
      editorId = vsId;
    } else {
      if (lookupType == $CONST.LOOKUPSYSENUMS) {
        editorId = "LKSYSENUMS";
      } else if (lookupType == $CONST.LOOKUPDATA) {
        let openstyle = fieldConf.lookup.openstyle || $CONST.DIALOG;
        if (openstyle == $CONST.DROPDOWN) {
          editorId = "LKDATADROPDOWN";
        } else if (openstyle == $CONST.DIALOG) {
          editorId = "LKDATADIALOG";
        }
      } else if (fieldConf.dbtype == "dbInt") {
        editorId = "INPUTINT";
      } else if (fieldConf.dbtype == "dbIntBoolean") {
        editorId = "CHECKBOXINT";
      } else if (fieldConf.dbtype == "dbBoolean") {
        editorId = "CHECKBOXBOOLEAN";
      } else if (fieldConf.dbtype == "dbFloat" || fieldConf.dbtype == "dbDouble") {
        editorId = "INPUTNUMBER";
      } else if (fieldConf.dbtype == "dbText") {
        if (this._isTable(controlName)) {
          editorId = "TEXTDIALOG";
        } else {
          editorId = "TEXTAREA";
        }
      } else if (fieldConf.dbtype == "dbDatetime") {
        if (childConf._isFilterEditor) {
          editorId = "DATERANGE";
        } else {
          editorId = "DATEPICKER";
        }
      }
    }
    return $ViewStyle[editorId].call(this, fieldConf, childConf, controlName);
  }
};