<template>
  <div>
    <vxe-select v-model="value" transfer>
      <vxe-option v-for="item in options" :key="item.value" :value="item.value" :label="item.label"></vxe-option>
    </vxe-select>
  </div>
</template>

<script>

export default {
  name: 'MkGridSelect',
  props: {
    params: Object
  },
  data() {
    return {
      options: [],
      value: ''
    }
  },
  created() {
    const { row, column } = this.params
    let params = column.params.getPostParams({ row, column });

    this.$mk.post({
      url: params.url,
      data: params.data
    }).then(a => {

      let options = [];
      a.forEach(item => {
        options.push({
          value: item[column.params.valueField],
          label: item[column.params.textField]
        })
      })
      this.options = options;

      if(row[column.field] && row[column.field][0]){
        this.value = row[column.field][0];
      }
    });
  },
  watch: {
    value() {
      const { row, column } = this.params
      if (this.value) {
        row[column.field] = [this.value, this.getLabel(this.value)];
      }
    }
  },
  methods: {

    // 获取label
    getLabel(v) {
      for (let i = 0; i < this.options.length; i++) {
        let o = this.options[i];
        if (o.value == v) {
          return o.label;
        }
      }
      return "";
    },

    // 加载数据
    load() {
      const { row, column } = this.params

      console.log(row, column)
    },

  }
}
</script>

<style>

</style>