// pages/updateWord/updateWord.js
const app = getApp();
const WXAPI = require('../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    rightIcon:['closed-eye','closed-eye','closed-eye'],
    type:['password','password','password'],
    password:["","",""],
    role:''
  },
  onClickIcon(event){
    var rightIcon=this.data.rightIcon
    var type=this.data.type
    var index=event.currentTarget.dataset.index
    if(rightIcon[index]=="closed-eye"){
      rightIcon[index]="eye-o"
      type[index]="text"
    }else{
      rightIcon[index]="closed-eye"
      type[index]="password"
    }
    this.setData({
      rightIcon,
      type
    })
  },
  onChange(event) {
    var password=this.data.password
    password[event.currentTarget.dataset.index]=event.detail
    this.setData({
      password:password
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    if(options.role){
      this.setData({
        role:options.role
      })
    }
  },
  // 修改密码
  modifyPassword(){
    console.log(this.data.role)
    if(this.data.role == 1){
      var userInfo=wx.getStorageSync('userInfo')
      if (this.data.password[2] != this.data.password[1]) {
        wx.showToast({
          title: '确认密码输入错误！',
          duration:2000,
          icon:'none'
        })
      } else if (this.data.password[0] == '' || this.data.password[1] == ''|| this.data.password[2] == '') {
        wx.showToast({
          title: '请输入修改的密码',
          duration:2000,
          icon:'none'
        })
      } else {
        var data = {
          userId: userInfo.id,
          new: this.data.password[2],
          old: this.data.password[0],
        };      
        WXAPI.editPassword(data).then(res1 => {
          if (res1.success) {
            wx.showToast({
              title: "修改成功",
              duration: 2000,
              icon: 'none'
            })
            this.setData({
              rightIcon:['closed-eye','closed-eye','closed-eye'],
              type:['password','password','password'],
              password:["","",""]
            })
          } else {
            wx.showToast({
              title: "修改失败",
              duration: 2000,
              icon: 'none'
            })
          }
        })
      }
    }else if(this.data.role == 2){
      if (this.data.password[2] != this.data.password[1]) {
        wx.showToast({
          title: '确认密码输入错误！',
          duration:2000,
          icon:'none'
        })
      } else if (this.data.password[1] == ''|| this.data.password[2] == '') {
        wx.showToast({
          title: '请输入新的密码',
          duration:2000,
          icon:'none'
        })        
      } else {
        let deviceinfo = wx.getStorageSync('deviceinfoList')
        var rowInfo = JSON.parse(deviceinfo[0].info)
        let loginKey = WXAPI.setCode("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCXKHkNKieiGOTjXZJ0lFBjhRCa\nZTaAebTIfB9nnnVmNVIp//yCfyFXSlxbAj58CQwCVhwy109ROYf3BcsQNMZz7Djo\n8a5V0JThNye4ihxLiYLawLRjb2y2/IhzV0/JZiFtaM83xUt0tbqTKxD00Up7dy8c\n9OXc0h//0uQSraf6JwIDAQAB",this.data.password[2])
        var data = {
          loginKey: loginKey,
          number:rowInfo.userTelephone,
          signature:wx.getStorageSync('signature'),
        };
        WXAPI.updateCard(data).then(res=>{
          if(res.success){
            wx.showToast({
              title: '修改成功',
              duration:2000,
              icon:'none'
            })        
          }else{
            wx.showToast({
              title: '修改失败',
              duration:2000,
              icon:'none'
            })      
          }
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})