// pages/meeting/detail/index.js
import {
    appointmentDateRecordList
} from '../../../utils/api/meeting'
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar, //胶囊按钮位置
        CustomBar: app.globalData.CustomBar,
        itemData: {},
        type: '',
        chooseId: 0,
        subscribeList: [],
        appointmentList: [],
    },
    // 获取日期
    getDate() {
        let num = this.data.itemData.meetingLargestDayNum
        let subscribeList = []
        for (let i = 0; i < num; i++) {
            let time = this.getDay(i)
            let data = {
                id: i,
                year: time,
                month: time.slice(5)
            }
            subscribeList.push(data)
        }
        this.setData({
            subscribeList
        })
    },
    getDay(day) {
        var today = new Date();
        var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
        today.setTime(targetday_milliseconds); //注意，这行是关键代码
        var tYear = today.getFullYear();
        var tMonth = today.getMonth();
        var tDate = today.getDate();
        tMonth = this.doHandleMonth(tMonth + 1);
        tDate = this.doHandleMonth(tDate);
        return tYear + "-" + tMonth + "-" + tDate;
    },
    doHandleMonth(month) {
        var m = month;
        if (month.toString().length == 1) {
            m = "0" + month;
        }
        return m;
    },
    goBack() {
        wx.navigateBack({
            delta: 1,
        })
    },
    goto(e) {
        let url
        let itemData = JSON.stringify(this.data.itemData)
        let {
            type,
            subscribeList,
            appointmentList,
            chooseId
        } = this.data
        const length = subscribeList.length;
        let subscribeYear = subscribeList[chooseId].year
        appointmentList = JSON.stringify(appointmentList)
        switch (e.currentTarget.dataset.id) {
            case '1':
                url = '/pages/meeting/subscribe/index?itemData=' + itemData + '&type=' + type + '&endDate=' + subscribeList[length - 1].year + '&appointmentList=' + appointmentList + '&subscribeYear=' + subscribeYear
                break;
            default:
                break;
        }
        wx.navigateTo({
            url
        })
    },
    goDetail(e) {
        let item = e.currentTarget.dataset.item
        let {
            itemData
        } = this.data
        item = Object.assign(item, itemData)
        item = JSON.stringify(e.currentTarget.dataset.item)
        wx.navigateTo({
            url: '/pages/meeting/appointmentDetail/index?item=' + item + '&type=' + this.data.type
        })
    },
    clickDate(e) {
        let item = e.currentTarget.dataset.item
        this.setData({
            chooseId: item.id
        })
        this.appointmentDateRecordList(item.year)
    },
    // 本会议室预约的会议列表
    appointmentDateRecordList(year) {
        let data = {
            appointmentDate: year,
            meetingRoomId: this.data.itemData.meetingRoomId
        }
        appointmentDateRecordList(data).then(res => {
            this.setData({
                appointmentList: res.data
            })
        })
    },
    preview(event) {
        let currentUrl = event.currentTarget.dataset.src
        wx.previewImage({
            current: currentUrl, // 当前显示图片的http链接
            urls: this.data.itemData.imgList // 需要预览的图片http链接列表
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            userInfo: wx.getStorageSync('userInfo'),
        })
        if (options.type) {
            this.setData({
                type: options.type,
                itemData: JSON.parse(options.item)
            })
        }
        this.getDate()
        this.appointmentDateRecordList(this.data.subscribeList[0].year)
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})