<template>
  <el-select
    v-model="value"
    :size="size || 'medium'"
    style="width: 100%"
    filterable
    clearable
    :placeholder="placeholder || ''"
    :disabled="disabled"
    :remote-method="remoteMethod"
    :loading="selectLoading"
    @change="selectChange"
  >
    <el-option
      v-for="(item, index) in options"
      :key="item.id + index"
      :label="item.platform_name"
      :value="item.id"
      :disabled="item.status + '' === '0' && !controlDisabled"
    />
  </el-select>
</template>

<script>
import asyncRequest from '@/apis/components/search-terrace'
import resToken from '@/mixins/resToken'
export default {
  name: 'SearchTerrace',
  mixins: [resToken],
  props: [
    'size',
    'value',
    'placeholder',
    'isDetail',
    'disabled',
    'type',
    'names',
    'is_show',
    'controlDisabled'
  ],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小            非必填
   * @param {Array}         value            : 选中值              必填
   * @param {String}        placeholder      : 提示信息            非必填
   * @param {Boolean}       isDetail         : 是否是详情逻辑       必填
   * @param {Boolean}       disabled         : 是否禁用            必填
   * @param {String}        names            : 选中值label         展示详情必填
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      options: [],
      selectLoading: false,
      searchName: '',
      formValue: {
        page: 1,
        size: 10000,
        start: '',
        is_show: '1',
        end: '',
        platform_name: '',
        creater: ''
      }
    }
  },
  watch: {
    names: function(val, old) {
      // console.log(val, old, this.isDetail, "1");
      this.searchName = val
      if (this.isDetail && this.searchName) {
        this.remoteMethod(this.searchName)
      }
    },
    isDetail: function(val, old) {
      // console.log(val, old, this.isDetail, "2");
      if (val && this.searchName) {
        this.remoteMethod(this.searchName)
      }
    }
  },
  mounted() {
    this.options = []
    this.selectLoading = false
    this.remoteMethod()
  },
  methods: {
    async selectChange(e) {
      if (e) {
        const index = this.options.findIndex((v) => v.id === e)
        if (index !== -1) {
          const model = {
            id: this.options[index].id,
            code: this.options[index].platform_code,
            label: this.options[index].platform_name,
            platform_type: this.options[index].platform_type
          }
          this.$emit('searchChange', model)
        } else {
          this.$emit('searchChange', {})
        }
      } else {
        this.$emit('searchChange', {})
      }
    },
    async remoteMethod() {
      this.selectLoading = true
      this.options = []
      this.formValue.is_show = this.is_show || '1'
      const { code, data, message } = await asyncRequest.list(this.formValue)

      if (code === 0) {
        const { list } = data
        this.options = list
      } else if (code >= 100 && code <= 104) {
        await this.logout()
      } else {
        this.$message.warning(message)
      }
      this.selectLoading = false
    }
  }
}
</script>

<style>
</style>
