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: {
    table: Constants.tables.income,
    cardList: [{
        name: 'main'
      },
      {
        name: 'items',
        title: mixins.$t('meet'),//'应付',
        chooseFlag: true,
        addFlag: false,
        chooseTitle:  mixins.$t('selectCategory'),//'选类别',
        urlKey: 'selectIncomeCategory'
      },
      {
        name: 'file'
      }
    ],
    contentObj: {
      main: [{
          code: 'accDate',
          name: 'accDate',
          title:  mixins.$t('paymentDate'),//'收款日期',
          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: 'sumAmount',
          type: 'number',
          title: mixins.$t('thisPaymentgo'),//'本次付款',
          required: true
        },

      ],
      items: [{
          code: 'receivableTypeName',
          name: '',
          type: 'str',
          title: mixins.$t('expenditureCategory'),//'支出类别',
          readonly: true,
          required: true
        },
        {
          code: 'amtPayable',
          type: 'number',
          title: mixins.$t('paymentAmount'),//''付款金额',
          required: true,
          control:'receivableTypeName'
        },

      ],
      file: [{
          code: 'remarks',
          type: 'textarea',
          title: mixins.$t('remarks'),//'备注'
          required: true,
          readonly:true,
        },
        {
          code: 'file',
          type: 'uploader',
          title: mixins.$t('uploader'),//'附件'
          required: true,
          readonly:true,
        }
      ]
    },
    totalObj: {
      items: [{
          code: 'receivableWaive',
          title: mixins.$t('totalPayable'),//'合计应付',
        },
        {
          code: 'verificationAmount',
          title: mixins.$t('verificationAmount'),//'核销金额',
        },
      ],
    },
    
    contentList: [{
      code: 'need',
      title: mixins.$t('totalAmount'),
      type: 'str'
    }, {
      code: 'amount',
      content: 0,
      type: 'number'
    }],
    btnFormData: null,
    routeObjName: 'otherPayable'
  },
    /**
     * @desc : 详细页面 （编辑）
     * @date : 2024/2/1 15:49
     * @author : 王英杰
     */
    toEdit(e) {
      let formData = JSON.parse(this.data.formData)
      let payableId = formData.payableId
      let that = this
      wx.navigateTo({
          url: this.data.route.add.url,
          events: {
              // 回调后，在这里给页面赋值
              bandData: function (e) { 
              }
          },
          success: function (res) {
              res.eventChannel.emit('params', {
                  id: payableId,
                  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)
  let cardList = this.data.cardList
  formData = data.data
  data.dataItem.forEach(element => {
    element.dataId = element.payableType
    element.receivableTypeName = element.payableTypeName
  });
  formData.itemList = data.dataItem
  formData.supId = data.data.objectId
 
  let objInfo = {
    supId: data.data.objectId,
    supCode: data.data.supCode,
    supName: data.data.supName,  
  }
  formData.objInfo  = objInfo
  formData.sumAmount  = data.data.sumAmtPayable
  this.setData({
    itemData: data,
    formData: JSON.stringify(formData),
    cardList:cardList,
  })
  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.amtPayable)) { //是数字才能加减
          itemNumber = Number(itemNumber)+ Number(element.amtPayable)
        }
      });
      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
      })
    }
  },
  
  
 
})