import { warehouseCascadeList } from '@/api/warehouse'
const Warehouse = {
  template: `
        <a-cascader
          :size="size"
          @change="handleChange"
          :value="defaultVal"
          expand-trigger="hover"
          :allowClear="allowClear"
          :options="warehouseCascadeData"
          :fieldNames="{ label: 'name', value: 'sn', children: 'warehouseLocationList' }"
          :id="id"
          placeholder="请选择仓库仓位"
          style="width: 100%;" />
    `,
  props: {
    value: {
      type: Array,
      defatut: function () {
        return []
      }
    },
    id: {
      type: String,
      default: ''
    },
    allowClear: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: 'default'
    }
  },
  data () {
    return {
      defaultVal: this.value,
      warehouseCascadeData: [],
      defaultWarehouseCascade:[]
    }
  },
  watch: {
    value (newValue, oldValue) {
      this.defaultVal = newValue
    },
  },
  mounted () {
    this.getWarehouseCascade()
  },
  methods: {
    handleChange (value, selectedOptions) {
      this.defaultVal = value
      this.$emit('change', this.defaultVal, selectedOptions)
      this.$emit('input', this.defaultVal)
    },
    //  仓库仓位  级联  列表
    getWarehouseCascade () {
      const _this = this
      warehouseCascadeList({}).then(res => {
        if (res.status == 200) {
          res.data.filter(item => {
            // 过滤默认仓库
            if (item.defaultFlag == 1 && item.wasteFlag == 0) { //  defaultFlag为1,且不是废品仓
              _this.defaultWarehouseCascade[0] = item.warehouseSn
              // 过滤默认仓位
              item.warehouseLocationList.filter(subItem => {
                if (subItem.defaultFlag == 1 && subItem.wasteFlag == 0) {
                  _this.defaultWarehouseCascade[1] = subItem.warehouseLocationSn
                }
              })
            }
          })
          res.data.map(item => {
            item.sn = item.warehouseSn
            if (item.warehouseLocationList) {
              item.warehouseLocationList.map(subItem => {
                subItem.sn = subItem.warehouseLocationSn
              })
            }
          })
          this.warehouseCascadeData = res.data
          if(this.defaultVal&&this.defaultVal.length==0){
            this.handleChange(_this.defaultWarehouseCascade)
          }
        } else {
          this.warehouseCascadeData = []
        }
      })
    },
    setDefaultVal(){
      if(this.defaultVal&&this.defaultVal.length==0){
        this.handleChange(this.defaultWarehouseCascade)
      }
    }
  }
}

export default Warehouse
