const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    beforeSumAmount: 0,
    afterSumAmount: 0,
    pageFlag: false,
    tableData: [],
    // 路由
    routeObjName: 'inboundReturn',
    cardList: [{
      name: 'items',
      title: mixins.$t("goodsDetail"),
      chooseGoodsFlag: false,
      sumReturnAmt: 0,
      info: '(' + mixins.$t("outOfStorage") + ')',
      color: '#E4002B',
      displayNum: 2
    }, {
      name: 'other',
      title: mixins.$t("otherInfo")
    }],
    contentObj: {
      other: [{
        code: 'remarks',
        type: 'textarea',
        title: mixins.$t("remarks")
      }, {
        code: 'file',
        type: 'uploader',
        title: mixins.$t("uploader")
      }]
    },
    buttonList: [
      { name: 'merge', title: mixins.$t("outProcessing"), }
    ],
    contentList: [
      { code: 'need', title: mixins.$t("totalReturn"), type: 'str' },
      { code: 'sumReturnAmt', content: 0, type: 'number' }
    ],
  },

  /**
  * @desc : 设置保存参数
  * @date : 2024/3/19 15:49
  * @author : 于继渤
  */
  setParams(params) {
    params.itemList = params.goodsList
    params.orgId = app.globalData.user.orgId
    params.staffId = app.globalData.user.staffId
    params.makeStaff = app.globalData.user.staffId
    params.supId = this.data.item.supId
    // params.annexPaths = this.data.item.supId
    delete params['goodsList']
    delete params['sumReturnAmt']
    delete params['sumReturnQty']
    console.log('params', params)
    // return
    return params
  },
  /**
    * @desc : 保存数据服务
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  saveData() {
    if (this.data.item.intoStatus == mixins.$t("inBoundedIntoStatus")) {
      //撤回
      return this.excute(this.data.service, this.data.service.inboundReturnCancel, {
        intoId: this.data.id
      });
    } else {
      //办理
      return this.excute(this.data.service, this.data.service.returnHandle, this.data.params);
    }

  },
  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setValuesByEdit(data) {
    let buttonList = this.data.buttonList
    let cardList = this.data.cardList
    console.log('data', data)
    if (data.intoStatus == mixins.$t("inBoundingIntoStatus") || data.intoStatus == mixins.$t("unInBoundIntoStatus")) {
      cardList[0].info = '(' + mixins.$t("outOfStorage") + ')'
      cardList[0].color = '#E4002B'
    }
    if (data.intoStatus == mixins.$t("inBoundedIntoStatus")) {
      buttonList = [
        { name: 'merge', title: mixins.$t("handleWithdrawal"), }
      ]
      cardList[0].info = '(' + mixins.$t("checkouted") + ')'
      cardList[0].color = ''
    }
    let contentList = this.data.contentList
    contentList[1].content = data.returnAmt
    cardList[0].sumReturnAmt = data.returnAmt
    let formData = JSON.parse(this.data.formData)
    data.goodsList.forEach(res => {
      res['returnQtyMin'] = res.returnQty
      res['returnQtyMax'] =  Number(-1)
      res['priceReturn'] = res.priceInto * -1
    })
    formData = data
    // 采购退货出库单【采购入库单（负数）】编辑：
    // （1）已出库（未退款），可以编辑修改商品、数量（修改商品、数量不能超出入库单（源单）商品范围，更改数量（变大）要校验库存）和备注附件；
    // （2）已出库（已退款），只能编辑备注、附件；
    this.setData({
      beforeSumAmount: data.returnAmt,
      contentList: contentList,
      buttonList: buttonList,
      cardList: cardList,
      formData: JSON.stringify(formData),
      item: data
    })
  },



  /**
  * @desc :商品明细数量加减事件
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  onChangeStepper(e) {
    let index = e.detail.index
    let formData = JSON.parse(this.data.formData)
    formData.goodsList[index].returnQty = e.detail.item
    this.setData({
      formData: JSON.stringify(formData)
    })
    //计算金额
    this.calculateTotal()
  },

  /**
* @desc :计算金额
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  calculateTotal(goodsList) {
    let contentList = this.data.contentList
    let cardList = this.data.cardList
    let beforeSumAmount = this.data.beforeSumAmount
    let sumReturnAmt = Number(0)
    let sumReturnQty = Number(0)
    let formData = JSON.parse(this.data.formData)
    formData.goodsList.forEach(item => {
      item.returnAmt = item.priceInto * item.returnQty
      sumReturnAmt += Number(item.priceInto * item.returnQty)
      sumReturnQty += (item.returnQty)
    })
    console.log('sumReturnQty',sumReturnQty)
    console.log('sumReturnAmt',sumReturnAmt)
    formData.returnQty = sumReturnQty
    formData.returnAmt = sumReturnAmt
    //设置显示
    contentList[1].content = sumReturnAmt
    cardList[0].sumReturnAmt = sumReturnAmt
    //设置退货后总额
    let afterSumAmount = beforeSumAmount - sumReturnAmt
    this.setData({
      afterSumAmount: afterSumAmount,
      contentList: contentList,
      cardList: cardList,
      formData: JSON.stringify(formData)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  loadInit() {


  },


})