// pages/changePsw/index.js
const app = getApp();
import { customRequest } from '../../utils/request.js';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    disabled: 0,
    getmsg: "获取验证码",
    btnColor: false,
    tel: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    if (options.type) {
      this.setData({
        type: options.type,
      });
      if (options.type == 1) {
        wx.setNavigationBarTitle({
          title: "找回密码",
        });
      } else {
        wx.setNavigationBarTitle({
          title: "修改登录密码",
        });
      }
    }
  },
  changeBtn: function (e) {
    console.log(e);
    if (this.verifyTel(e.detail.value)) {
      this.setData({
        disabled: "1",
        btnColor: true,
        tel: e.detail.value,
      });
    } else {
      this.setData({
        btnColor: false,
        disabled: 0,
      });
    }
  },
  /**
   * 手机号合规校验
   *
   *
   */
  verifyTel (value) {
    console.log(value);
    var myreg = /^[1][3456789][0-9]{9}$/;
    if (myreg.test(value)) {
      return true;
    } else {
      return false;
    }
  },
  /**
   * 密码重复校验
   *
   *
   */
  verify (newStr, oldStr) {
    if (newStr != oldStr) {
      return false;
    } else {
      return true;
    }
  },
  /**
   * 表单信息提交校验
   *
   *
   */
  beforeFormSubmit (e) {
    console.log(e);
    wx.showLoading({
      title: "提交中",
      mask: true,
      success: (result) => { },
    });
    let that = this;
    let value = e.detail.value;
    if (e.currentTarget.dataset.type == 1) {
      if (value.tel == "" || !this.verifyTel(value.tel)) {
        app.hideLoad();
        app.showTips(that, "请输入正确手机号");
        return;
      }
      if (value.code == "") {
        app.hideLoad();
        app.showTips(that, "验证码不能为空");
        return;
      }
      if (value.newPsw.trim() == "" || value.rePsw.trim() == "") {
        app.hideLoad();
        value.newPsw.trim() == "" ? app.showTips(that, "请输入新密码") : app.showTips(that, "请再次输入密码");
        return;
      }
      if (!this.verify(value.newPsw.trim(), value.rePsw.trim())) {
        app.hideLoad();
        app.showTips(that, "前后密码不一致");
        return;
      }
      app.hideLoad();
      this.formSubmit(value, 1);
    } else {
      console.log(app.globalData.user);
      if (value.tel == "") {
        app.hideLoad();
        app.showTips(that, "请输入原密码");
        return;
      }
      if (value.newPsw.trim() == "" || value.rePsw.trim() == "") {
        app.hideLoad();
        value.newPsw.trim() == "" ? app.showTips(that, "请输入新密码") : app.showTips(that, "请再次输入密码");
        return;
      }
      if (!this.verify(value.newPsw.trim(), value.rePsw.trim())) {
        app.hideLoad();
        app.showTips(that, "前后密码不一致");
        return;
      }
      this.formSubmit(value, 2);
    }
  },
  /**
   * 表单信息提交
   *
   *
   */
  formSubmit (value, type) {
    let that = this;
    if (type == 1) {
      console.log(value);
      customRequest("/yishoudan/updatePassword", {
        header: 'headers', method: 'GET', data: {
          tel: value.tel,
          code: value.code,
          password: value.newPsw,
        }
      }).then(({ data }) => {
        if (data.status == 200) {
          app.showTips(that, "修改成功");
          setTimeout(() => {
            if (app.globalData.isLogin) {
              wx.navigateBack({
                target: 1,
                success: (result) => { },
              });
            } else {
              wx.reLaunch({
                url: "/pages/login/index?type=psw",
                success: (result) => { },
              });
            }
          }, 2000);
        } else {
          app.showTips(that, data.msg);
        }
      })
      // wx.request({
      //   url: app.globalData.ip + "/yishoudan/updatePassword",
      //   data: {
      //     tel: value.tel,
      //     code: value.code,
      //     password: value.newPsw,
      //   },
      //   header: {
      //     "content-type": "application/json",
      //   },
      //   success: function ({ data }) {
      //     console.log(data);
      //     if (data.status == 200) {
      //       app.showTips(that, "修改成功");
      //       setTimeout(() => {
      //         if (app.globalData.isLogin) {
      //           wx.navigateBack({
      //             target: 1,
      //             success: (result) => { },
      //           });
      //         } else {
      //           wx.reLaunch({
      //             url: "/pages/login/index?type=psw",
      //             success: (result) => { },
      //           });
      //         }
      //       }, 2000);
      //     } else {
      //       app.showTips(that, data.msg);
      //     }
      //   },
      // });
    } else {
      customRequest("/yishoudan/updatePasswordByPwd", {
        header: 'headers', method: 'post', data: {
          tel: app.globalData.user.tel,
          oldPassword: value.oldPsw,
          password: value.newPsw,
        }
      }).then(({data}) => {
        if (data.status == 200) {
          app.showTips(that, "修改成功");
          setTimeout(() => {
            if (app.globalData.isLogin) {
              wx.navigateBack({
                target: 1,
                success: (result) => { },
              });
            } else {
              wx.reLaunch({
                url: "/pages/login/index?type=psw",
                success: (result) => { },
              });
            }
          }, 2000);
        } else {
          app.showTips(that, data.msg);
        }
      })
      // wx.request({
      //   url: app.globalData.ip + "/yishoudan/updatePasswordByPwd",
      //   data: {
      //     tel: app.globalData.user.tel,
      //     oldPassword: value.oldPsw,
      //     password: value.newPsw,
      //   },
      //   method: 'post',
      //   header: app.globalData.headers,
      //   success: function ({ data }) {
      //     console.log(data);
      //     if (data.status == 200) {
      //       app.showTips(that, "修改成功");
      //       setTimeout(() => {
      //         if (app.globalData.isLogin) {
      //           wx.navigateBack({
      //             target: 1,
      //             success: (result) => { },
      //           });
      //         } else {
      //           wx.reLaunch({
      //             url: "/pages/login/index?type=psw",
      //             success: (result) => { },
      //           });
      //         }
      //       }, 2000);
      //     } else {
      //       app.showTips(that, data.msg);
      //     }
      //   },
      // });
    }
  },
  /**
   * 改变修改密码类型
   *
   *
   */
  changeType () {
    this.setData({
      type: 1,
    });
    wx.setNavigationBarTitle({
      title: "找回密码",
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () { },
  /**
   * 获取验证码
   *
   *
   */
  sendMsg: function () {
    console.log(this.data.tel);
    var that = this;

    that.setData({
      btnColor: false,
      disabled: 0,
    });

    that.getCode();
    var timer = 1;
    if (timer == 1) {
      timer = 0;
      var time = 60;
      var inter = setInterval(function () {
        that.setData({
          getmsg: time + "s",
          btnColor: false,
          disabled: 0,
        });
        time--;
        if (time < 0) {
          timer = 1;
          clearInterval(inter);
          that.setData({
            getmsg: "重新获取",
            btnColor: true,
            disabled: 1,
          });
        }
      }, 1000);
    }
  },
  getCode: function () {
    var that = this;
    customRequest("/commons/sendMsgCode",{header:'headers', method: 'GET', data: {tel: that.data.tel}}).then((res)=>{
      if (res.data.status == 200) {
        app.showTips(that, "验证码发送成功");
      } else if (res.data.status == 9999) {
        app.dialogNotLogin();
      } 
    })
    // wx.request({
    //   url: app.globalData.ip + "/commons/sendMsgCode",
    //   data: {
    //     tel: that.data.tel,
    //   },
    //   header: app.globalData.headers,
    //   method: "GET",
    //   success: function (res) {
    //     console.log("发送短信验证码");
    //     console.log(res);
    //     if (res.data.status == 200) {
    //       app.showTips(that, "验证码发送成功");
    //     } else if (res.data.status == 9999) {
    //       app.dialogNotLogin();
    //     } else {
    //     }
    //   },
    // });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () { },

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

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

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

  /**
   * 用户点击右上角分享
   */
onShareAppMessage() {
  return app.sharePageImage()
},
});
