import { productTypeQueryAll, productTypeQuery } from '@/api/productType'
const ProductType = {
  template: `
      <a-cascader
        @change="handleChange"
        change-on-select
        :value="defaultVal"
        :size="size"
        style="width:100%;"
        expand-trigger="hover"
        :options="productTypeList"
        :disabled="disabled"
        :show-search="{ filter }"
        :fieldNames="{ label: 'productTypeName', value: 'productTypeSn', children: 'children' }"
        :id="id"
        :placeholder="placeholder"
        allowClear />
    `,
  props: {
    value: {
      type: Array,
      defatut: function () {
        return []
      }
    },
    placeholder: {
      type: String,
      default: '请选择产品分类'
    },
    id: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    },
    isAll: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: 'default'
    },
    // 是否显示全分类
    showAllType: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      defaultVal: this.value,
      productTypeList: []
    }
  },
  watch: {
    value (newValue, oldValue) {
      this.defaultVal = newValue
    }
  },
  mounted () {
    const a = this.$store.state.app.productTypeAllList
    // 如果有缓存
    if (this.isAll && a) {
      this.productTypeList = a || []
    } else {
      this.getProductType()
    }
  },
  methods: {
    filter (inputValue, path) {
      return path.some(option => option.productTypeName.indexOf(inputValue) > -1)
    },
    setDefValue (defaultVal, row) {
      this.handleChange(defaultVal, row)
    },
    handleChange (value, row) {
      console.log(value, row)
      this.defaultVal = value
      this.$emit('change', this.defaultVal, this.id, row)
      this.$emit('input', this.defaultVal)
    },
    showALLtype () {
      // 是否显示全分类
      const hasAll = this.productTypeList.some(item => item.productTypeSn == 'ALL')
      if (this.showAllType && !hasAll) {
        this.productTypeList.unshift({ productTypeName: '全分类', productTypeSn: 'ALL', children: [{ productTypeName: '全分类', productTypeSn: 'ALL' }] })
      }
    },
    //  产品分类  列表
    getProductType () {
      if (this.isAll) {
        productTypeQueryAll({}).then(res => {
          if (res.status == 200) {
            if (res.data && res.data.length > 0) {
              //  递归处理disabled禁用
              this.recursionFun(res.data, false)
              this.productTypeList = res.data
            } else {
              this.productTypeList = []
            }
          } else {
            this.productTypeList = []
          }
          // 缓存数据
          this.$store.state.app.productTypeAllList = this.productTypeList
          // 是否显示全分类
          this.showALLtype()
        })
      } else {
        productTypeQuery({}).then(res => {
          if (res.status == 200) {
            this.productTypeList = res.data
          } else {
            this.productTypeList = []
          }
          // 是否显示全分类
          this.showALLtype()
        })
      }
    },
    //  递归函数
    recursionFun (data, state) {
      if (data) {
        data.map((item, index) => {
          // enabledFlag为0表示禁用，父级禁用则子级也为禁用
          if ((item.enabledFlag && item.enabledFlag == 0) || state) {
            item.disabled = true
          } else {
            item.disabled = false
          }
          if (item.children && item.children.length == 0) {
            delete item.children
          } else {
            this.recursionFun(item.children, item.disabled)
          }
        })
      }
    }
  }
}

export default ProductType
