<template>
  <div class="input-control-wrapper">
    <component
      v-if="!isContent&&controlDisplay" 
      :is="curComponent"
      v-model="value"
      clearable
      :multiple="define.config.multiple"
      :disabled="state=='readOnly'"
      :placeholder="define.config.placeholder"
      :title="define.config.title"
      :maxTagCount="define.config.maxTagCount"
      :search-placeholder="define.config.searchPlaceholder"
      :auth-type="define.config.authType"
      :version-date="define.config.versionDate"
      :table-name="define.config.relation"
      :modalWidth="define.config.modalWidth"
      :inputLabel="define.config.inputLabel"
      :isFilter="define.config.isFilter"
      :isCode="define.config.isCode"
      :isLeaf="define.config.isLeaf"
      :hideSelected="define.config.hideSelected"
      :groupTableName="groupTableName"
      :structtype="structtype"
      :isNoDefine="false"
      :treeTableName="treeTableName"
      :filterFormularSP="define.config.formula"
      :filterFormular="filterFormular"
      :billdefineName="context.billdefine"
      :billId="context.bill&&context.bill.getMasterData&&context.bill.getMasterData().getValue&&context.bill.getMasterData().getValue('id') || ''"
      :bill="context.bill"
      :rowIndex="rowIndex"
      :expression="define.config.formula"
      @data-change="onBlur"
      ref="baseDataListModal"
    ></component>
    <div v-if="isContent" >
          {{getValue()}}
    </div>
  </div>
</template>

<script>
import input from './util/bc_input.js'
import BaseDataTreeSelect from './baseDataSelect/index.vue'
const componentMap = {
  '弹框': BaseDataTreeSelect
}
export default {
  mixins: [input],
  props: ['define', 'context'],
  components: {
    BaseDataTreeSelect
  },
  computed:{
    curComponent(){
     return componentMap['弹框']
    }
  },
  data(){
    return {
      structtype: -1,
      // 保存分组列表类型的tableName
      groupTableName: {},
      treeTableName: ''
    }
  },
  created() {
    this.getBaseDataDefine(this.define.config.relation)
  },
  mounted() {
    if (this.tableStyle) {
      this.$nextTick(function(){
        setTimeout(function(){
          this.loadControl();
        }.bind(this),600);
      }.bind(this));
    }
  },
  methods: {
    setDataObjectValue(valueChange){
      this.context.bill.setSubFocus(this.rowIndex)
      this.context.dataObject.setValue(this.define.config.field, valueChange);
    },
    loadControl(){
        if(this.$refs.baseDataListModal){
          this.$refs.baseDataListModal.showModal = true
        }
    },
    getBaseDataDefine(tableName) {
      GMS.$http.post('/baseData/define/get', {name: tableName}).then(res => {
        const { data } = res
        this.structtype = data.structtype
        const define = JSON.parse(data.define)
        const groupFieldName = data.groupfieldname
        if (define.showFields) {
          const relevance = define.showFields.find(item => item.columnName === groupFieldName)
          let relevanceField = ''
          if (relevance) {
            relevanceField = relevance.mapping? relevance.mapping.split('.')[0]: ''
          }
          this.groupTableName = {
            tableName: relevanceField,
            groupFieldName: data.groupfieldname,
            define: define
          }
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
@import "./baseDataSelect/dynamic-from-common.less";
</style>