/*******************************************************************************
 * Copyright(c) 2022 dongke All rights reserved. / Confidential
 * 类的信息：
 *		1.程序名称：
 *		2.客户资料列表
 * 编辑履历：
 *		作者				日期					版本				修改内容
 *		yjb		        2022-0-12	            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',
    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: 'remarks', type: 'textarea', rows: '5', required: true, showWordLimit: 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', },
        { code: 'file', type: 'uploader', title: mixins.$t("uploader") }
      ]
    },
    table: Constants.tables.cusFollow,
    customerService: app.globalData['customerService'],
    followStatus: mixins.$t('followStatusReporting')
  },


  /**
   * @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() {
    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 : 于继渤
   */
  toEdit(){
    let item = JSON.parse(this.data.formData)
    let url = null
    if (item.followStatus == '跟进状态-报备' || item.followStatus == '跟进状态-接待') {
      url = this.data.route.toFollowStatus1.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 : 于继渤
 */
  handleData() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 3]; //上二页
    prevPage.setData({
      refreshByAdd: true
    })
    wx.navigateBack({
      data: 2
    })
  },
  /**
   * @desc : 手机号离开焦点事件
   * @date : 2024/4/8 15:49
   * @author : 于继渤
   */
  changePhoneblur(e) {
    let phone = e.detail.detail.detail.value
    if (phone) {
      //查询客户信息
      this.getCustomerData({ cusPhone: phone })
    }
  },

  /**
   * @desc : 查询客户信息
   * @author : 于继渤
   * @date : 2024/1/23 9:16
   */
  getCustomerData(params) {
    return this.excute(this.data.customerService, this.data.customerService.selectByCondNoPage, params).then(res => {
      if (res.data.code == 200) {
        if (res.data.data.length > 0) {
          let formData = JSON.parse(this.data.formData)
          let item = res.data.data[0]
          formData.cusCode = item.cusCode
          formData.cusId = item.cusId
          formData.cusName = item.cusName
          formData.cusFromName = item.cusFromName
          //处理地址
          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
          this.setData({
            formData: JSON.stringify(formData)
          })
        }
      }
    });
  },
  /**
    * @desc : 顶部切换
    * @date : 2024/4/8 15:49
    * @author : 于继渤
    */
  onRadioFollowUp(e) {
    if (this.data.formMode == Constants.formMode.edit) {
      return
    }
    let detail = e.detail
    this.setFormContent(detail)
    this.setData({
      radioFollowUp: detail,
      formData: null
    })
    console.log('onRadioFollowUp', e.detail)
  },
  /**
  * @desc : 跳转购物车
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toShopping() {
    //跳转购物车开单
    let that = this
    wx.navigateTo({
      url: this.data.route.toSelectGoods.url,
      success: function (res) {
        res.eventChannel.emit('params', { choose: true, item: 'sale', formItem: that.data.formData, formType: 'customer-reception', route: '/package-sales/pages/order-billing/add/add' })
      }
    })
  },
  /**
   * @desc : 给表单赋值
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setValuesByEdit(data) {
    console.log(data)

    if (data.followStatus == '跟进状态-接待') {
      this.setData({
        radioFollowUp: '2'
      })
      this.setFormContent('2')
    }
    //处理地址
    data.address = {
      address: data.addressFull,
      addressFull: data.addressFull,
      addressArea: data.addressArea,
      addressGcj02: data.addressGcj02,
      addressName: data.addressName,
    }
    this.setData({
      formData: JSON.stringify(data)
    })
  },
  /**
  * @desc : 设置跟进form 字段
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  setFormContent(radioFollowUp) {
    let radioFollowUpOnelist = [
      { 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: 'remarks', type: 'textarea', rows: '5', required: true, showWordLimit: 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', },
      { code: 'file', type: 'uploader', title: mixins.$t("uploader") }
    ]
    let radioFollowUpTwolist = [
      { code: 'intention', name: 'intentionName', type: 'drop', required: true, dropType: 'customerIntention', title: mixins.$t('intention') },
      { code: 'remarks', type: 'textarea', rows: '5', required: true, showWordLimit: true, title: mixins.$t('followData') },
      { code: 'stayTimeLen', name: 'stayTimeLenName', required: true, title: mixins.$t('stayTimeLen'), dropType: 'storeRetentionTime', type: 'drop', },
      { code: 'file', type: 'uploader', title: mixins.$t("uploader") }
    ]

    let contentObj = this.data.contentObj
    let followStatus = this.data.followStatus
    if (radioFollowUp == '1') {
      followStatus = mixins.$t('followStatusReporting')
      contentObj.main = radioFollowUpOnelist
    }
    if (radioFollowUp == '2') {
      followStatus = mixins.$t('followStatusReception')
      contentObj.main = radioFollowUpTwolist
    }

    this.setData({
      followStatus, followStatus,
      contentObj: contentObj
    })
  }
})