const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    returnTotalAmount: 0,
    allChecked: false,
    // 路由
    routeObjName: 'inbound',
    tableData: [],
    choooseInboundItemList: [],
    buttonSaveList: [{ code: 'add', title:  mixins.$t('confirm'), width: '120rpx', color: '#1B365D' }],
    contentSaveList: [{code:'flag',title:'全退',type:'checkbox'},{code:'need',title:'退货总额',type:'str'},{code:'amount',content:0,type:'number'}],
  },
  /**
   * @desc :   加载数据源
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  getData(params) {
    return this.excute(this.data.service, this.data.service.selectInboundAndItem, params);
  },

  /**
  * @desc :   设置查询参数
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  setSearchParams(params) {
    params.intoType = Constants.intoType.pur
    params.intoStatus = Constants.intoStatus.inBounded
    params.fromId = this.data.id
    return params
  },
  /**
   * @desc :   父级商品信息选择
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  onListItemChange(e) {
    let tableData = this.data.tableData
    let index = e.currentTarget.dataset.index
    tableData[index].checked = !tableData[index].checked
    let inboundItemList = tableData[index].inboundItemList
    if (inboundItemList && inboundItemList.length > 0) {
      inboundItemList.forEach(res => {
        res.checked = tableData[index].checked
      })
    }

    this.setData({
      tableData: tableData
    })
    this.calculateTotal()
  },
  /**
   * @desc :   子级商品信息选择
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  onChangeItemCheckbox(e) {
    let tableData = this.data.tableData
    let index = e.currentTarget.dataset.index
    let index_ = e.currentTarget.dataset.index_
    console.log(index, index_)
    tableData[index].inboundItemList[index_].checked = !tableData[index].inboundItemList[index_].checked

    this.setData({
      tableData: tableData
    })

    this.calculateTotal()
  },

  /**
    * @desc :  全退
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  onCheckboxChange(e) {
    console.log(e.detail.checked)
    let checked = e.detail.checked
    let tableData = this.data.tableData
    tableData.forEach(res => {
      res.checked = checked
      if (res.inboundItemList && res.inboundItemList.length > 0) {
        res.inboundItemList.forEach(item => {
          item.checked = checked
        })
      }
    })
    this.setData({
      tableData: tableData
    })
    this.calculateTotal()
  },

  /**
    * @desc :   计算金额/过滤出已选数据
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  calculateTotal() {
    let tableData = this.data.tableData
    let contentSaveList = this.data.contentSaveList
    let choooseInboundItemList = []
    let returnTotalAmount = Number(0)
    //过滤出已选数据
    tableData.forEach(res => {
      if (res.inboundItemList && res.inboundItemList.length > 0) {
        res.inboundItemList.forEach(item => {
          if (item.checked) {
            item.itemQty = Number(item.rejectQty * -1)
            item.itemQtyMin = Number(item.rejectQty * -1)
            item.itemQtyMax = Number(-1)
            item.priceReturn = Number(item.priceInto * -1)
            item.itemAmt = Number(item.rejectQty * item.priceInto * -1)
            choooseInboundItemList.push(item)
          }
        })
      }
    })
    if (choooseInboundItemList && choooseInboundItemList.length > 0) {
      choooseInboundItemList.forEach(res => {
        returnTotalAmount += Number(res.rejectQty * res.priceInto)
      })
    }

    contentSaveList[2]['content'] = returnTotalAmount.toFixed(2)

    this.setData({
      returnTotalAmount:returnTotalAmount,
      choooseInboundItemList: choooseInboundItemList,
      contentSaveList: contentSaveList,
    })
  },
  /**
  * @desc :   跳转退货页
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toAdd() {
    let choooseInboundItemList = this.data.choooseInboundItemList
    let that = this
    let formData = that.data.formData
    let item = that.data.item
    if(this.data.id){
      wx.navigateTo({
        url: this.data.route.addReturn.url,
  
        success: function (res) {
          // 通过eventChannel向被打开页面传送数据 TODO 测试例子url是写死的，实际中，需要从route中读取
          res.eventChannel.emit('params', {
            formMode:'add',
            choooseInboundItemList: choooseInboundItemList,
            beforeSumAmount: that.data.returnTotalAmount * -1,
            item: JSON.stringify(item)
          })
        }
      })
    }else{
      const eventChannel = this.getOpenerEventChannel();
      eventChannel.emit('bindData', {
        data:{itemList: choooseInboundItemList,
          beforeSumAmount: that.data.returnTotalAmount * -1}
        
      })
      wx.navigateBack({
        data: 1
      })
    }
 

    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },


})