// pages/personCenter/accountSetting/accountSetting.js
const { baseUrl, globalImgUrl } = require("../../../request");
const { notLogin } = require('../../../utils/util')


Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName: "",
    userAvatar: "",
    phoneNumber: "",
    globalImgUrl,
    role: '',
  },

  gotoResetPwd() {
    const { role } = this.data;
    wx.navigateTo({
      url: `/pages/personCenter/resetPwd/resetPwd?role=${ role }`,
    })
  },

  logOut() {
    wx.request({
      url: baseUrl + '/userInfo/mini/logout',
      method: 'GET',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code === 1) {
          wx.showToast({
            title: '退出成功',
            icon: 'success'
          })
          wx.removeStorageSync('token')
          wx.removeStorageSync('role')
          setTimeout(() => {
            wx.reLaunch({
              url: '/pages/welcome/homePage/homePage',
            })
          }, 1000); // 1000ms = 1秒
        } else {
          wx.showToast({
            title: res.data.message,
            icon: 'error'
          })
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误，请重试', icon: 'none' });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const token = wx.getStorageSync('token');

    // 获取用户信息
    wx.request({
      url: baseUrl + '/userInfo/get/jwt',
      method: 'GET',
      header: {
        Authorization: token
      },
      success: res => {
        console.log('用户信息---->',res.data.data);
        if (res.data.code === 1) {
          this.setData({
            nickName: res.data.data.nickName,
            userAvatar: res.data.data.userAvatar,
            phoneNumber: res.data.data.phoneNumber,
            userAccount: res.data.data.userAccount,
            role:res.data.data.userRole,
          })
        } else {
          notLogin(res.data.message)
        }
      },
      fail: () => {
        wx.showToast({ title: '网络错误，请重试', icon: 'none' });
      }
    })
  },
  
  gotoAgreement() {
    wx.navigateTo({
      url: '/pages/loginModule/agreement/agreement',
    })
  },

  gotoPolicy() {
    wx.navigateTo({
      url: '/pages/loginModule/privacyPolicy/privacyPolicy',
    })
  },
 
})