// case/pages/pwdLogin/pwdLogin.js
const app = getApp()
import {phoneLogin,getCaptcha,pwdLogin} from "../../../api/api"
import {auth} from "../../../api/auth.js"
import {throttle} from "../../../utils/util"
import {base64src} from "../../../utils/base64ToImg"
let urlHost=app.hostConfig().agreehost;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked:false,
        mobile:'',
        isPwd:true,
        pwd:'',
        loginDevice:5,
        showSuccess:false,
        redirecUrl:'',
        imgCode:'',
        captchaUuid:'',
        captchaCode:'',
    },
    inputChange(e){
        this.setData({
          [e.target.dataset.id]: e.detail.value
      });
    },
    goAgreement:throttle(function(event){
        app.method.navigateTo({
          url:"/case/pages/privacy/privacy"
        })
      }),
      goRegister:throttle(function(){
        app.method.navigateTo({
        url:'/case/pages/register/register'
        })
    }),
    handleGetCaptcha(){
        getCaptcha().then(res=>{
            base64src(res.captchaBase64Image,(img)=>{
              this.setData({
                imgCode:img,
                captchaUuid:res.captchaUuid
              })
            })
        })
      },
    handlePwdLogin:throttle(function(){
        let {mobile,pwd,loginDevice,captchaUuid,captchaCode,checked}=this.data;
        if(!checked){
            wx.showToast({
                title: '请同意《隐私协议》!',
                icon:'none'
              })
              return false
        };
        if(!captchaCode){
            wx.showToast({
                title: '请输入图形验证码',
                icon:'none'
              })
              return false
        }
        if(!pwd){
            wx.showToast({
                title: '请输入密码',
                icon:'none'
              })
              return false
        }
        pwdLogin({
            mobile,
            pwd,
            loginDevice,
            captchaUuid,
            captchaCode

        }).then(data=>{
            const { envVersion } = wx.getAccountInfoSync().miniProgram;
            let token=''
            if(envVersion=="develop" || envVersion=="trial"){
                token="DEV_CASE_TOKEN"
            }else{
                token="PROD_CASE_TOKEN"
            }
           wx.setStorageSync(token, data.token);
          let url=this.data.redirectUrl?this.data.redirectUrl:'/pages/index/index';
        
          if(url.indexOf('login')!=-1 || url.indexOf('mobileLogin')!=-1){
            wx.reLaunch({
                url:'/pages/index/index'
            })
          }else{
            wx.reLaunch({
              url:url
            })
          } 
        }).catch(error=>{
            if(error.code==10007){
               this.setData({
                 showSuccess:true,
                 message:error.msg,
                 mobile:error.data
               })
            }else{
                this.handleGetCaptcha();
            }
        })
    }),
    getPhoneNumber:throttle(function(e) {
      if (e.detail.errMsg == 'getPhoneNumber:ok'){
           auth().then(res => {
          phoneLogin('wx415cbcf96f4a3b27',{
              code:e.detail.code
          }).then((data)=>{
              const { envVersion } = wx.getAccountInfoSync().miniProgram;
              let token=''
              if(envVersion=="develop" || envVersion=="trial"){
                  token="DEV_CASE_TOKEN"
              }else{
                  token="PROD_CASE_TOKEN"
              }
             wx.setStorageSync(token, data.token);
            let url=this.data.redirectUrl?this.data.redirectUrl:'/pages/index/index';
          
            if(url.indexOf('login')!=-1 || url.indexOf('mobileLogin')!=-1){
              wx.reLaunch({
                  url:'/pages/index/index'
              })
            }else{
              wx.reLaunch({
                url:url
              })
            } 
          }).catch(error=>{
              if(error.code==10007){
                 this.setData({
                   showSuccess:true,
                   message:error.msg,
                   mobile:error.data
                 })
              }
          })
        });
      }else if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){
        console.log('您拒绝手机号授权')
      }else{
        wx.showToast({
          title: '手机号授权失败',
          icon:'none'
        })
      }
    }),
    onConfirmSuccess(){
        let {mobile}=this.data;
      this.setData({
          showSuccess:false
      })
      app.method.navigateTo({
          url:'/case/pages/improveInfo/improveInfo?mobile='+mobile
      })
    },
    toggleEye(){
      let {isPwd}=this.data;
      this.setData({
          isPwd:!isPwd
      })
    },
    onCancelSuccess(){
        this.setData({
            showSuccess:false
        })
      },
    goMobile(){
        let url='/case/pages/mobileLogin/mobileLogin'; 
        let redirectUrl=this.data.redirectUrl?this.data.redirectUrl:'/pages/index/index';
        app.method.navigateTo({
          url: url+"?redirectUrl="+encodeURIComponent(redirectUrl)
        })
      },
    
    handleAgree(){
        if(!this.data.checked){
          wx.showToast({
            title: '请同意《用户服务协议》!',
            icon:'none'
          })
          return false
        };
      },
    onChange(event) {
        this.setData({
          checked: event.detail,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.redirectUrl){ 
            if(options.redirectUrl){
                this.setData({
                  redirectUrl:decodeURIComponent(options.redirectUrl)
                })
              };
            //console.log(decodeURIComponent(options.redirectUrl))
         
        }
    },

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

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.handleGetCaptcha();
    },

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

    },

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

    },

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

    },

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

    },

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

    // }
})