Goods.js 5.27 KB
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function (n) {
        console.log(n, 'show');
        if (n) {
          // this.getOrderBaseGoodsList(n)
          let thisPage = wx.$router.getPageInstance()
          thisPage.getQuotationInfo(false, 2000)
        }
      }
    },
    goodsData: {
      type: Array,
      observer: function (n) {
        if (n.length > 0) {
          this.getOrderBaseGoodsList(n)
        }
      }
    },

    chooseGoodsList: {
      type: Array,
      observer: function (n) {
        this.setGoodsActive()
      }
    },
    minPrice: {
      type: Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsList: [],
    recordList: [],
    // totalGoodsList:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    cancel() {
      let {
        goodsData
      } = this.data
      this.setData({
        goodsList: goodsData,
      })
      this.setGoodsActive()
    },
    setGoodsActive() {
      let {
        chooseGoodsList,
        goodsList,
        recordList
      } = this.data
      if (chooseGoodsList.length == 0) {
        goodsList.forEach(e => {
          e.active = false
        })
        recordList.forEach(e => {
          e.active = false
        })
      } else {
        // let indexList = []
        goodsList.map((e, index) => {
          e.active = false
          chooseGoodsList.map(s => {
            if (s.goodsId == e.id) {
              e.active = true
            }
          })
        })
        recordList.map((e, index) => {
          e.active = false
          chooseGoodsList.map(s => {
            if (s.goodsId == e.id) {
              e.active = true
            }
          })
        })
      }
      this.setData({
        goodsList,
        recordList
      })
    },
    getOrderBaseGoodsList(data) {
      this.setData({
        keyWord: '',
        goodsList: data,
        recordList: data
      })
    },
    getChooseList() {
      let {
        recordList,
        chooseGoodsList
      } = this.data
      let chooseList = []
      recordList.map(c => {
        // e.upgradeGoodsList.map(c => {
        if (c.active) {
          if (!c.count) c.count = 1
          if (chooseGoodsList.length > 0) {
            chooseGoodsList.map(x => {
              if (x.id == c.id) {
                console.log(x)
                c.price = x.price || 0
              }
            })
          } else {
            c.price = c.upgradePrice || 0
          }
          if (!c.price) c.price = c.upgradePrice
          c.goodsId = c.id
          chooseList.push(c)
        }
        // })
      })
      return chooseList
    },
    choose(e) {
      let {
        item,
        index,
        ind
      } = e.currentTarget.dataset
      let {
        active
      } = item
      let {
        minPrice,
        recordList
      } = this.data
      // console.log(item.goodsInfo.shopMinPrice)
      // console.log(minPrice && minPrice < item.shopMinPrice)
      // 如果有设置订单价格 那就不能选单个成本价比总价还高的
      if (minPrice && minPrice / 100 < item.shopMinPrice) return false
      let target = `goodsList[${ind}].active`
      let recordIndex = recordList.findIndex(el => el.id == item.id)

      this.setData({
        [`recordList[${recordIndex}].active`]: !active,
        [target]: !active
      })
    },
    addCount(e) {
      let {
        index,
        count,
        item,
        ind
      } = e.currentTarget.dataset
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`
      if (!item.count) item.count = 1
      item.count += count
      if (item.count < 1) item.count = 1
      if (item.count > 999) item.count = 999
      this.setData({
        [target]: item.count
      })
    },
    input(e) {
      let {
        index,
        ind
      } = e.currentTarget.dataset
      let target = `goodsList[${index}].upgradeGoodsList[${ind}].count`
      let {
        value
      } = e.detail
      if (value < 1) value = 1
      if (value > 999) value = 999
      this.setData({
        [target]: Number(value)
      })
    },
    searchGoods(e) {
      let {
        goodsData,
        recordList
      } = this.data
      let {
        value
      } = e.detail
      if (!value) {
        this.getOrderBaseGoodsList(recordList)
        return
      }
      let _DATA_ = JSON.parse(JSON.stringify(recordList))
      let arr = []
      _DATA_.map(c => {
        let name = c.upgradeGoodsAlias || c.upgradeGoodsName || c.goodsInfo.goodsName
        console.log(name, 'name');
        if (name.indexOf(value) != -1) {
          arr.push(c)
        }
      })
      console.log(arr)
      this.setData({
        goodsList: arr
      })
    },
    confirm() {
      let chooseList = this.getChooseList()

      let {
        goodsList
      } = this.data
      if (goodsList.length < 1) return
      if (!chooseList.length) return wx.$util.showToast('请勾选商品')
      this.triggerEvent('confirm', chooseList)
      let thisPage = wx.$router.getPageInstance()
      let total = 0
      chooseList.map(i => (total += i.count * i.price))

      thisPage.setData({
        ['orderInfo.vdQuotationGoodsList']: chooseList,
        ['orderInfo.total']: wx.$util.decimal(total, 2)
      })

      this.setData({
        show: false
      })
    }
  }
})