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

const Constants = require('@/utils/Constants.js');
import Dialog from '@/dist/dialog/dialog.js';
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    cardList: [
      { name: 'main' },
    ],
    contentObj: {
      main: [
        { code: 'cusName', type: 'selectIcon', urlKey: 'selectCustomers', required: true, readonly: true, title: mixins.$t('customerName') },
        { code: 'cusPhone', type: 'phone', title: mixins.$t('cusPhone'), required: true, readonly: true, },
        { code: 'addressFull', type: 'address', title: mixins.$t('adddressFull'), required: false, readonly: true, },
        { code: 'sumAmtRec', type: 'number', title: mixins.$t("amountCollected"), 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("collection"), color: '#CAA977' }],
    contentSaveList: [{ code: 'need', title: mixins.$t("saleCollection"), 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
    })
  },

  /**
  * @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: 3 })
      }
    })
  },

  /**
    * @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

    return JSON.stringify(param)
  },

  /**
    * @desc   : 选择回调
    * @author : 于继渤
    * @date   : 2024/1/26 11:46
    */
  chooseData(e) {
    console.log('eee1', e);
    let item = e.detail.data
    let code = e.detail.code
    let formData = JSON.parse(this.data.formData)
    //设置客户信息
    if (code == 'cusName') {
      formData.cusCode = item.cusCode
      formData.cusId = item.cusId
      formData.cusName = item.cusName
      formData.cusPhone = item.cusPhone
      formData.cusFromName = item.cusFromName
      formData.cusFrom = item.cusFrom
      //处理地址
      formData.address = {
        address: item.addressFull,
        addressFull: item.addressFull,
        addressArea: item.addressArea,
        addressGcj02: item.addressGcj02,
        addressName: item.addressName,
      }
      formData.addressArea = item.addressArea
      formData.addressFull = item.addressFull
      formData.addressGcj02 = item.addressGcj02
      formData.addressName = item.addressName
      formData.addressNo = item.addressNo
      //订单销售渠道 ,默认取的客户的渠道
      formData.salesChannel = item.channelId
      formData.channelId = item.channelId
      formData.channelName = item.channelName
      formData.contactPhone = item.contactPhone
      formData.contactPhone = item.contactPhone
    }
    ///设置主从业务员
    if (code == 'staff') {
      formData.staff = item.staffItem
      formData['staffId'] = item.staffMain.staffId
      formData['staffName'] = item.staffMain.staffName
      formData['staffList'] = item.staffList
      console.log(item.staffList)
    }
    ///设置主从业务部门
    if (code == 'org') {
      console.log('item', item)
      formData.org = item.orgItem
      formData['orgId'] = item.orgMain.orgId
      formData['orgName'] = item.orgMain.orgName
      formData['orgList'] = item.orgList
    }
    this.setData({
      formData: JSON.stringify(formData)
    })

  },




  /**
     * @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
        }
        //处理地址
        data.address = {
          address: data.addressFull,
          addressFull: data.addressFull,
          addressArea: data.addressArea,
          addressGcj02: data.addressGcj02,
          addressName: data.addressName,
        }
        //处理顶部
        data['staff'] = {
          text: data.staffName,
          value: [data.staffId]
        }
        data['org'] = {
          text: data.orgName,
          value: [data.orgId]
        }
        data.orgList = [
          {
            orgId:data.orgId,
            orgName: data.orgName,
            ownerFlag: true,
            allocationRatio: 100,
          }
        ]
        data.staffList = [
          {
            staffId: data.staffId,
            staffName: data.staffName,
            ownerFlag: true,
            allocationRatio: 100,
          }
        ]
        this.setData({
          formData: JSON.stringify(data),
          dataItem: res.data.data.dataItem
        })
        this.changeNumberBlur()
      }
    });
  },
  /**
  * 生命周期函数--监听页面加载
  */
  loadInit() {
    let formData = JSON.parse(this.data.formData)
    formData['staff'] = {
      text: app.globalData.user.staffName,
      value: [app.globalData.user.staffId]
    }
    formData['org'] = {
      text: app.globalData.user.orgName,
      value: [app.globalData.user.orgId]
    }
    formData.orgId = app.globalData.user.orgId
    formData.orgList = [
      {
        orgId: app.globalData.user.orgId,
        orgName: app.globalData.user.orgName,
        ownerFlag: true,
        allocationRatio: 100,
      }
    ]
    formData.staffId = app.globalData.user.staffId
    formData.staffList = [
      {
        staffId: app.globalData.user.staffId,
        staffName: app.globalData.user.staffName,
        ownerFlag: true,
        allocationRatio: 100,
      }
    ]
    //编辑收款
    if (this.data.formMode == Constants.formMode.other) {
      this.getRpForUpdate(this.data.id)
    }



    this.setData({
      formData: JSON.stringify(formData)
    })

  },
})