/*******************************************************************************
 * 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()
const config = require('@/config/config.js')
Page({
  mixins: [mixins],
  /**
   * 页面的初始数据
   */
  data: {
    routeObjName: 'cusFollow',
    cardList: ['main'],
    maxSize: Constants.uploadFileConfig.maxSize,
    buttonSaveList: [{ code: 'add', title: mixins.$t('save'), width: '120rpx' }],
    contentObj: {
      main: [
        { code: 'cusName', type: 'str', required: true, title: mixins.$t('customerName'), readonly: true },
        { code: 'cusPhone', type: 'phone', title: mixins.$t('cusPhone'), required: true, readonly: true },
        { code: 'addressFull', type: 'address', title: mixins.$t('addressFull'), required: false, readonly: true },
        { code: 'addressNo', type: 'str', required: false, title: mixins.$t('addressNo'), readonly: true },
        { code: 'staffName', type: 'str', required: false, title: mixins.$t('sales'), readonly: true },
        { code: 'flgAgainMeasure', type: 'checkedBox', required: false, title: mixins.$t('flgAgainMeasure'), readonly: false },
        { code: 'measureRemarks', type: 'textarea', required: false, rows: '5', title: mixins.$t('measureRemarks') },
        { code: 'measureTime', type: 'dateTime', required: true, title: mixins.$t('measureTime') },
      ]
    },
    measureRoomList: [
      {
        roomName: '',
        measureAnnex: [],
      }
    ],
  },
  /**
 * @desc : 空间添加
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  addItem() {
    let measureRoomList = this.data.measureRoomList;
    measureRoomList.push({
      roomName: '',
      measureAnnex: [],
    })
    this.setData({
      measureRoomList: measureRoomList
    })
  },
  /**
   * @desc : 上传附件
   * @author : 周兴
   * @date : 2024/3/27 16:16
   */
  afterRead(e) {
    let index = e.currentTarget.dataset.index
    let measureRoomList = this.data.measureRoomList
    const _this = this;
    const {
      file
    } = e.detail;
    // 配置存放的文件夹 公司code - 表名 - 年月 -- 日
    let folder = app.globalData.company.cpCode + '/' + 't_crm_measure_room' + '/' + new Date().toYearMonth2() + '/' + new Date().toDay()
    let url = config.upload_url;
    url = url.replace('mdm-server', 'mdm-server' + '-' + app.globalData.company.svcCode.replace('_', '-'))
    file.forEach(res => {
      // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
      wx.uploadFile({
        url: url,
        filePath: res.path,
        name: 'file',
        formData: {
          'folder': folder
        },
        header: {
          'Authorization': 'Bearer ' + app.globalData.token
        },
        success(res) {
          let data = JSON.parse(res.data)
          if (data.code === 200) {
            _this.setData({
              uploadFileFlag: false
            })
            let annexPaths = measureRoomList[index]['measureAnnex'] || []
            data.data.url = config.image_url + data.data.path
            annexPaths.push(data.data)
            measureRoomList[index]['measureAnnex'] = annexPaths;
            _this.setData({
              measureRoomList: measureRoomList
            })
          }
        },
      })
    })
  },
  /**
 * @desc : 空间删除
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  deleteItem(e) {
    console.log(e)
    let measureRoomList = this.data.measureRoomList
    let index = e.currentTarget.dataset.index
    measureRoomList.splice(index, 1)
    this.setData({
      measureRoomList: measureRoomList
    })
  },

  /**
 * @desc : 空间名称输入事件
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  onRoomNameChange(e) {
    let measureRoomList = this.data.measureRoomList
    let index = e.currentTarget.dataset.index
    measureRoomList[index].roomName = e.detail
    this.setData({
      measureRoomList: measureRoomList
    })
  },

  /**
 * @desc : 设置保存参数
 * @date : 2024/2/1 15:49
 * @author : 于继渤
 */
  setParams(params) {
    let measureRoomList = this.data.measureRoomList
    for (var i = 0; i < measureRoomList.length; i++) {
      if (!measureRoomList[i]['roomName']) {
        wx.showToast({
          title: mixins.$t('pleaseEnterTheSpaceName'),
          icon: 'none'
        })
        return
      }
      measureRoomList[i]['displayNo'] = i + 1
    }
    params.measureRoomList = this.data.measureRoomList
    params.flgAgainMeasure = this.data.params.flgAgainMeasure ? this.data.params.flgAgainMeasure : false
    console.log('params', params)
    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.updateMeasure, this.data.params);
    } else {
      return this.excute(this.data.service, this.data.service.saveMeasure, this.data.params);
    }


  },



  /**
   * @desc : 设置客户信息
   * @date : 2024/2/1 15:49
   * @author : 于继渤
   */
  setCus(formData, item) {
    formData['cusId'] = item.cusId ? item.cusId : null
    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.staffName = item.staffName
    formData.followId = item.followId
    return JSON.stringify(formData)
  },
  /**
     * @desc : 给表单赋值
     * @date : 2024/2/1 15:49
     * @author : 于继渤
     */
  setValuesByEdit(data) {

    this.setCoums()
    let formData = JSON.parse(this.data.formData)
    let measureReceipt = data.measureReceipt
    //处理量尺下信息
    formData.followId = data.followId
    formData.measureRemarks = measureReceipt.measureRemarks
    formData.receiptId = measureReceipt.receiptId

    formData.measureTime = measureReceipt.measureTime
    formData.flgAgainMeasure = measureReceipt.flgAgainMeasure
    formData.cusName = data.cusName
    formData.cusCode = data.cusCode
    formData.cusId = data.cusId
    formData.cusPhone = data.cusPhone
    //不这么设置 会不回显
    formData.address = {
      address: data.addressFull,
      addressFull: data.addressFull,
      addressArea: data.addressArea,
      addressGcj02: data.addressGcj02,
      addressName: data.addressName,
    }
    formData.addressArea = data.addressArea
    formData.addressFull = data.addressFull
    formData.addressGcj02 = data.addressGcj02
    formData.addressName = data.addressName
    formData.addressNo = data.addressNo
    formData.staffName = data.staffName
    formData.staffId = data.staffId
    if (this.data.formType == Constants.formMode.detail) {
      data.measureRoomList.forEach(res => {
        res.readonly = true
      })
    }


    this.setData({
      formData: JSON.stringify(formData),
      measureRoomList: data.measureRoomList,
    })
  },


  setCoums() {
    let contentObj = this.data.contentObj
    if (this.data.formType == Constants.formMode.detail) {
      contentObj.main = [
        { 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: 'staffName', type: 'str', required: false, title: mixins.$t('sales'), readonly: true },
        // { code: 'flgAgainMeasure', type: 'str', required: false, title: mixins.$t('flgAgainMeasure'),readonly:true  },
        { code: 'flgAgainMeasure', type: 'checkedBox', required: false, title: mixins.$t('flgAgainMeasure'), readonly: false },
        { code: 'measureRemarks', type: 'str', required: false, title: mixins.$t('measureRemarks'), readonly: true },
        { code: 'measureTime', type: 'str', required: false, title: mixins.$t('measureTime'), readonly: true },

      ]
    }else{
      contentObj.main = [
        { 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: 'staffName', type: 'str', required: false, title: mixins.$t('sales'), readonly: true },
        { code: 'flgAgainMeasure', type: 'checkedBox', required: false, title: mixins.$t('flgAgainMeasure'), readonly: false },
        { code: 'measureRemarks', type: 'textarea', required: false, rows: '5', title: mixins.$t('measureRemarks') },
        { code: 'measureTime', type: 'dateTime', required: true, title: mixins.$t('measureTime') },

      ]
    }

    this.setData({
      contentObj: contentObj
    })
  },
  /**
  * @desc : 编辑
  * @date : 2024/2/1 15:49
  * @author : 于继渤
  */
  toEdit() {
    let item = JSON.parse(this.data.formData)
    let url = this.data.route.toFollowStatus3.url
    console.log(item)
    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);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //接收父页面传递的参数
    let that = this
    // Constants.formMode.edit
    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
      })

    })
  },


})