import {loginByUserInfo ,Login}from './../../utils/login'
console.log(Login)
import Toast from '../../components/vant-ui/toast/toast';
import {
    subcribe
} from '../../utils/utils'
const app = getApp();
Page({
    options: {
        addGlobalClass: true,
    },
    /**
     * 页面的初始数据
     */
    data: {
        cu_items: [{
                title: '我关注的房源',
                icon: 'house',
                value: '/pages/follow-list/follow-list'
            },
            {
                title: '关于我们',
                icon: 'about',
                value: '/pages/about/about'
            },
            {
                title: '消息订阅',
                icon: 'guanzhu',
                action: 'dingyue',
                value: 'null'
            }
        ],
        user_code: '',
        isLogined: app.globalData.token ? true : false,
        userinfo: app.globalData.userinfo
    },

    Navigator_to(e) {
        const {
            value,
            action
        } = e.currentTarget.dataset
        if (value) {
            if (action === 'dingyue') {
                this.subscribeMessage()
                return
            }
            wx.navigateTo({
                url: value,
            })
        }
    },
    toModifyUser() {
        if (!this.data.isLogined) {
            return
        }
        wx.navigateTo({
            url: '/pages/user-info/user-info',
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        this.getSubscribeStatus()
        wx.login({
            success: (res) => {
                this.setData({
                    user_code: res.code
                })
            }
        })
    },
    onShow: function () {

        this.setData({
            isLogined: app.globalData.token ? true : false,
            userinfo: app.globalData.userinfo
        });
    },
    getPhoneNumber(e) {
        Login(e, this.data.user_code, true).then(res => {
            this.setData({
                isLogined: app.globalData.token ? true : false,
                userinfo: app.globalData.userinfo
            })
        })
    },
    bindgetuserinfo(e) {
        // login(e, this.data.user_code, true).then(res => {
        loginByUserInfo(e, this.data.user_code, true).then(res => {
            this.setData({
                isLogined: app.globalData.token ? true : false,
                userinfo: app.globalData.userinfo
            })
        })
    },
    getSubscribeStatus() {
        wx.getSetting({
            withSubscriptions: true,
            success: (setting) => {
            
                // if (setting.subscriptionsSetting.itemSettings && setting.subscriptionsSetting.itemSettings['GApxDrE3tPhysRxHEmGHRn-0mvNJXqtZ4znZ8PJnQ8Q']) {
                if (setting.subscriptionsSetting.itemSettings && setting.subscriptionsSetting.itemSettings['zWTBJFpALA3KR3qx1keYJCzJdVtRjFbxrnOJc_Htn-g']) {
                    this.setData({
                        cu_items: [{
                                title: '我关注的房源',
                                icon: 'house',
                                value: '/pages/follow-list/follow-list'
                            },
                            {
                                title: '关于我们',
                                icon: 'about',
                                value: '/pages/about/about'
                            }
                        ]
                    })
                }
            },
        })
    },
    subscribeMessage() {
        subcribe().then(res => {
            wx.getSetting({
                withSubscriptions: true,
                success: (setting) => {
                    if (setting.subscriptionsSetting.itemSettings && setting.subscriptionsSetting.itemSettings[res.subId]) {
                        this.setData({
                            cu_items: [{
                                    title: '我关注的房源',
                                    icon: 'house',
                                    value: '/pages/follow-list/follow-list'
                                },
                                {
                                    title: '关于我们',
                                    icon: 'about',
                                    value: '/pages/about/about'
                                }
                            ]
                        })
                        return
                    }
                    Toast({
                        message: '请勾选：总是保持以上选择，不再询问'
                    });
                },
            })
        })




        return
    }
})