SublistModal.js 5.23 KB
// components/SelectInvoiceModal/SelectInvoiceModal.js
import commonApi from '~/api/api'
import api from '~/tabWorkOrder/api'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    visible: {
      type: Boolean,
      value: false,
      observer: function (n, o) {
        this.handleOpen()
      }
    },
    orderInfo: {
      type: Object,
      value: {}
    },
    orderCode: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // selectType: 82,
    constructionList: [],
    loading: false,
    first: true
  },

  /**
   * 组件的方法列表
   */
  methods: {
	openInventoryPopup(e){
		console.log(e)
		let {inventory:{
			upgradeGoodsCommodityCode,
			upgradeGoodsCustomCode,
			specification,
			stock
		  }} = e.target.dataset
		  const inventoryData ={
			upgradeGoodsCommodityCode,
			upgradeGoodsCustomCode,
			specification,
			stock
		  }
		this.triggerEvent('openInventory', inventoryData)
	},
    selectType(e) {
      let { type } = e.currentTarget.dataset
      this.setData({
        selectType: type
      })
    },
    switchValue(e) {
      // let {constructionList} = this.data
      let { index, value } = e.currentTarget.dataset
      this.setData({
        first: false,
        ['constructionList[' + index + '].constructionFlag']: value
      })
    },
    handleOpen() {
      let { orderCode, first } = this.data
      api.listSubOrder({ orderCode }).then(res => {
        console.log(res.data, 'constructionList')
        first = res.data.every(item => ['1', '2', 1, 2].includes(item.constructionFlag))
        this.setData({
          constructionList: res.data,
          first: !first
        })
      })
      wx.$modal(
        this,
        {
          show: true,
          tabTitleShow: true,
          success: res => {
            // 跳转新增订单页面 并带特定参数
            let { selectType, orderInfo, loading } = this.data
            if (loading) {
              return true
            }
            console.log(orderInfo, 'orderInfo----')
            const params = {
              orderCode: orderInfo.orderCode,
              invoiceType: selectType
            }
            wx.$util.showLoading()
            this.setData({
              loading: true
            })
            commonApi
              .createOrderCode(params)
              .then(res => {
                wx.$util.hideLoading()
                if (res.code == 0) {
                  //   wx.$util.showToast('开票成功')
                  wx.$router.go('tabWorkOrder.invoiceCode', {
                    obj: {
                      orderCode: orderInfo.orderCode,
                      ...res.data
                    }
                  })
                } else {
                  wx.$util.showToast(res.message)
                }
              })
              .finally(() => {
                this.setData({
                  loading: false
                })
              })
          },
          error: () => { }
        },
        '#modal'
      )
    },
    handleCancel() {
      this.triggerEvent('cancel')
    },
    handleConfirm() {
      let { constructionList, orderCode, first, requesting } = this.data
      if (constructionList.length == 0 || first) return false

      if (constructionList.length > 0) {
        let flag = constructionList.every(item => {
          // console.log(item);
          return ['1', '2', 1, 2].includes(item.constructionFlag)
        })
        // console.log(flag, 'constructionFlag');
        if (!flag) {
          wx.$util.showToast('需要勾选全部施工商品')
          return
        }
        // 如果全部不施工,直接调用接口
        let no = constructionList.every(item => {
          return item.constructionFlag == 2
        })
        if (no) {
          const constructionInfoList = constructionList.map(val => {
            return {
              subOrderCode: val.subOrderCode,
              upgradeGoodsId: val.upgradeGoodsId,
              upgradeGoodsName: val.upgradeGoodsName,
			  constructionFlag: val.constructionFlag,
			  upgradeGoodsCommodityCode:val.upgradeGoodsCommodityCode,
			  upgradeGoodsCustomCode:val.upgradeGoodsCustomCode,
			  specification:val.specification,
			  stock:val.stock
            }
          })
          let params = {
            subOrderRelationList: constructionInfoList,
            orderCode
            // subOrderRelationList: []
          }
          if (requesting) return false
          this.setData({
            requesting: true
          })
          api.batchAdd(params).then(res => {
            let { code, message } = res
            if (code == 0) {
              wx.$util.showToast('提交成功')
              this.handleCancel()
              let thisPage = wx.$router.getPageInstance()
              thisPage.onRoute({ orderCode })
            } else {
              wx.$util.showToast(message)
            }

            setTimeout(() => {
              this.setData({
                requesting: false
              })
            }, 1000)
          })
          return false
        }
      }
      this.handleCancel()
      // this.triggerEvent('confirm', constructionList)
      wx.$router.go('tabWorkOrder.createAConstructionOrder', {
        constructionList,
        orderCode
      })
    }
  }
})