referraCountEdit.js 4.8 KB
// packageWorkspace/pages/validReferralCount/referraCountEdit/referraCountEdit.js
import api from '../../../api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    routerParams: {},
    detailInfo: {},
    loading: false,
    /** 本次草稿/提交的业务场景
     * 1-精品升级(轻改);2-会员权益(广联权益);3-单延保;
     * 用于存草稿/提交后,仅更新当前的场景值
     */
    editCurScene: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      routerParams: JSON.parse(options.info)
    });
    // 提交状态:0-未提交 1-已提交
    wx.setNavigationBarTitle({
      title: (this.data.routerParams.submitStatus == 0 && this.data.routerParams.updateFlag == 1) ? '编辑门店引荐数' : '查看门店引荐数'
    })
    this.getDetail();
  },

  defaultFillVal(data) {
    data.sceneTypeEffectiveReferrals.forEach(item => {
      // updateFlag 是否可编辑:0-不可编辑,1-可编辑(优先级最高)
      // status状态:0-未提交,1-草稿,2-已提交
      if (item.status == 0 && data.updateFlag == 1) {
        if (item.effectiveReferralCountDTOS && item.effectiveReferralCountDTOS.length) {
          item.effectiveReferralCountDTOS.forEach(k => {
            k.effectiveReferralCount = (k.deliveryCount || 0) + ''
          })
        }
      }
    })
    return data || {}
  },

  dealEditedVal(data) {
    let { editCurScene, detailInfo } = this.data
    console.log(detailInfo)
    // updateFlag 是否可编辑:0-不可编辑,1-可编辑(优先级最高)
    // status状态:0-未提交,1-草稿,2-已提交
    if (data.updateFlag == 1) {
      detailInfo.sceneTypeEffectiveReferrals.forEach((item, idx) => {
        let obj = data.sceneTypeEffectiveReferrals.find(k => k.sceneType == item.sceneType)
        if (editCurScene == item.sceneType || obj.status == 2) {
          detailInfo.sceneTypeEffectiveReferrals[idx] = obj
        }
      })
    } else {
      detailInfo = data
    }
    return detailInfo || {}
  },

  // Simulate fetching data from backend
  getDetail() {
    let { merchantId, reviewMonth } = this.data.routerParams
    let { editCurScene } = this.data
    const params = {
      merchantId,
      reviewMonth
    }
    api.getReferralDetail(params).then(res => {
      let { code, data, message } = res;
      if (code == 0) {
        let list = editCurScene != null ? this.dealEditedVal(data) : this.defaultFillVal(data)
        this.setData({
          detailInfo: list,
          editCurScene: null
        })
      }
    })
  },

  handleEditChange(e) {
    const { detailInfo } = this.data
    const updatedItem = e.detail;
    const idx = detailInfo.sceneTypeEffectiveReferrals.findIndex(item => item.sceneType == updatedItem.sceneType)
    detailInfo.sceneTypeEffectiveReferrals[idx] = updatedItem
    this.setData({
      detailInfo
    })
  },

  // Handle Save Draft
  onDraft(e) {
    const updatedItem = e.detail;
    console.log('Save Draft:', updatedItem);
    this.updateLocalList('draft', updatedItem);
  },

  // Handle Submit
  onSubmitItem(e) {
    const updatedItem = e.detail;
    console.log('Submit Item:', updatedItem);

    this.updateLocalList('submit', updatedItem);
  },

  /** typeStatus: 草稿(draft),提交(submit) */
  updateLocalList(typeStatus, updatedItem) {
    let { loading } = this.data
    if (loading) {
      return
    }
    // const targetItem = updatedItem.sceneTypeEffectiveReferrals;
    // const index = list.findIndex(item => item.sceneType === updatedItem.sceneType);
    const tipsObj = {
      'draft': '数据保存成功,未提交复核。',
      'submit': '有效引荐数提交成功。',
    }
    const params = {
      // 1-草稿,2-已提交
      ...updatedItem,
      status: typeStatus == 'submit' ? 2 : 1
    }
    console.log(params, 'params---')
    wx.$util.showLoading()
    this.setData({
      loading: true,
      editCurScene: updatedItem.sceneType || null
    })
    api.referralSubmit(params).then(res => {
      let { code, data, message } = res
      wx.$util.showToast(code == '0' ? `${tipsObj[typeStatus]}` : message);
      this.getDetail()
    }).finally(() => {
      wx.hideLoading();
      this.setData({
        loading: false
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})