import {
  imgServerUrl
} from '../../../../config/config.js'
import {
  showToast
} from '../../../../utils/tips.js'
import {
  logout_new
} from '../../../../services/index'
import {
  rootUrl
} from '../../../../config/config'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rootUrl,
    imgServerUrl: imgServerUrl,
    visible: false,
    actions: [{
      name: '确定注销',
      color: '#0177FF',
      fontWeight: 500
    },
    {
      name: '取消',
      color: '#0177FF',
    },
    ],
    version: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getAccountInfoSync) {
      const info = wx.getAccountInfoSync()
      this.setData({
        version: info.miniProgram.version || app.globalData.version
      })
    } else {
      this.setData({
        version: app.globalData.version
      })
    }
  },
  toclear() {
    wx.showModal({
      title: '清除缓存',
      content: '是否清除所有缓存内容',
      success(res) {
        if (res.confirm) {
          try {
            wx.clearStorage({
              success: function (res) {
                wx.clearStorageSync()
                app.globalData.userId = '';
                app.globalData.userToken = '';
                app.globalData.memberId = '';
                app.globalData.userInfo = null;
                showToast('清除成功')
              }
            })
          } catch (e) {
            // Do something when catch error
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toabout() {
    wx.navigateTo({
      url: '../about/index',
    })
  },
  updateVersion() {
    // 小程序版本更新
    if (wx.getUpdateManager) {
      const updateManager = wx.getUpdateManager()
      updateManager.onCheckForUpdate((res) => {
        // 请求完新版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(() => {
            wx.showModal({
              title: '更新提示',
              content: '新版本已经准备好，是否重启应用？',
              success: (res) => {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  wx.removeStorageSync("user_id");
                  wx.removeStorageSync("member_id");
                  wx.removeStorageSync("user_token");
                  wx.removeStorageSync("userInfo");
                  this.globalData.userId = '';
                  this.globalData.userToken = '';
                  this.globalData.memberId = '';
                  this.globalData.userInfo = null;
                  updateManager.applyUpdate()
                }
              }
            })
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: '当前已经是最新版本了~',
          })
        }
      })
      updateManager.onUpdateFailed(function () {
        // 新版本下载失败
        wx.showToast({
          icon: 'none',
          title: '新版本下载失败请稍后重试！',
        })
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '微信版本过低，请手动删除后重新搜索进入！'
      })
    }
  },
  toPage(e) {
    const {
      url
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url,
    })
  },
  cancellation() {
    this.setData({
      visible: true
    })
  },
  readFile() {
    wx.downloadFile({
      url: '../../docx/用户协议.docx',
      success: res => {
        console.log(res)
      }
    })
    wx.openDocument({
      filePath: '../../docx/用户协议.docx',
    })
  },
  login_out() {
    wx.showModal({
      title: '提示',
      content: '确定退出登录吗?',
      success: function (res) {
        if (res.confirm) {
          //清除登录缓存
          wx.removeStorageSync("user_id");
          wx.removeStorageSync("member_id");
          wx.removeStorageSync("user_token");
          wx.removeStorageSync("userInfo");
          app.globalData.userId = '';
          app.globalData.userToken = '';
          app.globalData.memberId = '';
          app.globalData.userInfo = null;
          showToast('退出成功');
          setTimeout(function () {
            wx.navigateBack();
          }, 500)
        } else if (res.cancel) {

        }
      }
    })
  },
  handel(e) {
    const {
      index
    } = e.detail;
    this.setData({
      visible: false
    })
    if (index === 0) {
      logout_new({}).then(data => {
        wx.removeStorageSync("user_id");
        wx.removeStorageSync("member_id");
        wx.removeStorageSync("user_token");
        wx.removeStorageSync("userInfo");
        app.globalData.userId = '';
        app.globalData.userToken = '';
        app.globalData.memberId = '';
        app.globalData.userInfo = null;
        wx.showToast({
          icon: 'none',
          mask: true,
          title: '注销成功',
          success: res => {
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }, 1500)
          }
        })
      })
    }
  },

})