// pages/index-guest/index-guest.js
const app = getApp();
const util = require('../../utils/util.js');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        imgPath: app.globalData.imgPath,
        path: app.globalData.path,
        app_bottom: '大城细管·大城众管·大城智管',
        nowTime: util.formatDate(null, 'yyyy.MM.dd'),
        nextMeeting: null,
        hasNext: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.info('userinfo', app.globalData.userInfo);
        // console.info('roleinfo', app.globalData.roleInfo);
        this.setData({
            username: app.globalData.userInfo.usernameShow,
            role_name: app.globalData.roleInfo.name,
            roleCode: app.globalData.roleInfo.code,
        });
        let that = this;
        if (!app.globalData.userInfo.usernameShow || !app.globalData.userInfo.nickName || !app.globalData.userInfo.headImgUrl) {
            wx.showModal({
                title: '提示',
                content: '轻应用需要取您的用户信息',
                showCancel: false,
                confirmText: '确定',
                success: (res) => {
                    if (res.confirm) {
                        wx.getUserProfile({
                            desc: '提升用户体验感',
                            success: (res) => {
                                app.globalData.userInfo.usernameShow = res.userInfo.nickName;
                                app.globalData.userInfo.nickName = res.userInfo.nickName;
                                app.globalData.userInfo.headImgUrl = res.userInfo.avatarUrl;
                                that.setData({
                                    username: app.globalData.userInfo.usernameShow,
                                    role_name: app.globalData.roleInfo.name,
                                    roleCode: app.globalData.roleInfo.code,
                                });
                                util.post({
                                    url: '/sys/user/updateNoAuth',
                                    data: {
                                        id: app.globalData.userInfo.id,
                                        nickName: res.userInfo.nickName,
                                        usernameShow: res.userInfo.nickName,
                                        headImgUrl: res.userInfo.avatarUrl
                                    }
                                });
                            },
                            fail: (res) => {
                                util.toast('拒绝授权将影响您的体验感');
                                that.setData({
                                    username: '未知用户',
                                    role_name: app.globalData.roleInfo.name
                                });
                            }
                        });
                    }
                },
            });
        } else {
            that.setData({
                username: app.globalData.userInfo.usernameShow,
                role_name: app.globalData.roleInfo.name
            });
        }
        // 下一场会议
        util.get({
            url: '/api/meeting/info/getNextMeeting',
            success: (res) => {
                wx.hideLoading();
                if (res.data.code != 200) {
                    util.toast(res.data.msg);
                } else {
                    let datas = res.data.data;
                    if (datas) {
                        that.setData({
                            hasNext: true,
                            nextMeeting: datas,
                            nextTitle: datas.title,
                            nextTime: util.formatDate(datas.receptionTime),
                            nextUnit: datas.receptionObject
                        });
                    } else {
                        util.toast('暂无下一场会议');
                    }
                }
            }
        });
    },

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

    },

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

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

    },

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

    },

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

    },

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

    },

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

    },

    /**
     * 调试使用：切换角色菜单
     */
    changeRole: function () {
        let that = this;
        console.info('changeRole', that.data);
        if (that.data.roleCode == 'GUEST') {
            that.setData({
                role_name: '运维人员',
                roleCode: 'OPERATION',
            });
        } else if (that.data.roleCode == 'OPERATION') {
            that.setData({
                role_name: '工作人员',
                roleCode: 'ADMIN',
            });
        } else if (that.data.roleCode == 'ADMIN') {
            that.setData({
                role_name: '来宾',
                roleCode: 'GUEST',
            });
        }
    },

    /**
     * 点击查看下一场会议详情
     */
    meetingInfo: function () {
        let nextMeeting = this.data.nextMeeting;
        if (nextMeeting) {
            wx.navigateTo({
                url: '../meeting-cover/meeting-cover?item=' + JSON.stringify(nextMeeting),
            });
        } else {
            util.toast('暂无下一场会议');
        }
    },

    /**
     * 日常巡检
     */
    dailyInspection: function () {
        wx.navigateTo({
            url: '../daily-inspection/daily-inspection',
        });
    },

    /**
     * 发布会议信息
     */
    informationRelease: function () {
        wx.navigateTo({
            url: '../information-release/information-release',
        });
    },

    /**
     * 会议巡检
     */
    meetingInspection: function () {
        wx.navigateTo({
            url: '../meeting-list/meeting-list?type=INSPECTION',
        });
    },

    /**
     * 分享海报
     */
    meetingCover: function () {
        util.toast('功能暂未开放');

        wx.navigateTo({
            url: '../meeting-list/meeting-list',
        });
    },

    /**
     * 点击参观议程跳转至会议列表
     */
    visitAgenda: function () {
        wx.navigateTo({
            url: '../meeting-list/meeting-list',
        });
    },

    /**
     * 图片回传
     */
    uploadImage: function () {
        let that = this;
        wx.chooseImage({
            count: 1,
            success: function (res) {
                console.info(res.tempFilePaths);
                wx.uploadFile({
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    url: that.data.path + '/api/upload/record/upload',
                    header: {
                        "Content-Type": "multipart/form-data",
                        "Authorization": app.globalData.Authorization
                    },
                    formData: {
                        fileType: 10
                    },
                    success(res) {
                        console.info(res)
                    }
                });
            }
        });
    },

    /**
     * 内容投屏
     */
    contentScreen: function () {
        let wifi, pass;
        // 打开微信扫一扫
        wx.scanCode({
            onlyFromCamera: true,
            scanType: 'qrCode',
            success: (res) => {
                console.info(res);
                if (res.result.indexOf('WIFI') < 0) {
                    util.toast('请扫描WIFI二维码');
                    return;
                }
                wx.showLoading({
                    title: '加载中...',
                    mask: true
                });
                try {
                    let a = res.result.split(',');
                    wifi = a[0].split(':')[1];
                    pass = a[1].split(':')[1];
                } catch (error) {
                    util.toast('请扫描WIFI二维码');
                    return;
                }
                // 获取设备系统类型
                wx.getSystemInfo({
                    success: (res) => {
                        console.info(res);
                        if (!res.wifiEnabled) {
                            wx.hideLoading();
                            util.toast('请打开WIFI功能');
                            return;
                        }
                        // 1、初始化Wi-Fi模块
                        wx.startWifi({
                            success: (res) => {
                                console.info('startWifi', res)
                                if (res.errMsg == 'startWifi:ok') {
                                    // 4、连接Wi-Fi
                                    wx.connectWifi({
                                        SSID: wifi,
                                        password: pass,
                                        success: (res) => {
                                            wx.hideLoading();
                                            console.info('connectWifi', res);
                                            if (res.errMsg == 'connectWifi:ok') {
                                                util.toast('WIFI连接成功', 'success');
                                            } else {
                                                util.toast(res.errMsg);
                                            }
                                        },
                                        fail: (e) => {
                                            console.info(e);
                                            util.toast('WIFI连接失败');
                                        }
                                    });
                                } else {
                                    util.toast(res.errMsg);
                                }
                            },
                            fail: (e) => {
                                console.info(e);
                                util.toast('初始化WiFi模块失败');
                            }
                        });
                    },
                    fail: (e) => {
                        util.toast('获取设备系统类型失败');
                        return;
                    }
                });
            },
            fail: (e) => {
                console.info('chooseImage fail', e)
                if (e.errMsg == 'scanCode:fail cancel') {
                    util.toast('取消扫码');
                } else {
                    util.toast('扫码失败，请重试');
                }
            }
        });
    },

    /**
     * 聊天室
     * @param {*} e 
     */
    chatRoom: function (e) {
        wx.navigateTo({
            url: '../chat-room/chat-room',
        });
    },

    /**
     * 地图
     */
    lightMap: function () {
        wx.getSetting({
            withSubscriptions: true,
            success: function (res) {
                console.info(res);
                if (res.authSetting['scope.userLocation'] == undefined) {
                    wx.authorize({
                        scope: 'scope.userLocation',
                        success: function (res) {
                            console.info(res);
                            wx.navigateTo({
                                url: '../light-map/light-map',
                            });
                        },
                        fail: function (e) {
                            console.info(e);
                            wx.showToast({
                                title: '用户拒绝授权',
                                icon: 'error'
                            });
                        }
                    });
                } else if (res.authSetting['scope.userLocation'] == true) {
                    wx.navigateTo({
                        url: '../light-map/light-map',
                    });
                } else {
                    wx.showModal({
                        content: '请开启相关权限,以便更好地体验小程序',
                        success: (res) => {
                            if (res.confirm) {
                                wx.openSetting({
                                    success: (res) => {
                                        console.info('openSetting', res.authSetting)
                                        if (res.authSetting['scope.userLocation']) {
                                            util.toast('授权成功');
                                            wx.navigateTo({
                                                url: '../light-map/light-map',
                                            });
                                        } else {
                                            util.toast('用户未授权');
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            }
        });
    },

    /**
     * 文件浏览
     */
    fileBrowse: function () {
        wx.navigateTo({
            url: '../file-browse/file-browse',
        });
    },

    /**
     * 故障处理
     */
    faultHandle: function () {
        wx.navigateTo({
            url: '../fault-list/fault-list',
        })
    }

})