const app = getApp()

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

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

import {
    selectReservationByIdRq,
    cancelOrderRq
} from "../../../../../api/meeting/meetingRoom.js"

Page({

    /**
     * 页面的初始数据
     */
    data: {
        IMG_NAME: app.IMG_NAME,
        id: null,
        detail: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let _this = this;
        _this.setData({
            ...options
        })
        // 获取详情
        _this.getDetail();
    },

    // 获取详情
    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);
            // 合计时间
            detail.countTime = twoTimeIntervalReturnHours(detail.startTime, detail.endDate)
            // 状态字体颜色
            let statusColor = "#FFB119";
            // 按钮是否显示
            let statusValue = detail.statusValue;
            let showPay = false;
            let showInvite = false;
            let showCancel = false;
            let showDetail = false;
            // 待支付 0 ：去支付、取消订单、查看详情
            if (statusValue == 0) {
                showPay = true;
                showCancel = true;
                showDetail = true;
                // 状态字体颜色
                statusColor = "#FFB119";
            }
            // 待使用 1：去邀请、取消订单、查看详情
            if (statusValue == 1) {
                showInvite = true;
                showCancel = true;
                showDetail = true;
                // 状态字体颜色
                statusColor = "#3794FF";
            }
            // 进行中 2：去邀请、查看详情
            if (statusValue == 2) {
                showInvite = true;
                showDetail = true;
                // 状态字体颜色
                statusColor = "#FF4040";
            }
            // 已结束 3：查看详情
            if (statusValue == 3) {
                showDetail = true;
                // 状态字体颜色
                statusColor = "#333333";
            }
            // 已取消 4：查看详情
            if (statusValue == 4) {
                showDetail = true;
                // 状态字体颜色
                statusColor = "#7F7F7F";
            }
            //
            // 赋值
            detail.showPay = showPay;
            detail.showInvite = showInvite;
            detail.showCancel = showCancel;
            detail.showDetail = showDetail;
            // 状态字体颜色
            detail.statusColor = statusColor;
            _this.setData({
                detail
            })
        })
    },

    // 取消订单
    cancelOrder(e) {
        console.log('cancelOrder', e);
        let _this = this;
        let id = e.currentTarget.dataset.id
        const beforeClose = (action) => {
            console.log('action', action);
            return new Promise((resolve) => {
                if (action === 'confirm') {
                    cancelOrderRq({
                        id,
                        cancelResaon: ""
                    }).then(res => {
                        console.log('cancelOrderRq', res);
                        if (res.code == 0) {
                            // 刷新数据
                            _this.getDetail();
                            // 数据状态变更，向上个页面传递变更
                            let pages = getCurrentPages(); //获取page
                            let prevPage = pages[pages.length - 2]; //上一个页面（父页面）
                            prevPage.setData({
                                dataChange: true
                            })
                        } else {
                            // 危险通知
                            Notify({
                                type: 'danger',
                                message: res.msg
                            });
                        }
                        resolve(true);
                    })
                } else {
                    // 拦截取消操作
                    resolve(true);
                }
            });
        }
        Dialog.confirm({
            title: '提示',
            message: '是否要取消订单!',
            beforeClose,
        });
    },

    // 跳转-支付
    jumpPay(e) {
        console.log('jumpPay', e);
        wx.navigateTo({
            url: "/pages/meeting/pay/waitPay/waitPay?id=" + e.currentTarget.dataset.id + "&type=meetingRoom"
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        console.log('onShareAppMessage', e);
        let _this = this;
        let id = e.target.dataset.id;
        let detail = _this.data.detail
        //
        let param = {
            title: detail.title,
            path: "/pages/meeting/invite/invite?id=" + id,
            imageUrl: app.IMG_NAME + detail.roomContent.indoorPicUrl,
        }
        console.log('onShareAppMessage', param);
        return param;
    }
})