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

Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    table: Constants.tables.income,
    cardList: [{
        name: 'main'
      },
      {
        name: 'items',
        title: mixins.$t('receivable'),//'应收',
        chooseFlag: true,
        addFlag: false,
        chooseTitle:  mixins.$t('selectCategory'),//'选类别',
        urlKey: 'selectIncomeCategory'
      },
      {
        name: 'file'
      }
    ],
    contentObj: {
      main: [{
          code: 'accDate',
          name: 'accDate',
          title:  mixins.$t('receiptDate'),//'收款日期',
          type: 'date',
          required: true,
        },
        {
          code: 'staffId',
          name: 'staffName',
          required: true,
          title:  mixins.$t('payee'),//''收款人',
          type: 'choose',
          urlKey: 'chooseStaff'
        },
        {
          code: 'orgId',
          name: 'orgName',
          required: true,
          type: 'choose',
          urlKey: 'chooseOrg'
        },
        {
          code: 'sumAmount',
          type: 'number',
          title: mixins.$t('thisPayment'),//'本次收款',
          required: true
        },

      ],
      items: [{
          code: 'receivableTypeName',
          name: '',
          type: 'str',
          title: mixins.$t('incomeCategory'),//'收入类别',
          readonly: true,
          required: true
        },
        {
          code: 'amtReceivable',
          type: 'number',
          title: mixins.$t('incomeAmount'),//''收入金额',
          required: true
        },

      ],
      file: [{
          code: 'remarks',
          type: 'textarea',
          title: mixins.$t('remarks'),//'备注'
          readonly:true,
        },
        {
          code: 'file',
          type: 'uploader',
          title: mixins.$t('uploader'),//'附件'
          readonly:true,
        }
      ]
    },
    totalObj: {
      items: [{
          code: 'receivableWaive',
          title: mixins.$t('totalAccountsReceivable'),//'合计应收',
        },
        {
          code: 'verificationAmount',
          title: mixins.$t('verificationAmount'),//'核销金额',
        },
      ],
    },
    // 弹出按钮
    buttonList: [{
      name: 'merge',
      title: mixins.$t('collection')
    }],
    contentList: [{
      code: 'need',
      title: mixins.$t('totalAmount'),
      type: 'str'
    }, {
      code: 'amount',
      content: 0,
      type: 'number'
    }],
    btnFormData: null,
    routeObjName: 'otherReceivable'
  },

   /**
     * @desc : 详细页面 （编辑）
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    toEdit(e) {
      let formData = JSON.parse(this.data.formData)
      let receivableId = formData.receivableId
      let that = this
      wx.navigateTo({
          url: this.data.route.add.url,
          events: {
              // 回调后，在这里给页面赋值
              bandData: function (e) { 
              }
          },
          success: function (res) {
              res.eventChannel.emit('params', {
                  id: receivableId,
                  formMode: Constants.formMode.edit
              })
          }
      })
  },

  /**
  * @desc : 给表单赋值
  * @date : 2024/2/1 15:49
  * @author : 王英杰
  */
 setValuesByEdit(data) {
  console.log("setValuesByEdit", data);
  let  formData = JSON.parse(this.data.formData)
  formData = data.data
  data.dataItem.forEach(element => {
    element.dataId = element.receivableType
    element.receivableTypeName = element.receivableTypeName
  });
  formData.itemList = data.dataItem

  let objInfo = {
    cusId: data.data.objectId,
    cusCode: data.data.cusCode,
    cusName: data.data.cusName,
    cusPhone: data.data.cusPhone, 
  }
  formData.objInfo  = objInfo
  formData.sumAmount  = data.data.sumAmtReceivable
  this.setData({
    itemData: data,
    formData: JSON.stringify(formData),
  })
  this.changeItemNumberField()
  this.changeNumber()
},
 
    /**
   * @desc : form表单 上 number类型修改回调
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  changeNumber() {
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    if (formData.itemList) { 
      cardList.forEach(element => {
        if (element.name == 'items') {
          element.verificationAmount = formData.sumAmount
        }
      }); 
  
      this.setData({
        cardList: cardList, 
      })

    }
  },
  /**
   * @desc : 明细里得 收入金额 输入完回调 计算出合计
   * @date : 2024/2/1 15:49
   * @author : 王英杰
   */
  changeItemNumberField() {
    let formData = JSON.parse(this.data.formData)
    let cardList = this.data.cardList
    let contentList = this.data.contentList
    if (formData.itemList) {
      let itemNumber = 0
      formData.itemList.forEach(element => {
        if (!isNaN(element.amtReceivable)) { //是数字才能加减
          itemNumber = Number(itemNumber)+ Number(element.amtReceivable)
        }
      });
      cardList.forEach(element => {
        if (element.name == 'items') {
          element.receivableWaive = itemNumber
        }
      }); 
      contentList.forEach(it => {
        if (it.type == 'number') {
          it.content = itemNumber
        }
      })
      this.setData({
        cardList: cardList,
        contentList:contentList
      })
    }
  },
 
 /**
  * @desc : 保存数据服务-跳转收款页面
  * @date : 2024/2/1 15:49
  * @author : 王英杰
  */
 open(e) {
  // 保存成功后 回调
  wx.navigateBack({
    data: 1
  })
}
})