const Constants = require('@/utils/Constants.js');
const app = getApp()
const mixins = require('@/mixins/index.js')

Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    returnTotalAmount: 0,
    allChecked: false,
    // 路由
    routeObjName: 'saleOutBound',
    orderService: app.globalData['orderService'],
    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.orderService, this.data.orderService.selectTotalSingleTapeDetailPage, params);
  },
  /**
 * @desc : 设置查询参数
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
setSearchParams(params) {
  //销售状态
  params.outStatusList = [Constants.outStatus.outBounding,Constants.outStatus.unOutBound]
  return params
},
  
  /**
   * @desc :   父级商品信息选择
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  onListItemChange(e) {
    let tableData = this.data.tableData
    let index = e.currentTarget.dataset.index
    tableData.forEach(res=>{
      res.checked =  false
      res.orderItemResponseList.forEach(item=>{
        item.checked = false
      })
    })
    tableData[index].checked = !tableData[index].checked
    let orderItemResponseList = tableData[index].orderItemResponseList
    if (orderItemResponseList && orderItemResponseList.length > 0) {
      orderItemResponseList.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.forEach(res=>{
      res.checked =  false
      // res.orderItemResponseList.forEach(item=>{
      //   item.checked = false
      // })
    })
    tableData[index].orderItemResponseList[index_].checked = !tableData[index].orderItemResponseList[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.orderItemResponseList && res.orderItemResponseList.length > 0) {
        res.orderItemResponseList.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.orderItemResponseList && res.orderItemResponseList.length > 0) {
        res.orderItemResponseList.forEach(item => {
          if (item.checked) {
            item.priceOut = item.priceStandard
            item.outQty = item.itemQty 
            item.outAmt = item.priceOut * item.itemQty
            delete item['priceLimited']
            delete item['pricePurchase']
            delete item['priceWholesale']
            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({
      choooseInboundItemList: choooseInboundItemList,
      contentSaveList: contentSaveList,
    })
  },
  /**
  * @desc :   跳转退货页
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toAdd() {
    let itemList = this.data.choooseInboundItemList
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.emit('bindData', { itemList: itemList,fromNo: itemList[0].orderNo})
    wx.navigateBack({
      data: 1
    })

  },

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

  },


})