import request from '~/api/request';

Page({
  data: {
    phoneNumber: '',
    isPhoneNumber: false,
    isCheck: false,
    isSubmit: false,
    isPasswordLogin: false,
    passwordInfo: {
      account: '',
      password: '',
    },
    radioValue: '',
    code: ''
  },
  onLoad() {
    // wx.login({
    //   success(res) {
    //     console.log(res.code)
    //     if (res.code) {
    //       //发起网络请求

    //     } else {
    //       console.log('登录失败！' + res.errMsg)
    //     }
    //   }
    // })
  },
  async getPhoneNumber(e) {
    // console.log(e.detail.code) // 动态令牌
    // console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
    // console.log(e.detail.errno) // 错误码（失败时返回）
    if (e.detail.code) {
      const res = await request('patient/quick_login', 'post', {
        code: e.detail.code
      })
      await wx.setStorageSync('access_token', res.token);
      if (res.is_personal_information_complete) {
        wx.navigateBack();
      } else {
        wx.navigateTo({
          url: `/pages/my/info-edit/index`,
        });
      }

    } else {
      wx.showToast({
        title: '登录失败',
        icon: 'none'
      })
    }
  },
  /* 自定义功能函数 */
  changeSubmit() {
    if (this.data.isPasswordLogin) {
      if (this.data.passwordInfo.account !== '' && this.data.passwordInfo.password !== '' && this.data.isCheck) {
        this.setData({
          isSubmit: true
        });
      } else {
        this.setData({
          isSubmit: false
        });
      }
    } else if (this.data.isPhoneNumber && this.data.isCheck) {
      this.setData({
        isSubmit: true
      });
    } else {
      this.setData({
        isSubmit: false
      });
    }
  },

  // 手机号变更
  onPhoneInput(e) {
    const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
    this.setData({
      isPhoneNumber,
      phoneNumber: e.detail.value,
    });
    this.changeSubmit();
  },

  // 用户协议选择变更
  onCheckChange(e) {
    const {
      value
    } = e.detail;
    this.setData({
      radioValue: value,
      isCheck: value === 'agree',
    });
    this.changeSubmit();
  },
  onCodeInput(e) {
    this.setData({
      code: e.detail.value
    });
  },

  onAccountChange(e) {
    this.setData({
      passwordInfo: {
        ...this.data.passwordInfo,
        account: e.detail.value
      }
    });
    this.changeSubmit();
  },

  onPasswordChange(e) {
    this.setData({
      passwordInfo: {
        ...this.data.passwordInfo,
        password: e.detail.value
      }
    });
    this.changeSubmit();
  },

  // 切换登录方式
  changeLogin() {
    this.setData({
      isPasswordLogin: !this.data.isPasswordLogin,
      isSubmit: false
    });
  },
  toLoginCode(){
    wx.navigateTo({
      url: `/pages/loginCode/loginCode`,
    });
  },
  async login() {
   
      const res = await request('patient/code_login', 'post', {
        code: this.data.code,
        mobile: this.data.phoneNumber
      });
      await wx.setStorageSync('access_token', res.token);
      if (res.is_personal_information_complete) {
        wx.navigateBack();
      } else {
        wx.navigateTo({
          url: `/pages/my/info-edit/index`,
        });
      }
      
  },
  // 获取验证码

  async getCode() {
    const res = await request('patient/send_code', 'post', {
      mobile: this.data.phoneNumber,
      type: 1
    });
  },
  uploadPass() {
   
    wx.navigateTo({
      url: `/pages/forGetPassword/index`,
    })
  }
});