// pages/home/home.js
import {
    meetingGetPageList,
    myMeeting
} from '../../utils/api/meeting'
import {
    doGetInfo
} from '../../utils/api/api'
import {
    GetQueryJson
} from '../../utils/util'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar, //胶囊按钮位置
        CustomBar: app.globalData.CustomBar,
        userInfo: wx.getStorageSync('userInfo'),
        myMeetingList: [],
        meetingList: [],
        funList: [{
                id: '1',
                name: '会议室预约',
                img: '/static/home/home-1.png',
                url: '/pages/meeting/index/index?type=meeting-detail',
                jurisdiction: [1, 2],
            },
            {
                id: '2',
                name: '我的会议',
                img: '/static/home/home-2.png',
                url: '/pages/myMeeting/index/index?type=my-meeting',
                jurisdiction: [1],
            },
            {
                id: '3',
                name: '我的预约',
                img: '/static/home/home-3.png',
                url: '/pages/mySubscribe/index/index?type=my-subscribe',
                jurisdiction: [1],
            },
            {
                id: '10',
                name: '我的续约',
                img: '/static/home/home-4.png',
                url: '/pages/myRenewalContract/index/index?type=my-renewalContract',
                jurisdiction: [1],
            },
            {
                id: '5',
                name: '我的转让',
                img: '/static/home/home-5.png',
                url: '/pages/myTransfer/index/index?type=my-transfer',
                jurisdiction: [1],
            },
            {
                id: '6',
                name: '转让审核',
                img: '/static/home/home-6.png',
                url: '/pages/myTransfer/index/index?type=transfer-audit',
                jurisdiction: [1],
            },
            {
                id: '4',
                name: '预约审核',
                img: '/static/home/home-7.png',
                url: '/pages/mySubscribe/index/index?type=subscribe-audit',
                jurisdiction: [2],
            },
            {
                id: '7',
                name: '续约审核',
                img: '/static/home/home-8.png',
                url: '/pages/myRenewalContract/index/index?type=renewalContract-audit',
                jurisdiction: [2],
            },
            {
                id: '9',
                name: '会议服务',
                img: '/static/home/home-9.png',
                url: '/pages/orderMeal/index/index?type=order-audit',
                jurisdiction: [3],
            },
        ],
        // accountId: null,
        // accountName: null,
    },
    // getVal(e) {
    //     console.log(e);
    //     this.setData({
    //         accountName: this.data.accountList[e.detail.value].name,
    //         accountId: this.data.accountList[e.detail.value].id
    //     })
    // },
    gotomy() {
        wx.switchTab({
            url: '/pages/mine/index/index',
        })
    },
    // 我的会议室-数据
    myMeeting() {
        let data = {
            data: {
                appointmentStatus: null
            },
            pageNum: 1,
            pageSize: 3,
        }
        myMeeting(data).then(res => {
            this.setData({
                myMeetingList: res.data.records
            })
        })
    },
    // 会议室-数据
    meetingGetList() {
        let data = {
            pageNum: 1,
            pageSize: 8,
        }
        meetingGetPageList(data).then(res => {
            this.setData({
                meetingList: res.data.records
            })
        })
    },
    // 我的会议-详情
    myMeetingFun(e) {
        let item = e.currentTarget.dataset.item
        item.status = item.appointmentStatus
        item = JSON.stringify(e.currentTarget.dataset.item)
        wx.navigateTo({
            url: '/pages/meeting/appointmentDetail/index?item=' + item + '&type=' + e.currentTarget.dataset.type
        })
    },
    // 会议室-详情
    goRoomFun(e) {
        let type = 'meeting-detail'
        if (e.currentTarget.dataset.item) {
            let item = JSON.stringify(e.currentTarget.dataset.item)
            wx.navigateTo({
                url: '/pages/meeting/detail/index?item=' + item + '&type=' + type
            })
        } else {
            wx.navigateTo({
                url: '/pages/meeting/index/index?' + '&type=' + type
            })
        }
    },
    // 功能区
    goFun(e) {
        let item = e.currentTarget.dataset.item
        if (item.id == 6 || item.id == 4 || item.id == 7 || item.id == 9) {
            wx.requestSubscribeMessage({
                tmplIds: ['w7cy1am3DS8BF31uUDoMKfruKWQXT0ckhALzyUdvweg', 'mJs6TXjIFuwgPu2JOEIpu3lO9ym9rnai-schUPuLuok'],
                success(res) {
                    wx.navigateTo({
                        url: item.url
                    })
                }
            })
        } else {
            wx.navigateTo({
                url: item.url
            })
        }
    },

    doGetInfo() {
        let userInfo = wx.getStorageSync('userInfo')
        if (userInfo) {
            doGetInfo().then(res => {
                if (res.code == 200) {
                    userInfo = Object.assign(userInfo, res.data);
                    this.setData({
                        userInfo
                    })
                    wx.setStorageSync('userInfo', userInfo)
                    this.meetingGetList()
                    // 预约权限
                    if (userInfo.appointmentAuth) {
                        this.myMeeting()
                    }
                } else {
                    this.removeData()
                }
            }).catch((err) => {
                this.removeData()
            })
        } else {
            this.removeData()
        }
    },
    removeData() {
        wx.removeStorageSync('userInfo')
        this.setData({
            userInfo: {},
            myMeetingList: [],
            meetingList: [],
        })
    },
    // 审核人消息订阅
    subscriptionFun() {
        wx.showModal({
            title: '感谢您使用！',
            content: '',
            success(res) {
                wx.requestSubscribeMessage({
                    tmplIds: ['w7cy1am3DS8BF31uUDoMKfruKWQXT0ckhALzyUdvweg'],
                    success(res) {}
                })
            }
        })

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let json = {};
        if (options.q) {
            var url = decodeURIComponent(options.q);
            json = GetQueryJson(url);
            if ('adminId' in json) {
                console.log(111, json.adminId);
                let adminId = wx.getStorageSync('adminId')
                if (adminId != json.adminId) {
                    this.removeData()
                }
                wx.setStorageSync('adminId', json.adminId)
            }
        }
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.doGetInfo()
    },

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

    },

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

    },

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

    },

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

    },

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

    }
})