// pages/meeting/appointmentDetail/index.js
import {
    cancellationMeeting,
    getMeetingInServiceList,
    meetingServiceListSubmit,appointmentDateRecordList
} from '../../../utils/api/meeting'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar, //胶囊按钮位置
        CustomBar: app.globalData.CustomBar,
        type: '',
        item: {},
        meetingData: {},
        funList: [{
                url: '/pages/meeting/renewalContract/index',
                name: '会议续约',
                status:[1,2],
            },
            {
                url: '/pages/meeting/transfer/index',
                name: '会议转让',
                status:[1],
            },
            {
                url: '',
                name: '取消会议',
                status:[1],
            },
        ],
        isShowFun: false,
        showPopup: false,
        orderMealList: [],
        leftList: [],
        rightList: [],
        projection: false,
        aircondition: false,
        nextMeetingStartTime:null,
    },
    // 是否显示提示框区域
    showFun() {
        let isShowFun = this.data.isShowFun
        this.setData({
            isShowFun: !isShowFun
        })
    },
    // 显示服务点单
    showPopupFun() {
        this.setData({
            showPopup: true
        })
    },
    // 关闭服务点单
    onClose() {
        let {
            orderMealList
        } = this.data
        orderMealList.forEach(item => {
            item.isTrue = false
        })
        this.setData({
            showPopup: false,
            orderMealList
        });
    },
    // 点餐
    getOrde(e) {
        let index = e.currentTarget.dataset.index
        this.setData({
            [`orderMealList[${index}].isTrue`]: !this.data.orderMealList[index].isTrue
        })
    },
    //确定点餐
    orderMealFun() {
        let {
            orderMealList,
            item
        } = this.data
        let data = {
            meetingInServiceList: [],
            meetingRecordId: item.meetingRecordId
        }
        orderMealList.forEach(item => {
            if (item.isTrue) {
                data.meetingInServiceList.push(item.name)
            }
        })
        if(data.meetingInServiceList.length<=0){
            wx.showToast({
              title: '请选择服务项',
              icon: 'none',
            })
            return
        }
        meetingServiceListSubmit(data).then(res => {
            wx.showToast({
                title: '点单成功',
                icon: 'none',
                duration: 1000,
                //显示透明蒙层，防止触摸穿透
                mask: true,
            })
            this.onClose()
        })
    },
    // 功能
    clickFun(e) {
        let data = e.currentTarget.dataset.item
        let item = JSON.stringify(this.data.item)
        if (data.name == '取消会议') {
            this.cancel()
        } else {
            wx.navigateTo({
                url: data.url + '?item=' + item,
            })
        }
    },
    // 跳转
    gotoFun(e) {
        let {
            item,
            type
        } = this.data
        let url;
        let id = e.currentTarget.dataset.id
        if (id == 3) {
            // 返回
            wx.navigateBack({
                delta: 1,
            })
        } else {
            switch (id) {
                case '1':
                    // 审核详情
                    url = '/pages/audit/deteil/index?item=' + JSON.stringify(item) + '&type=' + type
                    break;
                case '2':
                    //审核
                    url = '/pages/audit/index/index?item=' + JSON.stringify(item) + '&type=' + type
                    break;
                case '4':
                    // 点单
                    url = '/pages/orderMeal/index/index?item=' + JSON.stringify(item) + '&type=' + type
                    break;
                default:
                    break;
            }
            wx.navigateTo({
                url
            })
        }
    },
    // 取消
    cancel() {
        wx.showModal({
            title: '提示',
            content: '确定要取消吗',
            complete: (res) => {
                if (res.confirm) {
                    cancellationMeeting({
                        meetingRecordId: this.data.item.meetingRecordId
                    }).then(res => {
                        wx.showToast({
                            title: '操作成功',
                            icon: 'none',
                            duration: 2000,
                            //显示透明蒙层，防止触摸穿透
                            mask: true,
                            success: function () {
                                setTimeout(function () {
                                    wx.switchTab({
                                        url: '/pages/home/home',
                                    })
                                }, 1500)
                            }
                        })
                    })
                }
            }
        })
    },
    // 会议中可提供的服务列表
    getMeetingInServiceList() {
        getMeetingInServiceList({
            meetingRecordId: this.data.item.meetingRecordId
        }).then(res => {
            let list = []
            res.data.forEach(item => {
                let data = {}
                data.isTrue = false
                data.name = item
                list.push(data)
            })
            this.setData({
                orderMealList: list
            })
        })
    },
    // 本会议室预约的会议列表
    appointmentDateRecordList() {
        let item = this.data.item
        let data = {
            appointmentDate:item.appointmentDate,
            meetingRoomId: item.meetingRoomId
        }
        appointmentDateRecordList(data).then(res => {
          let index =  res.data.findIndex(data=>{
              return data.appointmentStartTime ==item.appointmentStartTime
          })
          if(index+1>=res.data.length){
            this.setData({
                nextMeetingStartTime:null
            })
          }else if(index>-1){
              this.setData({
                  nextMeetingStartTime:res.data[index+1].appointmentStartTime
              })
          }
            console.log(index);
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.item) {
            let item = JSON.parse(options.item)
            this.setData({
                type: options.type,
                item,
                leftList: item.meetingDeskVoList,
                rightList: item.meetingDeskOtherSideVoList,
                aircondition: item.aircondition,
                projection: item.projection,
                meetingData: item.meetingRoomVo || item
            })
        }
        console.log(this.data.type);
        console.log(this.data.item);
        if (this.data.type == 'my-meeting'){
            this.appointmentDateRecordList()
        }
        if (this.data.type == 'my-meeting' && this.data.item.status == 2) {
            this.getMeetingInServiceList()
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})