<template>
  <el-select
    v-model="value"
    multiple
    filterable
    remote
    clearable
    collapse-tags
    :multiple-limit="1"
    reserve-keyword
    :size="size || 'medium'"
    style="width: 100%"
    :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.name"
      :value="item.code + ''"
      :disabled="item.status !== '1'"
    >
    </el-option>
  </el-select>
</template>

<script>
import asyncRequest from "@/apis/components/search-supplier";
import resToken from "@/mixins/resToken";
export default {
  name: "SearchSupplier",
  mixins: [resToken],
  props: ["size", "value", "placeholder", "isDetail", "disabled", "names", "type", "isp"],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小            非必填
   * @param {Array}         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.isDetail, "1");
      this.searchName = val;
      if (this.isDetail && this.searchName) {
        this.remoteMethod(this.searchName, this.isp);
      }
    },
    isDetail: function (val, old) {
      // console.log(val, old, this.isDetail, "2");
      if (val && this.searchName) {
        this.remoteMethod(this.searchName, this.isp);
      }
    },
  },
  mounted() {
    this.options = [];
    this.selectLoading = false;
  },
  methods: {
    async selectChange(e) {
      // console.log(e);
      // console.log("1111" + this.isp);
      if (e && e.length > 0) {
        let index = this.options.findIndex((v) => v.code === e[0]);
        if (index !== -1) {
          let model = {
            id: this.options[index].id,
            code: this.options[index].code,
            label: this.options[index].name,
            isPower: this.options[index].has_account + "" !== "0",
          };
          this.$emit("searchChange", model);
        } else {
          this.$emit("searchChange", {});
        }
      } else {
        this.$emit("searchChange", {});
      }
    },
    async remoteMethod(query, types) {
      this.selectLoading = true;
      if (query !== "") {
        this.options = [];
        let formValue = {
          page: 1,
          size: 100,
          name: query,
          code: "",
          is_platform: this.type ? this.type : "",
        };
        const { code, data, message } = await asyncRequest.list(formValue);
        if (code === 0) {
          const { list } = data;
          this.options = list;
          if (types) {
            // console.log(this.value);
            const index = this.options.findIndex((v) => v.code === this.value[0]);
            // console.log(this.options);
            // console.log(index);
            if (index !== -1) {
              await this.selectChange([this.options[index].code]);
            }
          }
          // console.log(types);
        } else if (code >= 100 && code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(message);
        }
      } else {
        this.options = [];
      }
      this.selectLoading = false;
    },
  },
};
</script>

<style></style>
