import { productBrandQuery } from '@/api/productBrand'
const ProductBrand = {
  template: `
      <a-select
        :placeholder="placeholder"
        :id="id"
        allowClear
        :value="defaultVal"
        :showSearch="true"
        :disabled="disabled"
        @change="handleChange"
        option-filter-prop="children"
        :filter-option="filterOption">
        <a-select-option v-for="item in productBrandList" :key="item.brandSn" :value="item.brandSn">{{ item.brandName }}</a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: String,
      defatut: ''
    },
    id: {
      type: String,
      default: ''
    },
    placeholder:{
      type: String,
      default: '请选择产品品牌'
    },
    sysFlag: {
      type: String,
      default: ''
    },
    brandType: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      defaultVal: this.value,
      productBrandList: []
    };
  },
  mounted() {
    this.getProductBrand()
  },
  watch: {
    value(newValue, oldValue) {
      this.defaultVal = newValue
    },
    brandType(newValue,oldValue){
      this.handleChange(undefined)
      this.getProductBrand()
    }
  },
  methods: {
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    handleChange(value) {
      this.defaultVal = value;
      const row = this.productBrandList.find(item => item.brandSn == value)
      this.$emit('input', value);
      this.$emit('change', value, this.id, row);
    },
    //  产品品牌列表, sysFlag: 1 箭冠 0 自建
    getProductBrand () {
      productBrandQuery({ 'sysFlag': this.sysFlag, 'brandType': this.brandType }).then(res => {
        if (res.status == 200) {
          this.productBrandList = res.data
        } else {
          this.productBrandList = []
        }
      })
    },
  },
};

export default ProductBrand