import {loginByUserInfo, loginFn} from './../../utils/login'
import { VueLikePage } from '../../utils/page'
import Router from '../../utils/routes'
import { autoSubcrebe, subcribe } from '../../utils/utils'
const app = getApp();
VueLikePage([], {
    options: {
        addGlobalClass: true,
    },
    data: {
        cu_items: [{
                title: '名片夹',
                icon: 'my_contacts',
                value: 'businessCard',
                needLogin: true
            },
            {
                title: '我的收藏',
                icon: 'my_product',
                value: 'collectGoods',
                needLogin: true
            },
            // {
            //     title: '已收藏参展商',
            //     icon: 'my_enterprise',
            //     value: 'collectEnterprise',
            // },
            // {
            //     title: '数据报表',
            //     icon: 'my_reportform',
            //     value: 'dataReport',
            //     needLogin: true
            // },
            {
                title: '账号设置',
                icon: 'my_set',
                value: 'logout',
                needLogin: true
            },
            {
                title: '开启消息订阅',
                icon: 'cus_msg',
                value: 'message',
                needLogin: true,
                hiddenKey: 'isCustomer',
                hidden: true
            },
        ],
        user_code: '',
        isLogined: getApp().globalData.token ? true : false,
        userinfo: getApp().globalData.userinfo
    },
    methods: {
        Navigator_to(e) {
            const { value } = e.currentTarget.dataset
            const item = this.data.cu_items.find(item => item.value === value)
            if (item.needLogin && !getApp().globalData.token) {
                this.toLogin()
                return
            }
            if (value === 'message') {
                subcribe().then((res) => {
                    console.log(res, 'res')
                    this.onShow()
                })
                return
            }
            Router.push(value)
        },
        toModifyUser() {
            if (!this.data.isLogined) {
                return
            }
            wx.navigateTo({
                url: '/pages/user-info/user-info',
            })
        },
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (e) {
            this.isCustomer = true
            wx.login({
                success: (res) => {
                    this.setData({
                        user_code: res.code
                    })
                }
            })
        },
        onShow: function () {
            let cu_items = this.data.cu_items
            this.isCustomer = getApp().globalData.userinfo ? getApp().globalData.userinfo.type !== 'guide' : true
                autoSubcrebe().then(res => {
                    cu_items.forEach(item => {
                        if (item.hiddenKey) {
                            item.hidden = this[item.hiddenKey]
                        }
                        if (item.hiddenKey === 'isCustomer' && !this.isCustomer) {
                            item.hidden = res
                        }
                    })
                    this.setData({
                        cu_items,
                        isLogined: getApp().globalData.token ? true : false,
                        userinfo: getApp().globalData.userinfo,
                        showLogin: false
                    });
                })
            
            
            wx.login({
                success: (res) => {
                  if (res.code) {
                    this.wx_code = res.code
                  }
                }
              })
        },
        bindgetuserinfo (e) {
            if (!e.detail.userInfo) {
                return
              }
            wx.showLoading({
              title: '登录中',
            })
            loginByUserInfo(e.detail, this.wx_code).then(() => {
              wx.hideLoading({
                complete: (res) => {
                  this.onShow()
                },
              })
            })
            
          },
        toLogin () {
            // Router.push('login')
            this.setData({
                showLogin: true
            })
        },
        hideLogin () {
            this.setData({
                showLogin: false
            })
        },
        loginSuccess () {
            console.log('login')
            this.onShow()
        }
    }
})