place.js 6.23 KB
import create from 'dd-store'
import { queryAvailableMeetingRoomByTime, modifySchedule } from './../../api/request'
import { throttle } from './../../utils/utils.js'
import { observer } from '/utils/observer.js'
create.Page({
  data: {
    $data: null,
    place: '',
    availableMeetingRoom: [],
    scheduleItem: '',
    repeatable: 0,
    // 传递给组件selectPopup 的数据
    conSelectPopupData: {
      selectPopupId: '',
      showSelectPopup: false,
      selectType: 'common',
      editSaveList: [{ text: '仅保存此会议', id: 'only' }, { text: '保存此次及以后会议', id: 'future' }]
    },
    // 传递给组件centerPopup 的数据
    centerPopup: {
      showCenterPopup: false,
      centerPopupList: [
        { id: 0, text: '不发送通知' },
        { id: 1, text: '发送通知' },
        { id: -1, text: '取消编辑' }
      ],
      centerPopupId: ''
    },
    // 传递给组件toast的所有数据
    conToastData: {
      showToast: false,
      title: '保存成功'
    },
  },
  onLoad(e) {
    if (e.scheduleItem) {
      const _that = this
      observer.register({
        notice(scheduleItem) {
          _that.setData({
            'scheduleItem.scheduleTemplateId': scheduleItem
          })
          console.log(scheduleItem);
        }
      })
      this.setData({
        repeatable: e.repeatable,
        scheduleItem: JSON.parse(e.scheduleItem)
      })
    }
    dd.setNavigationBar({
      title: '会议地点'
    })
    let that = this
    let data = {
      startTime: this.$store.data.startTime.replace(/\//g, "-"),
      endTime: this.$store.data.endTime.replace(/\//g, "-")
    }
    queryAvailableMeetingRoomByTime(data).then(res => {
      that.setData({
        availableMeetingRoom: res.data.data
      })
    })
  },
  selectPlace(event) {
    this.$store.data.locationName = event.target.dataset.locationName
    this.$store.data.roomId = ''
    this.update()
    if (!!this.data.scheduleItem) {
      if (this.data.repeatable != 0) {
        this.setData({
          'conSelectPopupData.showSelectPopup': true
        })
      } else {
        let data = {
          location: {
            longitude: -999,
            latitude: -999,
            locationName: this.$store.data.locationName
          },
          meetingRoomId: this.$store.data.roomId,
          modifyModel: 'only',
          modifyContent: 'modify_location'
        }
        this.modifySchedule(data, 0).then(res => {
          this.editLocationS()
        }).catch(err => {
          this.editLocationE()
        })
      }
    } else {
      dd.navigateBack({
        delta: 1
      })
    }
  },

  onSelectPopup(event) {
    this.setData({
      'conSelectPopupData.showSelectPopup': false,
      'conSelectPopupData.selectPopupId': event.currentTarget.dataset.item.id,
      'centerPopup.showCenterPopup': true
    })
  },
  // 选择发不发通知 
  onSelectSend(event) {
    this.setData({
      'centerPopup.showCenterPopup': false
    })
    if (event.currentTarget.dataset.item.id > -1) {
      let data = {
        location: {
          longitude: -999,
          latitude: -999,
          locationName: this.$store.data.locationName
        },
        meetingRoomId: this.$store.data.roomId,
        modifyModel: this.data.conSelectPopupData.selectPopupId,
        modifyContent: 'modify_location'
      }
      this.modifySchedule(data, event.currentTarget.dataset.item.id).then(res => {
        this.editLocationS()
      }).catch(err => {
        this.editLocationE(true)
      })
    } else {
      this.editLocationE(false)
    }
  },
  // 修改地点成功
  editLocationS() {
    this.$store.data.originalData.location.locationName = this.$store.data.locationName
    this.$store.data.originalData.meetingRoomId = this.$store.data.roomId
    this.update()
    dd.navigateBack({
      delta: 1
    })

  },
  // 修改地点失败
  editLocationE(lock = true) {
    this.$store.data.locationName = this.$store.data.originalData.location.locationName
    this.$store.data.roomId = this.$store.data.originalData.meetingRoomId
    this.update()
    if (lock) {
      this.setData({
        'conToastData.showToast': true,
        'conToastData.title': '网络异常'
      })
    }

  },
  // 保存编辑
  modifySchedule(data, needNotice = 0) {
    let obj = {
      needNotice: needNotice,
      scheduleId: this.data.scheduleItem.id,
      templateId: this.data.scheduleItem.scheduleTemplateId,
      planDate: this.data.scheduleItem.planDate
    }
    return new Promise((resolve, rejects) => {
      modifySchedule(Object.assign(data, obj)).then(res => {
        if (res.data.code === 0) {
          this.setData({
            'conToastData.showToast': true,
            'conToastData.title': '保存成功'
          })
          if (this.$store.data.originalData.repeatable === 1) {
            observer.notice(res.data.data)
          }
          return resolve(res)
        } else {
          return rejects(res)
        }
      }).catch(err => {
        return rejects(err)
      })
    })
  },
  // onSelectPopupCancel 点击取消 
  onSelectPopupCancel() {
    this.setData({
      'conSelectPopupData.showSelectPopup': false,
      'centerPopup.showCenterPopup': false
    })
    this.editLocationE(false)
  },
  selectMeetingRoom(e) {
    this.$store.data.locationName = e.target.dataset.locationName
    this.$store.data.roomId = e.target.dataset.locationid
    this.update()
    if (!!this.data.scheduleItem) {
      if (this.data.repeatable != 0) {
        this.setData({
          'conSelectPopupData.showSelectPopup': true
        })
      } else {
        let data = {
          location: {
            longitude: -999,
            latitude: -999,
            locationName: this.$store.data.locationName
          },
          meetingRoomId: this.$store.data.roomId,
          modifyModel: 'only',
          modifyContent: 'modify_location'
        }
        this.modifySchedule(data, 0).then(res => {
          this.editLocationS()
        }).catch(err => {
          this.editLocationE()
        })
      }
    } else {
      dd.navigateBack({
        delta: 1
      })
    }
  },
  inputPlace(event) {
    this.setData({
      place: event.detail.value
    })
  },
  toRoomList: throttle(function () {
    dd.navigateTo({ url: `./../meetingRoomList/meetingRoomList` })
  }, 1000)
});