export default {
    computed: {
      tagValue: {
        get: function () {
          if (this.multiple) {
            return this.selection
          } else {
            return 0 == this.selection.length ? null : this.selection[0]
          }
        },
      },
    },
    watch: {
      value: {
        handler: function (newVal) {
          if (newVal) {
            this.init(newVal)
          } else {
            this.selection = []
            this.sendEvent(null)
          }
        },
        deep: true,
      },
    },
    
    methods: {
      init(value) {
        if (this.multiple) {
          let codes = []
          if (this.isCode) {
            if(value&&value[0] instanceof Object) {
              codes = value.map((item) => item.code.toString())
            } else {
              codes = value.map(item => item.toString())
            }
          } else {
            codes = value.map((item) => item.code.toString())
          }
          if(this.setSelection) {
            this.initBySelection(codes, this.setSelection)
          } else {
            this.initBySelection(codes)
          }
        } else {
            let code = ''
            if(value instanceof Object) {
              code = value.code.toString()
            } else {
              code = value.toString()
            }
          if(this.setSelection) {
            this.initBySelection(code, this.setSelection)
          } else {
            this.initBySelection(code)
          }
        }
      },
      async initBySelection(code, callback) {
        if (this.isOrg) {
          let param = {
            pagination: false,
            queryDataStructure: 'ALL',
          }
          if (this.multiple) {
            param.orgCodes = code
          } else {
            param.code = code
          }
          this.selection = await this.getOrg(param)
        } else {
          let postData = {
            tableName: this.tableName,
            searchKey: '',
            queryDataStructure: 'ALL',
            authType: this.authType,
            versionDate: this.versionDate,
            expression: this.filterFormular,
            'BI-SYNTAX': null
          }
          if (this.multiple) {
            postData.baseDataCodes = code
          } else {
            postData.code = code
          }
          this.getBaseData(postData, '', true)
            .then((response) => {
              const { rows, rs, total } = response.data
              this.selection = rows
              if(this.isCode &&rows&&rows[0]){
                this.selectedValue = rows[0].code
              }else if(rows&&rows[0]){
                this.selectedValue = rows[0]
                this.selectedValue.id = rows[0].code;
                this.selectedValue.text = rows[0].name;
              }
              if (callback) {
                callback(rows)
              }
            })
            .catch((XMLHttpRequest) => {
              this.$Message.error('信息获取失败')
            })
        }
      }
    }
  }