<template>
  <el-select
    v-model="value"
    :size="size || 'medium'"
    style="width: 100%"
    :placeholder="placeholder || ''"
    :disabled="disabled"
    @change="selectChange"
  >
    <el-option
      v-for="item in options"
      :key="item.result_code"
      :label="item.result"
      :value="item.result_code"
      :disabled="item.status==='0'"
    >
    </el-option>
  </el-select>
  <!-- <el-select
    v-model="value"
    multiple
    filterable
    remote
    :multiple-limit="1"
    reserve-keyword
    :size="size || 'medium'"
    style="width: 100%"
    :placeholder="placeholder||''"
    :disabled="disabled"
    :remote-method="getList"
    :loading="selectLoading"
    @change="selectChange"
  >
    <el-option
      v-for="(item, index) in options"
      :key="item.id + index"
      :label="item.name"
      :value="item.code+''"
      :disabled="item.status!=='1'"
    >
    </el-option>
  </el-select> -->
</template>

<script>
import asyncRequest from "@/apis/components/abnormal-cause";
import resToken from "@/mixins/resToken";
export default {
  name: "abnormalCause",
  mixins: [resToken],
  props: [
    "size",
    "value",
    "placeholder",
    "isDetail",
    "disabled",
    "names",
    "type",
  ],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小            非必填
   * @param {String}        value            : 选中值              必填
   * @param {String}        placeholder      : 提示信息            非必填
   * @param {Boolean}       isDetail          : 是否是详情逻辑       必填
   * @param {Boolean}       disabled         : 是否禁用            必填
   * @param {String}        names            : 选中值label         展示详情必填
   * @param {String}        type             : 数据类型            非必填 1是平台供应商0非平台供应商
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      options: [],
      selectLoading: false,
      searchName: "",
    };
  },
  watch: {
    names: function (val, old) {
      // console.log(val, old);
      this.searchName = val;
      if (this.isDetail && this.searchName) {
        this.getList(this.searchName);
      }
    },
  },
  mounted() {
    this.options = [];
    this.selectLoading = false;
    this.getList()
  },
  methods: {
    async selectChange(e) {
      if (e) {
        let index = this.options.findIndex((v) => v.result_code === e);
        if (index !== -1) {
          let model = {
            id: this.options[index].id,
            code: this.options[index].result_code,
            label: this.options[index].result,
          };
          this.$emit("searchChange", model);
        } else {
          this.$emit("searchChange", {});
        }
      } else {
        this.$emit("searchChange", {});
      }
    },
    async getList() {
      this.selectLoading = true;
      this.options = [];
      let formValue = {
        type: this.type || "1",
        page: 1,
        size: 500,
      };

      let res = await asyncRequest.list(formValue);
      if (res && res.code === 0 && res.data) {
        const { list } = res.data;
        this.options = list;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }

      this.selectLoading = false;
    },
  },
};
</script>

<style>
</style>