/*******************************************************************************
 * Copyright(c) 2021 dongke All rights reserved. / Confidential
 * 类的信息： 
 *		1.程序名称：
 *		2.功能描述：客户收款新建
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		        2022-5-25		        1.00		   	    新建
 *******************************************************************************/

const Constants = require('@/utils/Constants.js');
const utils = require('@/utils/util')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: [
      { name: 'main' },
    ],
    contentObj: {
      main: [
        { code: 'accDate', name: 'accDate', type: 'date', required: true, },
        { code: 'staffId', name: 'staffName', required: true, title: mixins.$t("staffId"), type: 'choose', urlKey: 'chooseStaff' },
        { code: 'orgId', name: 'orgName', required: true, type: 'choose', urlKey: 'chooseOrg' },
        { code: 'sumAmtRec', type: 'number', negative: true, maxFlag: false, title: mixins.$t("thisRefund"), readonly: false },
        { code: 'remarks', type: 'textarea', title: mixins.$t("remarks"), readonly: false },
        { code: 'file', type: 'uploader', title: mixins.$t("uploader") }
      ],

    },
    objInfo: {},
    buttonSaveList: [{ code: 'add', title: mixins.$t("refund"), color: '#CAA977' }],
    contentSaveList: [{ code: 'need', title: mixins.$t("cusReturnCollectionAmt"), type: 'str' }, { code: 'amount', content: 0, type: 'number' }],
    routeObjName: 'cusReceiptPayment',
    accountService: app.globalData['accountService'],
  },
  /**
* @desc : 计算需收款
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  changeNumberBlur(e) {
    let formData = JSON.parse(this.data.formData)
    let contentSaveList = this.data.contentSaveList
    contentSaveList[1].content = formData.sumAmtRec ? formData.sumAmtRec : 0

    this.setData({
      contentSaveList: contentSaveList
    })
  },
  chooseObjTypeData(e) {
    let formData = JSON.parse(this.data.formData)
    console.log('chooseObjTypeData', formData)
  },


  /**
   * @desc   : 选择回调
   * @author : 于继渤
   * @date   : 2024/1/26 11:46
   */
  chooseData(e) {
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = e.detail.data.data
    if (code == "staffId") { //员工
      formData.staffId = data.id
      formData.staffName = data.name
    }
    if (code == "orgId") { //部门
      formData.orgId = data.id
      formData.orgName = data.name
    }
    this.setData({
      formData: JSON.stringify(formData)
    })
  },
  /**
  * @desc : 保存数据服务-跳转收款页面
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  open(e) {
    let _this = this
    wx.navigateTo({
      url: this.data.route.selectCollectionConfirm.url,
      events: {
        // 回调后
        bindData: function (data) {
          // 保存成功后 回调
          wx.navigateBack({
            data: 1
          })
        }
      },
      success: function (res) {
        //设置参数
        let item = _this.setSaveData()
        res.eventChannel.emit('params', { item: item, formType: 4 })
      }
    })
  },

  /**
    * @desc : 处理保存参数
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  setSaveData() {
    let formData = JSON.parse(this.data.formData)
    let param = formData
    param.cusName = formData.cusName
    param.objInfo = {}
    param.objInfo['cusPhone'] = formData.cusPhone
    param.objInfo['cusName'] = formData.cusName
    param.objInfo['objectId'] = formData.cusId
    param.objInfo['addressFull'] = formData.addressFull
    param.cusName = formData.cusName
    param.cusPhone = formData.cusPhone
    param.addressFull = formData.addressFull
    if (this.data.formMode !== Constants.formMode.other) {
      param.orgId = app.globalData.user.orgId
      param.orgName = app.globalData.user.orgName
      param.staffId = app.globalData.user.staffId
      param.staffName = app.globalData.user.staffName
      param.objectId = formData.cusId
    } else {
      //编辑退款
      param.editFlag = true
      this.data.dataItem.forEach(res=>{
        res.checked = true
        res.sumAmount =  Number(res.amtRec).toFixed(2)
      })
      param.dataItem = this.data.dataItem
    }
    param.annexPaths = formData.annexPaths
    param.rpType = '收付款类型-退收款'
    param.sumAmtRec = formData.sumAmtRec
    param.sumAmount = formData.sumAmtRec
    console.log(param)
    return JSON.stringify(param)
  },

  /**
    * @desc : 编辑用
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  getRpForUpdate(id) {
    return this.excute(this.data.service, this.data.service.getRpForUpdate + '/' + id, {}).then(res => {
      if (res.data.code == 200) {
        console.log('res', res)
        let data = res.data.data.data
        data.customerId = data.cusId
        data.objInfo = {
          cusId: data.cusId,
          cusName: data.cusName,
          cusPhone: data.cusPhone,
          cusCode: data.cusCode
        }
        this.setData({
          formData: JSON.stringify(data),
          dataItem:res.data.data.dataItem
        })
        this.changeNumberBlur()
      }
    });
  },




  /**
     * @desc : 给表单赋值
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
  setValuesByEdit(data) {
    data.customerId = data.cusId
    data.objInfo = {
      cusId: data.cusId,
      cusName: data.cusName,
      cusPhone: data.cusPhone,
      cusCode: data.cusCode
    }
    this.setData({
      formData: JSON.stringify(data)
    })
  },


  /**
  * 生命周期函数--监听页面加载
  */
  loadInit() {
    let formData = JSON.parse(this.data.formData)
    formData.orgId = app.globalData.user.orgId
    formData.orgName = app.globalData.user.orgName
    formData.staffId = app.globalData.user.staffId
    formData.staffName = app.globalData.user.staffName
    this.setData({
      formData: JSON.stringify(formData)
    })
    if (this.data.formMode == Constants.formMode.other) {
      this.getRpForUpdate(this.data.id)
    }


  },
})