import { employeeQueryList } from '@/api/expenseManagement'
const Employee = {
  template: `
      <a-select
        :placeholder="placeholder"
        :id="id"
        allowClear
        :value="defaultVal"
        :showSearch="true"
        @change="handleChange"
        option-filter-prop="children"
        :filter-option="filterOption">
        <a-select-option v-for="item in list" :key="item.employeeSn" :value="item.employeeSn">
        {{ item.name }}
        <span v-if="item.kdDepartmentAllName">--{{item.kdDepartmentAllName}}</span>
        </a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: String,
      defatut: ''
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择申请人（输入名称搜索）'
    },
    notInSn:{
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      defaultVal: this.value,
      list: []
    };
  },
  mounted() {
    this.getList()
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    },
    notInSn(a,b){
      console.log(a,b)
      if(JSON.stringify(a) !== JSON.stringify(b)){
        this.getList()
      }
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      this.defaultVal = value;
      const item = this.list.find(item => item.employeeSn == value)
      this.$emit('change', value, item);
      this.$emit('input', value);
    },
    getList () {
      console.log(11)
      this.list = this.$store.state.app.employeeList.filter(item => this.notInSn.indexOf(item.employeeSn)<0)
      if(this.list.length==0){
        employeeQueryList({}).then(res => {
          if (res.status == 200) {
            this.list = res.data
          } else {
            this.list = []
          }
        })
      }
    },
  },
};

export default Employee