let app = getApp()

import Notify from '@vant/weapp/notify/notify';

import {
    selfFormatTimeYMD,
    selfFormatTimeHM,
    twoTimeIntervalReturnHours
} from "../../../utils/util.js"

import {
    selectReservationByIdRq,
    selectCoordinateRq,
    roomContentIsVisitorRq,
    roomContentAddVisitorRq
} from "../../../api/meeting/meetingRoom.js"

import {
    openDoorRq
} from "../../../api/meeting/equipment.js"

import {
    loginRq,
    registerPhone
} from "../../../api/login/login.js"


Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        mapData: {
            // latitude: 33.601291,
            // longitude: 119.031829,
            // markers: [{
            //     id: 1,
            //     latitude: 33.601291,
            //     longitude: 119.031829,
            //     title: '淮安茂业天地购物中心'
            // }]
        },
        id: null,
        detail: null,
        address: {},
        openid: null,
        authorizationShow: true,
        participateShow: false,
        openDoorShow: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        _this.setData({
            ...options
        })
        // 获取详情
        _this.getDetail();
        // 获取地址信息
        _this.getAddress()
        // 自动登录获取openid
        _this.autoLogin()
    },

    // 获取地址信息
    getAddress() {
        let _this = this;
        selectCoordinateRq().then(res => {
            _this.setData({
                address: res,
                mapData: {
                    latitude: res.lat,
                    longitude: res.lng,
                    markers: [{
                        id: 1,
                        latitude: res.lat,
                        longitude: res.lng,
                        title: res.address,
                    }]
                },
            })
        })
    },

    // 获取详情
    getDetail() {
        let _this = this;
        let id = _this.data.id;
        selectReservationByIdRq(id).then(res => {
            let detail = res.data;
            // 时间段
            detail.timeSlot = selfFormatTimeYMD(detail.startTime) + ' ' + selfFormatTimeHM(detail.startTime) + '~' + selfFormatTimeHM(detail.endDate);
            _this.setData({
                detail
            })
        })
    },

    // 打开地图
    openMap(e) {
        console.log('openMap', e);
        let _this = this;
        wx.openLocation({
            name: _this.data.address.address,
            latitude: _this.data.address.lat,
            longitude: _this.data.address.lng,
        })
    },

    // 自动登录获取openid
    autoLogin() {
        let _this = this;
        wx.login({
            success(wxRes) {
                loginRq({
                    "jsCode": wxRes.code
                }).then(res => {
                    console.log('loginRq', res);
                    _this.setData({
                        openid: res.openid
                    })
                    // 判断用户是否注册
                    if (res.token) {
                        // 用户授权成功
                        _this.userAuthorizationSuccess(res.openid, res.user, res.token.token)
                    }
                })
            }
        })
    },

    // 获取手机号
    getPhoneNumber(e) {
        let _this = this;
        console.log('getPhoneNumber', e)
        registerPhone({
            "code": e.detail.code,
            "openid": _this.data.openid
        }).then(res => {
            console.log('registerPhone', res);
            // 用户授权成功
            _this.userAuthorizationSuccess(res.openid, res.user, res.token.token)
        })
    },
    // 用户授权成功
    userAuthorizationSuccess(openid, user, token) {
        let _this = this;
        _this.setData({
            authorizationShow: false
        })

        // 清空所有缓存
        wx.clearStorageSync()
        // 存储用户信息
        wx.setStorageSync('openid', openid)
        wx.setStorageSync('user', user)
        wx.setStorageSync('userId', user.id)
        wx.setStorageSync('token', token)

        // 判断是否参与过会议
        _this.whetherParticipate();
    },

    // 判断是否参与过会议
    whetherParticipate() {
        let _this = this
        roomContentIsVisitorRq({
            "userId": _this.data.detail.userId,
            "participantId": wx.getStorageSync('userId'),
            "reservationId": _this.data.detail.id,
        }).then(res => {
            console.log('whetherParticipate', res);
            if (res.code == 0) { // 未预约
                _this.setData({
                    participateShow: true
                })
            } else { // 已预约
                _this.setData({
                    openDoorShow: true
                })
            }
        })
    },

    // 参与会议
    participateMeeting() {
        let _this = this;
        roomContentAddVisitorRq({
            "userId": _this.data.detail.userId,
            "participantId": wx.getStorageSync('userId'),
            "reservationId": _this.data.detail.id,
            "participantPhone": wx.getStorageSync('user').mobile
        }).then(res => {
            console.log('participateMeeting', res);
            if (res.code == 0) { //参与成功
                _this.setData({
                    participateShow: false,
                    openDoorShow: true
                })
            } else { //参与失败
                Notify({
                    type: 'danger',
                    message: res.msg
                });
            }
        })
    },

    // 开门
    openDoor() {
        let _this = this;
        wx.reLaunch({
            url: "/pages/meeting/accessControl/accessControl",
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})