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

Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    returnTotalAmount: 0,
    allChecked: false,
    tableDataItem:{},
    // 路由
    routeObjName: 'saleOutBound',
    orderService: app.globalData['orderService'],
    tableData: [],
    choooseInboundItemList: [],
    buttonSaveList: [{ code: 'add', title: mixins.$t('confirm'), width: '120rpx',  }],
    contentSaveList: [{ 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年3月8日
   * @author : 王英杰
   */
  handleSearchData(tableData) {
   
    tableData.forEach(item=>{
      item.outingQty = Number(item.itemQty  - item.outQty) 
    })
    this.setData({
      tableData:tableData
    })
  },

 
  /**
 * @desc : 设置查询参数
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  setSearchParams(params) {
    //销售状态
    params.outStatusList = [Constants.outStatus.unOutBound]
    params.cusId = this.data.item.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.forEach(res => {
      res.checked = false
      res.orderItemResponseList.forEach(item => {
        item.checked = false
      })
    })
    tableData[index].checked = e.detail
    let orderItemResponseList = tableData[index].orderItemResponseList
    if (orderItemResponseList && orderItemResponseList.length > 0) {
      orderItemResponseList.forEach(res => {
        res.checked = tableData[index].checked
      })
    }
    if(e.detail){
      this.setData({
        tableDataItem: tableData[index]
      })
    }
    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.forEach(item => {
    //   item.checked = false
    //   item.orderItemResponseList.forEach(it => {
    //     it.checked = false
    //   })
    // })
    tableData[index].orderItemResponseList[index_].checked = e.detail
    if(e.detail){
      this.setData({
        tableDataItem: tableData[index]
      })
    }
    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.priceSale
            item.outingQty = Number(item.itemQty  - item.outQty) 
            item.outingAmt = item.priceOut * item.outingQty
            //设置最大值
            item['outingQty_min'] = 1
            item['outingQty_max'] = item.outingQty
            delete item['priceLimited']
            delete item['pricePurchase']
            delete item['priceWholesale']
            choooseInboundItemList.push(item)
          }
        })
      }
    })
    if (choooseInboundItemList && choooseInboundItemList.length > 0) {
      choooseInboundItemList.forEach(res => {
        returnTotalAmount += Number(res.itemQty * res.priceSale)
      })
    }

    contentSaveList[1]['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 , item:this.data.tableDataItem, fromId: itemList[0].orderId })
    wx.navigateBack({
      data: 1
    })

  },

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

  },


})