const {
  regPwd
} = require("../../../utils/util");

// pages/info/index.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showPwdPop: false,
    userInfo: {},
    postData: {}
  },

  /**
   * 解绑
   */
  unbindWX: function () {
    const userId = this.data.userInfo.userId;
    wx.showModal({
      title: '微信解绑',
      content: '是否解绑当前账号？',
      complete: (res) => {
        if (res.confirm) {
          app.axios('POST', 'app', `/User/userUnbinding/${userId}`, {}, false).then(res => {
            if (res.code == 1) {
              this.data.userInfo.openid = '';
              this.setData(this.data)
              wx.showToast({
                title: '操作成功',
                icon: 'none'
              })

            } else wx.showToast({
              title: res.message,
              icon: 'none'
            })
          })
        }
      }
    })
  },

  /**
   * 绑定
   */
  bindWX: function () {
    const userId = this.data.userInfo.userId;
    wx.login({
      success: (res) => {
        app.axios('POST', 'app', '/User/userBinding', {
          userId,
          code: res.code
        }, true).then(res => {
          if (res.code == 1) {
            this.data.userInfo.openid = res.data;
            this.setData(this.data)
            wx.showToast({
              title: '绑定成功',
              icon: 'none'
            })
          } else wx.showToast({
            title: res.message,
            icon: 'none'
          })
        })
      },
    })
  },

  /**
   * 协议
   */
  deal: function (params) {
    wx.navigateTo({
      // url: `/pages/web/index?title=用户协议和隐私协议`,
      url: `/pages/agreement/index`,

    })
  },
  /**
   * 修改名字
   */
  fixName: function (params) {
    this.data.showNamePop = !this.data.showNamePop
    this.setData(this.data)
  },

  /**
   * 修改密码弹窗
   */
  changePwd: function () {
    this.data.showPwdPop = !this.data.showPwdPop
    this.setData(this.data)
  },

  /**
   * 输入框
   */
  inputedit: function (e) {
    let dataset = e.currentTarget.dataset;
    let value = e.detail.value;
    this.data[dataset.obj][dataset.item] = value;
    this.setData({
      postData: this.data[dataset.obj]
    });
  },

  /**
   * 提交
   */
  submit: function (e) {
    const type = e.currentTarget.dataset.type;
    if (type == 1) {
      if (!this.data.postData.oldPassword) {
        wx.showToast({
          title: '请输入旧密码',
          icon: 'none'
        })
        return
      } else if (!this.data.postData.password) {
        wx.showToast({
          title: '请输入新密码',
          icon: 'none'
        })
        return
      } else if (!regPwd(this.data.postData.password)) {
        wx.showToast({
          title: '密码格式不正确',
          icon: 'none'
        })
        return
      } else if (!this.data.postData.checkPassword) {
        wx.showToast({
          title: '请确认新密码',
          icon: 'none'
        })
        return
      } else if (this.data.postData.password !== this.data.postData.checkPassword) {
        wx.showToast({
          title: '两次密码输入不一致！',
          icon: 'none'
        })
        return
      }

      this.data.postData.userId = app.globalData.userInfo.userId;
      app.axios('POST', 'app', '/User/uploadPassword', this.data.postData, true).then(res => {
        if (res.code == 1) {
          this.data.showPwdPop = false
          this.setData(this.data)
          wx.showToast({
            title: '修改成功',
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: 'none'
          })
        }
      })
    } else {
      this.data.showPwdPop = false
      this.setData(this.data)
    }
  },

  /**
   * 退出登录
   */
  exit: function (params) {
    wx.showModal({
      title: '温馨提示',
      content: '是否退出当前账号？',
      complete: (res) => {
        if (res.confirm) {
          app.globalData.userInfo = {}
          wx.reLaunch({
            url: '/pages/login/index',
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.userInfo = app.globalData.userInfo;
    this.setData(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})