const app = require('../../utils/util.js');
const tools = require('../../utils/tools.js');
Page({
  data: {
    version:'',
    isRz: false,
    dataArr: [{
        img: "/images/person1.png",
        name: "我的培训",
        subname: "社会总是善待努力学习的人",
        url: '/pages/myTraining/myTraining'
      },
      {
        img: "/images/person2.png",
        name: "我的积分",
        subname: "积少成多，检验收获",
        url: '/pages/myPoints/myPoints'
      }, {
        img: "/images/person3.png",
        name: "我的互动",
        subname: "随时发布问题",
        url: '/pages/myInteractions/myInteractions'
      }, {
        img: "/images/person4.png",
        name: "后期跟踪",
        subname: "",
        url: '/pages/last/last'
      }, {
        img: "/images/person5.png",
        name: "身份认证",
        subname: "",
        url: '/pages/Identity/Identity'
      }, {
        img: "/images/person6.png",
        name: "使用指南",
        subname: "",
        url: '/pages/userGuide/userGuide'
      },
      {
        img: "/images/person7.png",
        name: "退出登录",
        subname: ""
      }
    ]
  },
  async goTurn(e) {
    const sessionKey = await tools.checkSessionAndLogin();
    let _this = this;
    if (e.currentTarget.dataset.index != 6) {
      wx.navigateTo({
        url: this.data.dataArr[e.currentTarget.dataset.index].url,
      })
    } else {
      wx.showModal({
        showCancel: true,
        content: "您确认要退出吗?",
        success(res) {
          console.log(res)
          if (res.confirm) {
            wx.showLoading({
              mask: true,
              title: '退出中',
            })

            wx.request({
              url: app.globalData.publicUrl + '/wx/student/mini-info',
              method: "DELETE",
              data: {
                sessionKey
              },
              success: (res) => {
                wx.hideLoading();
                if (res.data.code == 0) {
                  wx.showModal({
                    showCancel: false,
                    content: "退出登录成功！",
                    success(res) {
                      console.log(res)
                      if (res.confirm) {
                        console.log("dianji")
                        _this.setData({
                          isRz: true
                        })
                      }
                    }
                  })
                } else {
                  wx.showModal({
                    content: "您已处于未登录状态啦",
                    showCancel: false
                  })
                }
              },
              fail: (res) => {
                wx.hideLoading();
                console.log(res)
                wx.showModal({
                  content: "退出登录失败",
                  showCancel: false
                })
              }
            })
          }
        }
      })
    }
  },
  getInformation(sessionKey) {
    wx.request({
      url: app.globalData.publicUrl + '/wx/student/selStudentSessionKeyEcho',
      method: "post",
      data: {
        sessionKey
      },
      success: (res) => {
        console.log(res);
        console.log(res.data.msg)
        if (res.data.msg == '您尚未认证,请先进行身份认证!') {
          this.setData({
            isRz: true
          })
        } else {
          this.setData({
            isRz: false
          })
        }
      }
    })
  },
  goRz() {
    wx.navigateTo({
      url: '/pages/Identity/Identity'
    })
  },
  async onShow() {
    this.setData({version:app.globalData.version});
    
    const sessionKey = await tools.checkSessionAndLogin();
    this.getInformation(sessionKey);
  }
})