import { getStorage } from '@/common/utils/storage.js'
export default {
  data() {
    return {
      // 网点下拉选项
      websitList: [],
      // 网点下拉选项选中的下标
      index: 0,
      // 类型切换选中值
      selected: 1,
      // 是否显示类型切换
      showTypeSwitching: true,
      // 下拉刷新状态
      refresherTriggered: false,
      // 配件类型列表
      accessoriesList: [],
      // 类型选中下标
      accessoriesIndex: -1,
      // 列表数据
      list: [],
      // 名称搜索
      partsName: '',
      option: {},
      apis: {
        default: {
          getGroupUrl: '/app/parts/stock/exist/websit/group/list',
          getStockUrl: '/app/parts/stock/exist/worker/stock/list'
        },
        applicationParts: {
          getGroupUrl: '/app/parts/websit/stock/group/list',
          getStockUrl: '/app/parts/websit/stock/list'
        }
      }
    }
  },
  onLoad(option) {
    this.option = option
    if (!option.partsWebsitId && !option.websitId) {
      this.getWebsitInfo()
    }
  },
  computed: {
    accessoriesParams() {
      if (this.option.partsWebsitId && this.option.websitId) {
        return {
          identity: this.userInfo.idCard,
          partsWebsitId: this.option.partsWebsitId,
          websitId: this.option.websitId
        }
      }
      var data = this.websitList[this.index]
      if (data) {
        return {
          identity: this.userInfo.idCard,
          partsAttr: ['OLD', 'NEW'][this.selected],
          partsWebsitId: data.partsWebsitId,
          websitId: data.websitId
        }
      } else {
        return false
      }
    },
    userInfo() {
      return getStorage('user')
    },
    idcard() {
      return this.userInfo.idCard
    }
  },
  watch: {
    accessoriesParams(newVal) {
      this.getGroup(newVal)
      this.list = []
    },
    accessoriesList(newVal) {
      if (this.accessoriesIndex !== 0) {
        this.accessoriesIndex = 0
      } else {
        this.getStock()
      }
    },
    accessoriesIndex() {
      this.getStock()
    },
    partsName() {
      this.getStock()
    }
  },
  methods: {
    // 获取网点
    getWebsitInfo() {
      this.$api
        .post('/app/parts/stock/exist/websit/list', {
          identity: this.idcard
        })
        .then(res => {
          console.log(res.data)
          this.websitList = res.data
        })
        .catch(() => {
          this.websitList = []
        })
    },
    // 获取分组
    getGroup(params) {
      if (params) {
        this.$api
          .post(this.apis[this.option.apiType || 'default'].getGroupUrl, params)
          .then(res => {
            this.accessoriesList = res.data
          })
          .catch(() => {
            this.accessoriesList = []
          })
      } else {
        this.accessoriesList = []
      }
    },
    // 获取配件
    getStock() {
      if (this.accessoriesList.length && this.accessoriesList[this.accessoriesIndex]) {
        this.$api
          .post(this.apis[this.option.apiType || 'default'].getStockUrl, {
            ...this.accessoriesParams,
            materialGroupName: this.accessoriesList[this.accessoriesIndex],
            partsName: this.partsName
          })
          .then(res => {
            this.list = res.data
          })
          .catch(() => {
            this.list = []
          })
      } else {
        this.list = []
      }
    },
    // 搜索按钮
    search(val) {
      this.partsName = val
    },
    // 监听下拉选项
    bindPickerChange(e) {
      this.index = e.detail.value
    },
    // 滚动到底部
    carScrolltolower(e) {},
    // 触发下拉刷新
    carRefresherrefresh(e) {
      this.refresherTriggered = true
      setTimeout(() => {
        this.refresherTriggered = false
      }, 3000)
    },
    // 下拉刷新结束
    carRefresherrestore(e) {},
    // 点击购买配件
    buyAccessories() {}
  }
}
