/*******************************************************************************
 * 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: {
    radioFollowUp: '1',
    followStatus: mixins.$t('followStatusFollowUp'),
    routeObjName: 'cusFollow',
    cardList: ['main'],
    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: false },
        { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo') },
        { code: 'intention', name: 'intentionName', type: 'drop', required: true, dropType: 'customerIntention', title: mixins.$t('intention') },
        { code: 'followData', type: 'textarea', required: true, title: mixins.$t('followData') },
        { code: 'nextFollowTime', type: 'dateTime', required: true, title: mixins.$t('nextFollowTime') },
        { code: 'stayTimeLen', name: 'stayTimeLenName', required: true, title: mixins.$t('stayTimeLen'), dropType: 'storeRetentionTime', type: 'drop', },
      ],
    },
  },
  /**
  * @desc : 设置保存参数
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  setParams(params) {
    params.followStatus = this.data.followStatus
    params.followOrg = app.globalData.user.orgId
    return params
  },
  /**
   * @desc : 保存数据服务
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  saveData() {
    return this.excute(this.data.service, this.data.service.insert, this.data.params);
  },
  /**
  * @desc : 编辑
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toEdit() {
    let item = JSON.parse(this.data.formData)
    let url = null
    if (item.followStatus == '跟进状态-跟进' || item.followStatus == '跟进状态-邀约') {
      url = this.data.route.toFollowStatus2.url
    }
    let _this = this;
    wx.navigateTo({
      url: url,
      events: {
        // 保存成功后刷新页面
        refresh: function (data) {
          // 刷新一览界面
          _this.searchData();
        }
      },
      success: function (res) {
        let data = {}
        data.id = item.followId
        data.formMode = Constants.formMode.edit
        res.eventChannel.emit('params', data);
      }
    })
  },
  /**
     * @desc : 给表单赋值
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
  setValuesByEdit(data) {
    console.log(data)
    let radioFollowUp = '1'
    if (data.followStatus == '跟进状态-跟进') {
      radioFollowUp = '1'
    } else if (data.followStatus == '跟进状态-邀约') {
      radioFollowUp = '2'
    } else if (data.followStatus == '跟进状态-约尺') {
      radioFollowUp = '3'
    }
    this.setData({
      radioFollowUp: radioFollowUp
    })
    this.setFormContent(radioFollowUp)
    //处理地址
    data.address = {
      address: data.addressFull,
      addressFull: data.addressFull,
      addressArea: data.addressArea,
      addressGcj02: data.addressGcj02,
      addressName: data.addressName,
    }
    this.setData({
      formData: JSON.stringify(data)
    })
  },
  /**
 * @desc : 保存处理
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  handleData() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 3]; //上二页
    prevPage.setData({
      refreshByAdd: true
    })
    wx.navigateBack({
      data: 2
    })
  },

  /**
 * @desc : 顶部选择
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  onRadioFollowUp(e) {
    if (this.data.formMode == Constants.formMode.edit) {
      return
    }
    this.setFormContent(e.detail)
    let followStatus = this.data.followStatus
    if (e.detail == 1) { //跟进
      followStatus = mixins.$t('followStatusFollowUp')
    }
    if (e.detail == 2) { //邀约进店
      followStatus = mixins.$t('followStatusInvitation')
    }
    if (e.detail == 3) { //预约量尺
      followStatus = mixins.$t('followStatusFoot')
    }
    this.setData({
      followStatus: followStatus,
      radioFollowUp: e.detail,
      formData: this.setCus(JSON.parse(this.data.formData), this.data.item),
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //接收父页面传递的参数
    let that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('params', function (data) {
      let item = that.data.item
      let formData = that.setCus(JSON.parse(that.data.formData), item)
      that.setData({
        formData: formData
      })
    })
  },

  /**
   * @desc : 设置客户信息
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setCus(formData, item) {
    formData.cusId = item.cusId
    formData.cusName = item.cusName
    formData.cusCode = item.cusCode
    formData.cusPhone = item.cusPhone
    //不这么设置 会不回显
    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.saleStatus = item.saleStatus
    return JSON.stringify(formData)
  },

  /**
    * @desc : 设置跟进form 字段
    * @date : 2024/2/1 15:49
    * @author : 于继渤
    */
  setFormContent(radioFollowUp) {
    let radioFollowUpList1 = [
      { 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: false },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo') },
      { code: 'intention', name: 'intentionName', type: 'drop', required: true, dropType: 'customerIntention', title: mixins.$t('intention') },

      { code: 'followData', type: 'textarea', required: true, title: mixins.$t('followData') },
      { code: 'nextFollowTime', type: 'dateTime', required: true, title: mixins.$t('nextFollowTime') },
      { code: 'stayTimeLen', name: 'stayTimeLenName', required: true, title: mixins.$t('stayTimeLen'), dropType: 'storeRetentionTime', type: 'drop', },
    ]

    let radioFollowUpList11 = [
      { code: 'cusName', type: 'str', required: false, title: mixins.$t('customerName'),readonly:true },
      { code: 'cusPhone', type: 'str', required: false, title: mixins.$t('cusPhone') ,readonly:true },
      { code: 'addressFull', type: 'str', required: false, title: mixins.$t('addressFull'),readonly:true  },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo'),readonly:true  },
      { code: 'intentionName', type: 'str', required: false, title: mixins.$t('intention'),readonly:true  },
      { code: 'followData', type: 'textarea', required: false, title: mixins.$t('followData'),readonly:true  },
      { code: 'nextFollowTime', type: 'str', required: false, title: mixins.$t('nextFollowTime'),readonly:true  },
      { code: 'stayTimeLenName', type: 'str', required: false, title: mixins.$t('stayTimeLen'),readonly:true  },
    ]
    let radioFollowUpList2 = [
      { 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: false },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo') },
      { code: 'inviteResult', name: 'inviteResultName', type: 'drop', required: true, dropType: 'inviteResult', title: mixins.$t('inviteResult') },
      { code: 'inviteTime', type: 'dateTime', required: true, title: mixins.$t('inviteResult') },
      { code: 'intention', name: 'intentionName', type: 'drop', required: true, dropType: 'customerIntention', title: mixins.$t('intention') },
      { code: 'followData', type: 'textarea', required: true, title: mixins.$t('followData') },
      { code: 'nextFollowTime', type: 'dateTime', required: true, title: mixins.$t('nextFollowTime') },
    ]
    let radioFollowUpList22 = [
      { code: 'cusName', type: 'str', required: false, title: mixins.$t('customerName'),readonly:true },
      { code: 'cusPhone', type: 'str', required: false, title: mixins.$t('cusPhone') ,readonly:true },
      { code: 'addressFull', type: 'str', required: false, title: mixins.$t('addressFull'),readonly:true  },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo'),readonly:true  },
      { code: 'inviteResult', type: 'str', required: false, title: mixins.$t('inviteResult'),readonly:true  },
      { code: 'inviteTime', type: 'str', required: false, title: mixins.$t('inviteTime'),readonly:true  },
      { code: 'intentionName', type: 'str', required: false, title: mixins.$t('intention'),readonly:true  },
      { code: 'followData', type: 'textarea', required: false, title: mixins.$t('followData'),readonly:true  },
      { code: 'nextFollowTime', type: 'str', required: false, title: mixins.$t('nextFollowTime'),readonly:true  },
    ]
    let radioFollowUpList3 = [
      { 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: false },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo') },
      { code: 'designStaff', name: 'designStaffName', type: 'drop', required: true, dropType: 'staff', title: mixins.$t('designStaff') },
      { code: 'inviteResult', name: 'inviteResultName', type: 'drop', required: true, dropType: 'inviteResult', title: mixins.$t('inviteResult') },
      { code: 'followTime', type: 'dateTime', required: true, title: mixins.$t('nextFollowTimeS') },
      { code: 'followData', type: 'textarea', required: true, title: mixins.$t('followData') },
      { code: 'nextFollowTime', type: 'dateTime', required: true, title: mixins.$t('nextFollowTime') },

    ]
    let radioFollowUpList33 = [
      { code: 'cusName', type: 'str', required: false, title: mixins.$t('customerName'),readonly:true },
      { code: 'cusPhone', type: 'str', required: false, title: mixins.$t('cusPhone') ,readonly:true },
      { code: 'addressFull', type: 'str', required: false, title: mixins.$t('addressFull'),readonly:true  },
      { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo'),readonly:true  },
      { code: 'designStaffName', type: 'str', required: false, title: mixins.$t('designStaff'),readonly:true  },
   
      { code: 'inviteResultName', type: 'str', required: false, title: mixins.$t('inviteResult'),readonly:true  },

      { code: 'followTime', type: 'str', required: false, title: mixins.$t('nextFollowTimeS'),readonly:true  },
      { code: 'followData', type: 'textarea', required: false, title: mixins.$t('followData'),readonly:true  },
      { code: 'nextFollowTime', type: 'str', required: false, title: mixins.$t('nextFollowTime'),readonly:true  },

    ]
    let contentObj = this.data.contentObj
    // let radioFollowUp = this.data.radioFollowUp
    if (radioFollowUp == '1') {
      contentObj.main = this.data.formType == 'detail' ? radioFollowUpList11:radioFollowUpList1 
    }
    if (radioFollowUp == '2') {
      contentObj.main =  this.data.formType == 'detail' ? radioFollowUpList22:radioFollowUpList2
    }
    if (radioFollowUp == '3') {
      contentObj.main =  this.data.formType == 'detail' ? radioFollowUpList33:radioFollowUpList3
    }
    this.setData({
      contentObj: contentObj
    })
  }
})