import FormItemWrapper from '@base/components/xform/form-designer/form-widget/field-widget/form-item-wrapper'
import emitter from '@base/components/xform/utils/emitter'
import i18n from "@base/components/xform/utils/i18n";
import {deepClone} from "@base/components/xform/utils/util";
import fieldMixin from "@base/components/xform/form-designer/form-widget/field-widget/fieldMixin";

export default {
  mixins: [emitter, fieldMixin, i18n],
  props: {
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,

    designState: {
      type: Boolean,
      default: false
    },
    columnConfig: {
      type: Object,
      default: null
    },
    subFormRowIndex: { /* 子表单组件行索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormColIndex: { /* 子表单组件列索引，从0开始计数 */
      type: Number,
      default: -1
    },
    subFormRowId: { /* 子表单组件行Id，唯一id且不可变 */
      type: String,
      default: ''
    }
  },
  components: {
    FormItemWrapper
  },
  data() {
    let multipleChoices = this.field.options.multipleChoices || false;
    let defaultValue = !multipleChoices ? null : [];
    return {
      oldFieldValue: defaultValue, //field组件change之前的值
      fieldModel: defaultValue,
      showValue2: null,
      rules: [],
      showSearchialog: false,
      dialogContent: null,
      showSysSearchialog: false,
      systemDialogParam: {},
      showVformDialog: false,
      code: null,
      formDialogOption: {}
    }
  },
  watch: {
    fieldModel(val) {
      // this.handleChangeEvent(val);
      let currentData = this.currentData;
      let fieldKeyName = this.fieldKeyName;
      currentData[fieldKeyName] = val;
    },
    showValue(val) {
      this.setShowValue(val);
    }
  },
  computed: {
    showValue() {
      let currentData = this.currentData;
      let fieldKeyName = this.fieldKeyName;
      let vabSearchName = this.field.options.vabSearchName;
      let multipleChoices = this.field.options.multipleChoices || false;
      let labelField = this.field.options.vabSearchName || fieldKeyName;
      let value = null;
      if (!multipleChoices) {
        value = currentData[labelField] ?? null;
      } else {
        if (this.fieldModel) {
          value = this.fieldModel.map(item => item[labelField]).join(",");
        }
      }
      this.showValue2 = value;
      return value
    },
    tableRow() {
      let tableParam = this.tableParam;
      return tableParam && tableParam.row ? tableParam.row : null;
    },
    currentData() {
      let tableParam = this.tableParam;
      return tableParam && tableParam.row ? tableParam.row : this.formModel;
    }
  },
  beforeCreate() {
    /* 这里不能访问方法和属性！！ */
  },

  created() {
    /* 注意：子组件mounted在父组件created之后、父组件mounted之前触发，故子组件mounted需要用到的prop
       需要在父组件created中初始化！！ */
    if (this.field.options.readonly == undefined) {
      this.$set(this.field.options, "readonly", true);
    }
    this.initFieldModel()
    this.registerToRefList()
    this.initEventHandler()
    this.buildFieldRules()

    this.handleOnCreated()
    // this.initValue();
  },

  mounted() {
    this.handleOnMounted()

  },

  beforeDestroy() {
    this.unregisterFromRefList()
  },
  methods: {
    setShowValue(val = null) {
      let multipleChoices = this.field.options.multipleChoices || false;
      if (!multipleChoices) {
        let fieldKeyName = this.fieldKeyName;
        let labelField = this.field.options.vabSearchName || fieldKeyName;
        // this.$set(this.currentData, labelField, val)

        this.currentData[labelField] = val
        this.showValue2 = val;
      }
    },
    setValue: function (e) {
      if (this.field.formItemFlag) {
        let data = this.tableRow || this.formModel;
        var t = deepClone(this.fieldModel);
        this.fieldModel = e,
          this.initFileList(),
          this.syncUpdateFormModel(e),
          this.emitFieldDataChange(e, t);
      }
    },
    clearHandle() {
      let multipleChoices = this.field.options.multipleChoices || false;
      let defaultValue = !multipleChoices ? null : [];
      this.setShowValue(null)
      this.setValue(defaultValue)
      if (this.field.options.onSearchClear) {
        let tableParam = this.tableParam;
        let eventParamNames = [];
        let eventParamValues = [];
        if (tableParam) {
          eventParamNames = ['rowData', 'rowIndex'];
          eventParamValues = [tableParam.row, tableParam.rowIndex];
        }
        this.handleCustomEvent(this.field.options.onSearchClear, eventParamNames, eventParamValues)

        /* var e = new Function(this.field.options.onSearchClear);
         e.call(this);*/
      }
      this.handleClearSearchDialog();
    },
    changeShowValue2(val) {
      if (this.field.options.readonly === false) {
        let multipleChoices = this.field.options.multipleChoices || false;
        if (!multipleChoices) {
          let fieldKeyName = this.fieldKeyName;
          let vabSearchName = this.field.options.vabSearchName;
          let labelField = this.field.options.vabSearchName || fieldKeyName;
          this.currentData[labelField] = val;
        }
      }
    },
    fieldModelLabel() {
      return this.showValue;
    },
  }
}
