/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：  付款单
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		姜永辉      2024-03-22    1.00		   	    
 *******************************************************************************/
const mixins = require('@/mixins/index.js')
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const app = getApp();
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: [
      { name: 'main' },
      { name: 'bill', title: mixins.$t('payment'), chooseFlag: true, chooseTitle: mixins.$t('paymentSelect'), urlKey: 'selectBill' },
      { name: 'file' }],
    contentObj: {
      main: [
        {
          code: 'accDate',
          name: 'accDate',
          type: 'date',
          required: true,
        },
        {
          code: 'staffId',
          name: 'staffName',
          required: true,
          title: mixins.$t('payer'),
          type: 'choose',
          urlKey: 'chooseStaff'
        },
        {
          code: 'orgId',
          name: 'orgName',
          required: true,
          type: 'choose',
          urlKey: 'chooseOrg'
        },
        {
          code: 'sumAmtPay',
          type: 'number',
          title: mixins.$t('thisPaymentgo'),
          required: true
        },

      ],
      bill: [
        { code: 'cancelAmount', type: 'cancel', title: mixins.$t('sumAmtPayableHandle'), bill: 0, amount: 0 },
        { code: 'payableResidue', type: 'number', title: mixins.$t('tobeverifiedAmount'), readonly: true, },
        { code: 'currentPayableResidue', type: 'number', title: mixins.$t('curentVerifiedAmount'), required: true },
        { code: 'sumWaiveAmt', type: 'number', title: mixins.$t('sumWaiveAmt'), },
        { code: 'paymentResidue', type: 'number', title: mixins.$t('prepaidDeduction'), readonly: true, },

      ],
      file: [{
        code: 'remarks', type: 'textarea', title: mixins.$t('remarks')
      },
      { code: 'file', type: 'uploader', title: mixins.$t('uploader') }]
    },
    //应付金额 预付金额 
    amountArr: [
      { title: mixins.$t('payAmount'), code: 'payableResidue' },
      { title: mixins.$t('advancePrepaidAmount'), code: 'paymentResidue' }],
    // 三个汇总金额
    totalObj: {
      bill: [{ title: mixins.$t('paymentAmount'), code: 'paymentAmount' },
      { title: mixins.$t('totalWriteOffAmount'), code: 'totalWriteOffAmount' },
      { title: mixins.$t('toAdvancePrepaidAmount'), code: 'toAdvancePrepaidAmount' }]
    },
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t('payment')
    }],

    btnFormData: null,
    routeObjName: 'payment',
    accountService: app.globalData['accountService'],
    payableResidue: 0,
    paymentResidue: 0,
    bill: 0,
    objInfo: null,
    table: Constants.tables.recPay,
    form:{},
    showPop:false,
    popContent:[],
    // 底部保存按钮
    buttonFootList: [{
      name: 'paymentInfo',
      title: mixins.$t('paymentInfo')
    }],
  },


  /**
  * @desc : 给表单赋值
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  setValuesByEdit(data) {
    console.log("setValuesByEdit", data, this.data.item);
    let contentObj = this.data.contentObj
    let formData = { ...this.data.item }
    let form = {}
    formData.objInfo = this.data.item
    formData.receivableList = data.recPayHandleItem
    formData.objInfo.cusId = this.data.item.objectId
    // 收款总额
    formData.paymentAmount = formData.sumAmtPay
    
    formData.payableResidue = formData.sumShouldHandle
    formData.currentPayableResidue = formData.sumAmtPayableHandle  // 本次核销金额
    formData.totalWriteOffAmount = formData.currentPayableResidue  // 核销总额
    formData.paymentResidue = formData.sumUsePaymentResidue  //预收抵扣
 

    contentObj['bill'].forEach(itcard => {
      if (itcard.code === 'cancelAmount') {
        //单数和金额
        itcard.bill = data.recPayHandleItem.length
        itcard.amount = formData.sumShouldHandle // 待核销金额
      }

    })
    let popContent = []
    if (data.recPayItem!=null && data.recPayItem.length > 0 ){
        for (let index = 0; index < data.recPayItem.length; index++) {
          const element = data.recPayItem[index];
          popContent.push(
            { 
              //新建的 各个控件信息
              code: element.macCode,
              type: 'str',
              title: element.macTypeName, 
              readonly: true
            }
          )
          form[element.macCode] =  Number(element.amtPay).toFixed(2)           
        }
    }

    this.setData({
      itemData: data,
      popContent,
      form:JSON.stringify(form),
      contentObj,
      formData: JSON.stringify(formData),
    })
  },

  /**
  * @desc : 收款详情
  * @date : 2024/2/1 15:49
  * @author : 姜永辉
  */
  openInfo(e) {
    this.setData({
      showPop:true
    })
  },

})