import { queryAuthWarehouse, warehouseAllList } from '@/api/warehouse'
const warehouse = {
  template: `
      <a-select
        :placeholder="placeholder"
        :id="id"
        style="width:100%;"
        :allowClear="allowClear"
        :value="defaultVal"
        :showSearch="true"
        :size="size"
        :disabled="disabled"
        :mode="modeType"
        @change="handleChange"
        :filter-option="filterOption">
        <a-select-option v-for="item in warehouseData" :key="item.warehouseSn" :value="item.warehouseSn">{{ item.name }}</a-select-option>
      </a-select>
    `,
  props: {
    value: {
      type: [String, Array],
      defatut: ''
    },
    isPermission: {// false无权限 true有权限
      type: Boolean,
      default: false
    },
    allowClear: {
      type: Boolean,
      default: true
    },
    size: {
      type: String,
      default: 'default'
    },
    id: {
      type: String,
      default: ''
    },
    placeholder: {
      type: String,
      default: '请选择仓库'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    modeType: {
      type: String,
      default: 'default'
    },
    isDefault: { // 是否有默认值
      type: Boolean,
      default: undefined
    }
  },
  data () {
    return {
      defaultVal: this.value,
      warehouseData: [],
      pageNo: 1,
      pageSize: 1000
    }
  },
  mounted () {
    // 如果已经存在仓库数据
    const a = this.$store.state.app.warehouseAuthList 
    const b = this.$store.state.app.warehouseAllList

    if (this.isPermission) {
      this.$store.state.app.defWarehouse = null
      this.$store.state.app.isWarehouse = true
      if(a){
        this.setWarehouseData(a)
      }else{
        // 不存在，远程获取
        this.getWarehouse()
      }
    }else{
      if(b){
        this.setWarehouseData(b)
      }else{
        // 不存在，远程获取
        this.getWarehouse()
      }
    }
    
  },
  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
      this.$emit('input', value)
      this.$emit('change', value)
    },
    // 设置仓库数据
    setWarehouseData(data){
      // 有权限控制的
      if (this.isPermission) {
        this.$store.state.app.isWarehouse = true
        this.$store.state.app.defWarehouse = data && data[0]
      }
      this.warehouseData = data || []
      let defaultWarehouseVal = {}
      if (this.isDefault) {
        defaultWarehouseVal = data.find(con => {
          return con.defaultFlag == 1 ? con.warehouseSn : ''
        })
        this.defaultVal = defaultWarehouseVal.warehouseSn
        this.$emit('load', defaultWarehouseVal.warehouseSn, this.warehouseData)
      } else {
        this.$emit('load', null, this.warehouseData)
      }
    },
    //  获取仓库列表
    getWarehouse () {
      const ajaxName = this.isPermission ? queryAuthWarehouse : warehouseAllList
      ajaxName({}).then(res => {
        if (res.status == 200) {
          this.setWarehouseData(res.data)
          // 存储仓库数据本地
          if(this.isPermission){
            this.$store.state.app.warehouseAuthList = this.warehouseData
          }else{
            this.$store.state.app.warehouseAllList = this.warehouseData
          }
        } else {
          if (this.isPermission) {
            this.$store.state.app.isWarehouse = true
            this.$store.state.app.defWarehouse = null
          }
          this.warehouseData = []
        }
      })
    },
    hasWarehouse(sn){
      return this.warehouseData.find(item => item.warehouseSn == sn)
    },
    clearData () {
      this.handleChange([])
    },
    // 清空缓存的数据
    clearCacheData(){
      this.$store.state.app.warehouseAuthList = null
      this.$store.state.app.warehouseAllList = null
    }
  }
}

export default warehouse
