/*******************************************************************************
 * Copyright(c) 2021 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.功能描述：单据列表-应收单据 -应付单据
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		jyh		     2022-5-25		        1.00		   	    新建
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const mixins = require('@/mixins/index.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    totalAmount: 0,
    allChecked: false,
    objInfo: {},

    tableData: [
      {
        "addressArea": null,
        "addressFull": null,
        "addressGcj02": null,
        "addressName": null,
        "addressNo": null,
        "annexPaths": null,
        "channelName": null,
        "contactName": null,
        "contactPhone": null,
        "cpId": 1,
        "cusCode": null,
        "cusFromName": null,
        "cusId": null,
        "cusName": null,
        "cusPhone": null,
        "flgValid": true,
        "fromId": "10112024-0320-0000-0000-0000157d995e",
        "fromNo": "PO240320048T995E",
        "goodsList": null,
        "inboundItemList": [
          {
            "costAmt": 0,
            "costPrice": 0,
            "cpId": 1,
            "fromId": "10112024-0320-0000-0000-0000157d995e",
            "fromItemId": "10112024-0320-0000-0000-0000157e51f1",
            "intoAmt": 0,
            "intoId": "10112024-0320-0000-0000-00001581e811",
            "intoItemId": "10112024-0320-0000-0000-00001582320f",
            "intoQty": 0,
            "intoStatus": "入库状态-入库中",
            "intoType": "入库类型-采购入库",
            "intoingAmt": 2,
            "intoingQty": 2,
            "invId": null,
            "invQty": 0,
            "itemId": "10112024-0320-0000-0000-00001582320f",
            "itemIndex": 1,
            "nonStdCode": "-",
            "outQty": 0,
            "priceInto": 1,
            "pricePurchase": 1,
            "priceStd": 2,
            "rejectQty": 0,
            "remarks": null,
            "returnAmt": 0,
            "returnQty": 0,
            "skuCode": "A00Z01S7",
            "skuId": "10112024-0304-0000-0000-00000907f868",
            "skuImages": {
              "path": "https://lingtui-1315959497.cos.ap-beijing.myqcloud.com/20240306152837.jpg"
            },
            "skuModel": "112",
            "skuName": "SSS"
          },
          {
            "costAmt": 0,
            "costPrice": 0,
            "cpId": 1,
            "fromId": "10112024-0320-0000-0000-0000157d995e",
            "fromItemId": "10112024-0320-0000-0000-0000157f4b6a",
            "intoAmt": 0,
            "intoId": "10112024-0320-0000-0000-00001581e811",
            "intoItemId": "10112024-0320-0000-0000-00001583b1c5",
            "intoQty": 0,
            "intoStatus": "入库状态-入库中",
            "intoType": "入库类型-采购入库",
            "intoingAmt": 20.44,
            "intoingQty": 2,
            "invId": null,
            "invQty": 0,
            "itemId": "10112024-0320-0000-0000-00001583b1c5",
            "itemIndex": 2,
            "nonStdCode": "-",
            "outQty": 0,
            "priceInto": 10.22,
            "pricePurchase": 10.22,
            "priceStd": 798,
            "rejectQty": 0,
            "remarks": null,
            "returnAmt": 0,
            "returnQty": 0,
            "skuCode": "AAA1",
            "skuId": "10112024-0228-0000-0000-000001c3cc7e",
            "skuImages": {
              "path": "https://lingtui-1315959497.cos.ap-beijing.myqcloud.com/20240306152837.jpg"
            },
            "skuModel": "AAA",
            "skuName": "AAA"
          },
          {
            "costAmt": 0,
            "costPrice": 0,
            "cpId": 1,
            "fromId": "10112024-0320-0000-0000-0000157d995e",
            "fromItemId": "10112024-0320-0000-0000-000015800512",
            "intoAmt": 0,
            "intoId": "10112024-0320-0000-0000-00001581e811",
            "intoItemId": "10112024-0320-0000-0000-00001584c494",
            "intoQty": 0,
            "intoStatus": "入库状态-入库中",
            "intoType": "入库类型-采购入库",
            "intoingAmt": 20000,
            "intoingQty": 2,
            "invId": null,
            "invQty": 0,
            "itemId": "10112024-0320-0000-0000-00001584c494",
            "itemIndex": 3,
            "nonStdCode": "-",
            "outQty": 0,
            "priceInto": 10000,
            "pricePurchase": 10000,
            "priceStd": 2000,
            "rejectQty": 0,
            "remarks": null,
            "returnAmt": 0,
            "returnQty": 0,
            "skuCode": "HE900AB",
            "skuId": "10112024-0306-0000-0000-00000a70e010",
            "skuImages": null,
            "skuModel": "智能一体机盖板HE900A ",
            "skuName": "智能一体机盖板HE900A "
          }
        ],
        "intoAmt": 0,
        "intoDate": null,
        "intoId": "10112024-0320-0000-0000-00001581e811",
        "intoNo": "AA240320048XE811",
        "intoQty": 0,
        "intoReason": null,
        "intoReasonName": null,
        "intoStatus": "入库状态-入库中",
        "intoStatusName": null,
        "intoType": "入库类型-采购入库",
        "intoTypeName": null,
        "intoingAmt": 10011.22,
        "intoingQty": 6,
        "makeStaff": "10112024-0315-0000-0000-000012034444",
        "makeStaffName": null,
        "makeTime": "2024-03-20 13:52:52",
        "orgId": "10112024-0221-0000-0000-000000011b47",
        "orgName": null,
        "payableId": null,
        "receivableId": null,
        "remarks": null,
        "returnAmt": 0,
        "returnQty": 0,
        "staffId": "10112024-0315-0000-0000-000012034444",
        "staffName": null,
        "sumAmount": null,
        "sumQuantity": null,
        "supId": "10112024-0307-0000-0000-00000c81ca28",
        "supplierName": null,
        "whId": null,
        "whName": null
      },
      {
        "addressArea": null,
        "addressFull": null,
        "addressGcj02": null,
        "addressName": null,
        "addressNo": null,
        "annexPaths": null,
        "channelName": null,
        "contactName": null,
        "contactPhone": null,
        "cpId": 1,
        "cusCode": null,
        "cusFromName": null,
        "cusId": null,
        "cusName": null,
        "cusPhone": null,
        "flgValid": true,
        "fromId": "10112024-0320-0000-0000-0000157d995e",
        "fromNo": "PO240320048T995E",
        "goodsList": null,
        "inboundItemList": [
          {
            "costAmt": 0,
            "costPrice": 0,
            "cpId": 1,
            "fromId": "10112024-0320-0000-0000-0000157d995e",
            "fromItemId": "10112024-0320-0000-0000-0000157e51f1",
            "intoAmt": 0,
            "intoId": "10112024-0320-0000-0000-0000159171f9",
            "intoItemId": "10112024-0320-0000-0000-000015922409",
            "intoQty": 0,
            "intoStatus": "入库状态-入库中",
            "intoType": "入库类型-采购入库",
            "intoingAmt": 2,
            "intoingQty": 2,
            "invId": null,
            "invQty": 0,
            "itemId": "10112024-0320-0000-0000-000015922409",
            "itemIndex": 1,
            "nonStdCode": "-",
            "outQty": 0,
            "priceInto": 1,
            "pricePurchase": 1,
            "priceStd": 2,
            "rejectQty": 0,
            "remarks": null,
            "returnAmt": 0,
            "returnQty": 0,
            "skuCode": "A00Z01S7",
            "skuId": "10112024-0304-0000-0000-00000907f868",
            "skuImages": {
              "path": "https://lingtui-1315959497.cos.ap-beijing.myqcloud.com/20240306152837.jpg"
            },
            "skuModel": "112",
            "skuName": "SSS"
          },
          {
            "costAmt": 0,
            "costPrice": 0,
            "cpId": 1,
            "fromId": "10112024-0320-0000-0000-0000157d995e",
            "fromItemId": "10112024-0320-0000-0000-0000157f4b6a",
            "intoAmt": 0,
            "intoId": "10112024-0320-0000-0000-0000159171f9",
            "intoItemId": "10112024-0320-0000-0000-00001593290f",
            "intoQty": 0,
            "intoStatus": "入库状态-入库中",
            "intoType": "入库类型-采购入库",
            "intoingAmt": 20.44,
            "intoingQty": 2,
            "invId": null,
            "invQty": 0,
            "itemId": "10112024-0320-0000-0000-00001593290f",
            "itemIndex": 2,
            "nonStdCode": "-",
            "outQty": 0,
            "priceInto": 10.22,
            "pricePurchase": 10.22,
            "priceStd": 798,
            "rejectQty": 0,
            "remarks": null,
            "returnAmt": 0,
            "returnQty": 0,
            "skuCode": "AAA1",
            "skuId": "10112024-0228-0000-0000-000001c3cc7e",
            "skuImages": {
              "path": "https://lingtui-1315959497.cos.ap-beijing.myqcloud.com/20240306152837.jpg"
            },
            "skuModel": "AAA",
            "skuName": "AAA"
          }
        ],
        "intoAmt": 0,
        "intoDate": null,
        "intoId": "10112024-0320-0000-0000-0000159171f9",
        "intoNo": "AA240320049D71F9",
        "intoQty": 0,
        "intoReason": null,
        "intoReasonName": null,
        "intoStatus": "入库状态-入库中",
        "intoStatusName": null,
        "intoType": "入库类型-采购入库",
        "intoTypeName": null,
        "intoingAmt": 20022.44,
        "intoingQty": 6,
        "makeStaff": "10112024-0315-0000-0000-000012034444",
        "makeStaffName": null,
        "makeTime": "2024-03-20 13:55:56",
        "orgId": "10112024-0221-0000-0000-000000011b47",
        "orgName": null,
        "payableId": null,
        "receivableId": null,
        "remarks": null,
        "returnAmt": 0,
        "returnQty": 0,
        "staffId": "10112024-0315-0000-0000-000012034444",
        "staffName": null,
        "sumAmount": null,
        "sumQuantity": null,
        "supId": "10112024-0307-0000-0000-00000c81ca28",
        "supplierName": null,
        "whId": null,
        "whName": null
      }
    ],
    choooseInboundItemList: [],
    // 路由
    routeObjName: 'account',
    formMode: Constants.formMode.index,
     // 弹出按钮
     buttonList: [{
      name: 'merge',
      title: mixins.$t('confirm')
    }],
     
  },

  /**
     * @desc : 页面加载数据方法
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
  loadInit() {
    console.log("loadInit");
    this.excute(this.data.service, this.data.service.getAccount + this.data.item.cusId, null).then(res => {
      console.log("loadInit", res);
      this.setData({
        objInfo: res.data.data
      })

    })
  },

  /**
   * @desc :   加载数据源
   * @date : 2024/2/1 15:49
   * @author : 姜永辉
   */
  getData(params) {
    console.log("getData(params)");
    return this.excute(this.data.service, this.data.service.getReceivableAccountItem, params);
  },

  /**
  * @desc :   设置查询参数
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  setSearchParams(params) {
    // 退收款单用的
    if(this.data.item.formType){
      params.formType = this.data.item.formType
    }   
    params.objectId = this.data.item.cusId
    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

    this.setData({
      tableData: tableData
    })
    this.calculateTotal()
  },

  /**
    * @desc :  全退
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  onCheckboxChange(e) {
    let checked = e.detail.checked
    let tableData = this.data.tableData
    tableData.forEach(res => {
      res.checked = checked
    })
    this.setData({
      tableData: tableData
    })
    this.calculateTotal()
  },

  /**
    * @desc :   计算金额/过滤出已选数据
    * @date : 2024/2/1 15:49
    * @author : 姜永辉
    */
  calculateTotal() {
    let tableData = this.data.tableData
    let totalAmount = Number(0)
    //过滤出已选数据
    tableData.forEach(item => {
      if (item.checked) {
        totalAmount += Number(item.amtResidue)
      }
    })
    this.setData({
      totalAmount: totalAmount.toFixed(2),
    })
  },
  /**
  * @desc :   跳转退货页
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  returnData(e) {
    let that = this
    let itemLists = []
    let tableData = that.data.tableData
    if (tableData == null || tableData.length == 0) {
      return
    }
    //过滤出已选数据
    tableData.forEach(item => {
      if (item.checked) {
        itemLists.push(item)
      }
    })
    const eventChannel = that.getOpenerEventChannel();
    eventChannel.emit('bindData', {
      data: {
        receivableList: itemLists,
        objInfo: that.data.objInfo,
        receivableResidue: that.data.totalAmount,
        receiptResidue: that.data.objInfo.receiptResidue, //剩余预存
      }
    })
    wx.navigateBack({
      delta: 1
    })
  },


})