<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    filterable
    :filter-method="filterValue"
    @change="change"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.tableComment"
      :value="item.id"
    >
      <el-row :gutter="10">
        <el-col :span="12">{{ item.id }}</el-col>
        <el-col :span="12" class="text-right">{{ item.tableComment }}</el-col>
      </el-row>
    </el-option>
  </el-select>

</template>

<script>
import { getCodegenTablePage } from '@/api/infra/codegen'

export default {
  props: {
    value: {
      type: [String, Number],
      default: ''
    }
  },
  data() {
    return {
      options: [],
      loading: false
    }
  },
  computed: {
    selected: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  created() {
    this.fetchBusinessNameList()
  },
  methods: {
    fetchBusinessNameList(tableComment) {
      this.loading = true
      const params = {
        pageNo: 1,
        pageSize: 200,
        tableComment
      }
      getCodegenTablePage(params).then(res => {
        const { list = [] } = res.data || {}
        this.options = list
      }).finally(() => {
        this.loading = false
      })
    },
    change(v) {
      const item = this.options.find(item => item.id === v)
      this.$emit('change', item)
    },
    filterValue(v) {
      this.fetchBusinessNameList(v)
    }
  }
}
</script>

  <style>

  </style>

