import { productTypeQuery } from '@/api/productType'
import { dealerProductTypeList } from '@/api/dealerProductType'
const ProductType = {
  template: `
      <a-cascader
        @change="handleChange"
        change-on-select
        :value="defaultVal"
        expand-trigger="hover"
        :options="productTypeList"
        :fieldNames="{ label: 'productTypeName', value: 'productTypeSn', children: 'children' }"
        :id="id"
        placeholder="请选择产品分类"
        allowClear />
    `,
  props: {
    value: {
      type: Array,
      defatut: function () {
        return []
      }
    },
    id: {
      type: String,
      default: ''
    },
    isDealer: {
      type: Boolean,
      default: true
    },
    tenantId: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      defaultVal: this.value,
      productTypeList: []
    }
  },
  watch: {
    value (newValue, oldValue) {
      this.defaultVal = newValue
    },
    tenantId(newValue, oldValue){
      console.log(newValue)
    }
  },
  mounted () {
    this.getProductType()
  },
  methods: {
    handleChange (value) {
      this.defaultVal = value
      this.$emit('change', this.defaultVal)
    },
    //  产品分类列表 sysFlag: 1 箭冠 0 自建
    getProductType () {
      const params = {}
      if(this.tenantId!="NONE"){
        params.tenantId = this.tenantId
      }
      if(this.tenantId=="NONE"){
        params.sysFlag = '1'
      }
      if(this.isDealer){
        dealerProductTypeList(params).then(res => {
          if (res.status == 200) {
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
        })
      }else{
        productTypeQuery(params).then(res => {
          if (res.status == 200) {
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
        })
      }
      
    }
  }
}

export default ProductType
