<!-- 员工信息下拉列表 -->
<template>
  <el-select
    v-model="selected"
    v-bind="$attrs"
    :loading="loading"
    filterable
    :filter-method="filterValue"
    @change="employeeChange"
    @visible-change="handleVisibleChange"
  >
    <el-option
      v-for="item in options"
      :key="item.id"
      :label="item.employeeNo"
      :value="item.employeeNo"
    >
      <el-row :gutter="10">
        <el-col :span="12">{{ item.employeeNo }}</el-col>
        <el-col :span="12" class="text-right">{{ item.employeeName }}</el-col>
      </el-row>
    </el-option>
  </el-select>
</template>

<script>
import { getEmployeeMasterPage } from "@/api/mes/employeeMaster/index";
import { debounce } from "throttle-debounce";

export default {
  props: {
    value: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      options: [],
      loading: false,
    };
  },
  computed: {
    selected: {
      get() {
        return this.value;
      },
      set(v) {
        this.$emit("input", v);
      },
    },
  },
  // created() {
  //   this.fetchEmployeeInformationList()
  // },
  methods: {
    fetchEmployeeInformationList(employeeName) {
      this.loading = true;
      const params = {
        pageNo: 1,
        pageSize: 100,
        employeeName,
      };
      getEmployeeMasterPage(params)
        .then((res) => {
          const { list = [] } = res.data || {};
          this.options = list;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    filterValue: debounce(250, function (v) {
      this.fetchEmployeeInformationList(v);
    }),
    employeeChange(v) {
      const item = this.options.find((item) => item.employeeNo === v);
      this.$emit("change", item);
    },
    handleVisibleChange(visible) {
      if (visible) {
        this.fetchEmployeeInformationList();
      }
    },
  },
};
</script>

<style></style>
