import { supplierAllList, supplierAuthAllList } from '@/api/supplier.js'
const Employee = {
  template: `
      <a-select
        :placeholder="placeholder"
        :id="id"
        allowClear
        :value="defaultVal"
        :showSearch="true"
        :disabled="disabledFlag"
        @change="handleChange"
        option-filter-prop="children"
        :filter-option="filterOption">
        <a-select-option v-for="item in list" :key="item.supplierSn" :value="item.supplierSn">
        {{ item.supplierName }}
        </a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: String,
      defatut: ''
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择供应商'
    },
    enableFlag: {
      type: String,
      default: ''
    },
    disabledFlag: {
      type: Boolean,
      default: false
    },
    isPermission: {
      type: Boolean,
      default: false
    },
    pageSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      defaultVal: this.value,
      list: []
    }
  },
  mounted () {
    this.getList()
  },
  watch: {
    value (newValue, oldValue) {
      this.defaultVal = newValue
    }
  },
  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.supplierSn == value)
      this.$emit('input', value)
      this.$emit('change', value, item)
    },
    getList () {
      const _this = this
      const funs = _this.isPermission ? supplierAuthAllList : supplierAllList
      funs().then(res => {
        if (res.status == 200) {
          if (_this.enableFlag != '') {
            if (_this.pageSn === '福麦斯') {
              _this.list = res.data.filter(item => item.supplierName.includes('福麦斯') && item.enableFlag == _this.enableFlag)
            } else {
              _this.list = res.data.filter(item => item.enableFlag == _this.enableFlag)
            }
          } else {
            _this.list = res.data
          }
        } else {
          _this.list = []
        }
      })
    }
  }
}

export default Employee
