/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		于继渤		 2024-1-23			1.00		    客户档案新增
 *******************************************************************************/
const Constants = require('@/utils/Constants.js');
const util = require('@/utils/util.js')
const mixins = require('@/mixins/index.js')
const app = getApp()
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'customer',
    gradeCode: app.globalData.company.gradeCode,
    saleChannelService: app.globalData['saleChannelService'],
    cardList: ['main', 'center', 'remarks'],
    buttonSaveList: [{ code: 'add', title: mixins.$t('save'), width: '120rpx' }],
    contentObj: {
      main: [
        { code: 'cusName', type: 'str', required: true, title: mixins.$t('customerName') },
        { code: 'cusPhone', type: 'phone', title: mixins.$t('cusPhone'), required: true },
        { code: 'addressFull', type: 'address', title: mixins.$t('addressFull'), required: true },
        { code: 'addressNo', type: 'str', required: true, title: mixins.$t('addressNo') },
        { code: 'cusFrom', name: 'cusFromName', required: true, title: mixins.$t('customerSource'), type: 'choose', urlKey: 'choosecusFrom' },
        { code: 'contactName', type: 'str', required: false, title: mixins.$t('contactName') },
        { code: 'contactPhone', type: 'str', title: mixins.$t('cpPhone'), required: false },
      ],
      center: [
        // { code: 'channelId', name: 'channelName', required: true, title: mixins.$t('saleChannel'), type: 'choose', urlKey: 'chooseChannel' },
        // { code: 'staffId', name: 'staffName', type: 'choose', required: true, title: mixins.$t('saleStaff'), urlKey: 'chooseStaff' },
        // { code: 'orgId', name: 'orgName', type: 'choose', required: true, title: mixins.$t('saleOrg'), urlKey: 'chooseOrg' },
        // { code: 'initialPaymentId', name: 'initialPayment', type: 'choose', title: mixins.$t('initialRPayment'), urlKey: 'chooseInitialPaymentType' },
      ],
      remarks: [
        { code: 'remarks', type: 'textarea', title: mixins.$t('remarks') }
      ]
    },
  },

  /**
* @desc : 选择数据源
* @date : 2024/2/1 15:49
* @author : 于继渤
*/
  chooseData(e) { 
    let formData = JSON.parse(this.data.formData)
    let code = e.detail.code
    let data = null
    if (code == "initialPaymentId") {
      data = e.detail.list
    } else {
      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
    }
    //销售渠道
    if (code == 'channelId') {
      formData.channelId = data.id
      formData.channelCode = data.code
      formData.channelName = data.name
    }
    //客户来源
    if (code == 'cusFrom') {
      formData.cusFrom = data.id
      formData.cusFromCode = data.code
      formData.cusFromName = data.name
    }
    //客户来源
    if (code == 'macId') {
      formData.macId = data.id
      formData.macCode = data.code
      formData.macName = data.name
    }
    //初始款项
    if (code == "initialPaymentId") {
      formData.initialPaymentId = data.id
      formData.initialPayment = data.name
      let contentObj = this.data.contentObj;
      let center = contentObj.center
      center = center.filter(it => it.code != 'startAmount' && it.code != 'macId')
      // 初始款项-欠款
      if (data.id == Constants.initialPayType.debt) {
        center.push({ code: 'startAmount', type: 'number', required: true, min: Constants.MIN_VALUE, title: mixins.$t('initialDebtOwed') })
      } else if (data.id == Constants.initialPayType.payment) {
        // 初始款项-预收
        center.push({ code: 'startAmount', type: 'number', required: true, title: mixins.$t('initialPayment') })
        center.push({ code: 'macId', name: 'macName', type: 'choose', required: true, title: mixins.$t('moneyAccount'), urlKey: 'chooseMoneyAccount' })
      }
      contentObj.center = center
      this.setData({
        contentObj: contentObj
      })
    }
    this.setData({
      formData: JSON.stringify(formData)
    })
  },


  /**
  * @desc : 保存数据服务
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  saveData() {

    if (this.data.formMode == Constants.formMode.edit) {
      return this.excute(this.data.service, this.data.service.update, this.data.params);
    } else {
      return this.excute(this.data.service, this.data.service.insert, this.data.params);
    }
  },

  /**
     * @desc : 处理保存后续事件
     * @date : 2024/2/1 15:49
     * @author : 姜永辉
     */
  handleSaveData() {
    //设置默认值 
    let formData = {}
    if (this.data.channelDef) {
      formData.channelId = this.data.channelDef.channelId
      formData.channelName = this.data.channelDef.channelName
      formData.channelCode = this.data.channelDef.channelCode
    }
    formData.staffId = app.globalData.user.staffId
    formData.staffName = app.globalData.user.staffName
    formData.orgId = app.globalData.user.orgId
    formData.orgName = app.globalData.user.orgName
    //设置默认值
    this.setData({
      formData: JSON.stringify(formData),
    })
  },

  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setValuesByEdit(data) {
    //处理地址
    data.address = {
      address: data.addressFull,
      addressFull: data.addressFull,
      addressArea: data.addressArea,
      addressGcj02: data.addressGcj02,
      addressName: data.addressName,
    }
    this.setData({
      formData: JSON.stringify(data)
    })
    // 控制初始欠款不允许编辑
    let contentObj = this.data.contentObj;
    let filters = contentObj.center.filter(it => it.code == 'initialPaymentId')

    if (filters && filters.length > 0) {
      filters[0].readonly = true;
    }
    this.setData({
      contentObj: contentObj
    })
  },

  /**
  * @desc : 设置销售渠道/人员/业务部门
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  getDefChannel() {
    this.excute(this.data.saleChannelService, this.data.saleChannelService.selectByCond, {
      flgDefault: true
    }).then(res => {
      if (res.data.code == 200) {
        let formData = JSON.parse(this.data.formData)
        if (res.data.data.list && res.data.data.list.length > 0) {
          formData.channelId = res.data.data.list[0].channelId
          formData.channelName = res.data.data.list[0].channelName
          formData.channelCode = res.data.data.list[0].channelCode
        }
        formData.staffId = app.globalData.user.staffId
        formData.staffName = app.globalData.user.staffName
        formData.orgId = app.globalData.user.orgId
        formData.orgName = app.globalData.user.orgName
        //设置默认值
        this.setData({
          formData: JSON.stringify(formData),
          channelDef: res.data.data.list[0]
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  loadInit(options) {
    //TODO  销售渠道默认 零售 ,销售部门销售员工默认当前登录人的
    let contentObj = this.data.contentObj
    if (this.data.gradeCode == Constants.gradeCode.STD) {

      contentObj.main = [
        { code: 'cusName', type: 'str', required: true, title: mixins.$t('customerName') },
        { code: 'cusPhone', type: 'phone', title: mixins.$t('cusPhone'), required: true },
        { code: 'addressFull', type: 'address', title: mixins.$t('addressFull'), required: true },
        { code: 'addressNo', type: 'str', required: true, title: mixins.$t('addressNo') },
        // { code: 'cusFrom', name: 'cusFromName', required: true, title: mixins.$t('customerSource'), type: 'choose', urlKey: 'choosecusFrom' },
        { code: 'contactName', type: 'str', required: false, title: mixins.$t('contactName') },
        { code: 'contactPhone', type: 'str', title: mixins.$t('cpPhone'), required: false },
      ]
      contentObj.center = [
        { code: 'initialPaymentId', name: 'initialPayment', type: 'drop', title: mixins.$t('initialRPayment'), urlKey: 'chooseInitialPaymentType', dropType: 'initialPayment' },
      ]

    } else if (this.data.gradeCode == Constants.gradeCode.PRO) {

      contentObj.main = [
        { code: 'cusName', type: 'str', required: true, title: mixins.$t('customerName') },
        { code: 'cusPhone', type: 'phone', title: mixins.$t('cusPhone'), required: true },
        { code: 'addressFull', type: 'address', title: mixins.$t('addressFull'), required: true },
        { code: 'addressNo', type: 'str', required: true, title: mixins.$t('addressNo') },
        { code: 'cusFrom', name: 'cusFromName', required: false, title: mixins.$t('customerSource'), type: 'choose', urlKey: 'choosecusFrom' },
        { code: 'contactName', type: 'str', required: false, title: mixins.$t('contactName') },
        { code: 'contactPhone', type: 'str', title: mixins.$t('cpPhone'), required: false },
      ]
      contentObj.center = [
        { code: 'staffId', name: 'staffName', type: 'choose', required: true, title: mixins.$t('reportStaff'), urlKey: 'chooseStaff' },
        { code: 'orgId', name: 'orgName', type: 'choose', required: true, title: mixins.$t('reportOrg'), urlKey: 'chooseOrg' },
        { code: 'initialPaymentId', name: 'initialPayment', type: 'drop', title: mixins.$t('initialRPayment'), urlKey: 'chooseInitialPaymentType', dropType: 'initialPayment'},
      ]
    }
    this.setData({
      contentObj: contentObj
    })

    if (this.data.formMode == Constants.formMode.edit) {
      wx.setNavigationBarTitle({
        title: mixins.$t('editCus'),
      })
    } else {
      //设置默认值
      this.getDefChannel()
    }
  },

})