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

<script>
import asyncRequest from "./http";
import resToken from "@/mixins/resToken";
export default {
  name: "SearchWorkCompany",
  mixins: [resToken],
  props: [
    "size",
    "value",
    "placeholder",
    "isDetail",
    "disabled",
    "type",
    "names",
    "is_show",
  ],
  /**
   * 属性集合
   * @param {String}        size             : 组件大小            非必填
   * @param {Array}         value            : 选中值              必填
   * @param {String}        placeholder      : 提示信息            非必填
   * @param {Boolean}       isDetail         : 是否是详情逻辑       必填
   * @param {Boolean}       disabled         : 是否禁用            必填
   * @param {String}        names            : 选中值label         展示详情必填
   */
  /**
   * 事件集合
   * @searchChange             : 选中值变化调用   抛出选中数据
   */
  data() {
    return {
      options: [],
      loading: false,
      searchName: "",
    };
  },
  mounted() {
    this.options = [];
    this.loading = false;
    this.remoteMethod();
  },
  methods: {
    async selectChange(e) {
      // console.log(e);
      let index = this.options.findIndex((v) => v.companyNo === e);
      if (index !== -1) {
        let model = {
          id: this.options[index].id,
          code: this.options[index].companyNo,
          label: this.options[index].company,
        };
        this.$emit("searchChange", model);
      } else {
        this.$emit("searchChange", {});
      }
    },
    async remoteMethod() {
      this.loading = true;
      const res = await asyncRequest.list({
        is_show: this.is_show ? this.is_show : "1",
      });
      const { code, data } = await this.useResHandle(res);
      if (code === 0) {
        const { list } = data;
        this.options = list;
      } else {
        this.options = [];
      }
      this.loading = false;
    },
  },
};
</script>

<style></style>
