const app = getApp({ allowDefault: true });
let util = require('../../utils/util')
const apiArr = require('../../api/water_filter')

// pages/chooseEntrance/chooseEntrance.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        s: app.system.statusBarHeight, // 状态栏高度
        n: (app.menu.top - app.system.statusBarHeight) * 2 + app.menu.height, // 导航栏高度
        t: app.menu.top, // 胶囊局顶部距离
        h: app.menu.height, // 胶囊高度
    },
    //客户端
    navigateToClient() {

        if (wx.getStorageSync('ctoken') && wx.getStorageSync('loginMaster') == '1') {

            wx.getSetting({
                withSubscriptions: true,
                success: (res) => {
                    if (res.subscriptionsSetting && res.subscriptionsSetting.mainSwitch) {
                        const itemSettings = res.subscriptionsSetting.itemSettings || {};
                        const tmplMap = {
                            '5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0': '工单完成通知',
                            'qLYGETdlX5pR8WvTs8v4g4zlBOhA04z46KS_Q_yesr4': '工单派工通知',
                            '5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0': '订单付款通知'
                        };

                        const unsubscribedTmplIds = [];
                        Object.keys(tmplMap).forEach(tmplId => {
                            if (itemSettings[tmplId] !== 'accept') {
                                unsubscribedTmplIds.push(tmplId);
                            }
                        });

                        if (unsubscribedTmplIds.length > 0) {
                            wx.requestSubscribeMessage({
                                tmplIds: unsubscribedTmplIds,
                                success: (res) => {
                                    wx.navigateTo({
                                        url: '/pages/water_filter/water_filter',
                                    })
                                },
                                fail: (err) => {
                                    wx.navigateTo({
                                        url: '/pages/water_filter/water_filter',
                                    })
                                }
                            });
                        } else {
                            wx.navigateTo({
                                url: '/pages/water_filter/water_filter',
                            })
                        }
                    }
                },
                fail: () => {
                    wx.navigateTo({
                        url: '/pages/water_filter/water_filter',
                    })
                }
            });

          
        } else {

            
            wx.navigateTo({
                url: '/pages/login/login',
            })
        }
    },
    //师傅端
    navigateToWorker() {
        if (wx.getStorageSync('is_master')) {
            wx.navigateTo({
                url: '/packages/master/order/index',
            })
        } else {
            return wx.navigateTo({
                url: '/pages/MasterLogin/MasterLogin',
            })
        }

    },
    navigateToAdmin() {

        if (wx.getStorageSync('admin')) {
            wx.navigateTo({
                url: '/packages/partner/pages/partner',
            })
        } else {
            wx.navigateTo({
                url: '/packages/partner/pages/login/login',
            })
        }
    },


    getPermission() {
        let that = this
        wx.requestSubscribeMessage({
            tmplIds: ["5yPg-WOoP9-9ZU1fHjC4zg1KNaPWb76K87JzzKb58f0", "2BX7Zh5ccLzmHvvbCHuPWiaoZQyDCGfziCruxUct_EU", "eEetEKoNpqXk_EY0xqMt22_Xd7NFEyiODY6j0t3_A24"],
            success(res){
                console.log(res,'sucs');
            },
            fail(err){
                console.log(err,'err');
            }
            
        })

        wx.requestSubscribeMessage({
            tmplIds: ["re34uubgvrwJLaiM3LhQEmvxrRxoNcJbo7b8gcbxx44", "_s7GcsGNqapbnlLAJ5lUFexCEAx-dl4RD-DwwL9QqC4", "CWtF10H3syth9rdUaGr-4XojSa8TJMflb2z7zStw384"],
            success(res){
                console.log(res,'sucs');
            },
            fail(err){
                console.log(err,'err');
            }
        })
        wx.requestSubscribeMessage({
            tmplIds: ["qLYGETdlX5pR8WvTs8v4g4zlBOhA04z46KS_Q_yesr4", "T87KsBIrVrjgO4VETEOpIn4c4-bsxOTpzM6lR0ghduo", "q3Aa07wgYPgB23mu4JOuSzgFoiYhouUcw-NhV5CNuSA"],
            success(res){
                console.log(res,'sucs');
            },
            fail(err){
                console.log(err,'err');
            }
        })
        wx.requestSubscribeMessage({
            tmplIds: ["pUkdPwh7jWLjCWWv2zyz7I086xNO_GoaJQ6A-cYuGVg", "VXRbeTG6gPPOrxTSwF_da8jzqH0UVHxQykYAH0XH51s"],
            success(res){
                console.log(res,'sucs');
            },
            fail(err){
                console.log(err,'err');
            }
        })
    },


    getVersion() {
        wx.showLoading({
            title: '加载中...',
        })
        let that = this
        let version = ''
        const accountInfo = wx.getAccountInfoSync();
        if (accountInfo.miniProgram.version) {
            version = accountInfo.miniProgram.version
        } else {
            version = '1.0.24'
        }

        wx.setStorageSync('version', version)
        util.postUrl3(apiArr.getVersion, {}, version, res => {
            wx.hideLoading()
            if (res.versions) {
                let LoginType = res.versions.includes(version)
                wx.setStorageSync('LoginType', LoginType)
            } else {
                wx.setStorageSync('LoginType', false)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getVersion()
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})