// pages/username_login/username_login.js
let log = require('../../../utils/log.js');
let Util = require("../../../utils/util");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loginName: null,
    password: null,
    checkPass: false,
    errorMsg: '',
    passwordFocus:false,
    servicetel: getApp().globalData.helpPhoneNum,
    queryip: ''
  },
  onError(e) {
    wx.showModal({
      title: '温馨提示',
      content: '当前网络环境较差，无法连接服务器，请稍后重试。有问题请联系客服电话4009608068，接听时段08:30-17:00。',
      showCancel: false,
      confirmColor:'#00AADD',
    });
  },
  inputAccount(e) {
    this.setData({
      loginName: e.detail.value
    });
    //console.log(e);
    if (this.data.loginName && this.data.password) {
      this.setData({
        checkPass: true
      });
    } else {
      this.setData({
        checkPass: false
      });
    }

  },
  inputPassword(e) {
    this.setData({
      password: e.detail.value
    });
    //console.log(e);
    if (this.data.loginName && this.data.password) {
      this.setData({
        checkPass: true
      });
    } else {
      this.setData({
        checkPass: false
      });
    }
  },
  loginUser(e) {
    let that = this;
    that.loginUserInner(e,that,that.data.queryip)
  },
  loginUserInner(e,that,ip) {
    console.info("fff")
    console.info(ip)
    let { loginName, password } = that.data;
    log.info('[登录]', '[登录]', '[请求]', { loginName, password });
    let ipp = ip;
    wx.login({
        success(loginRes) {
          wx.request({
            url: getApp().globalData.postHeadAgreement +'/restapi/wechat/login',
            data: {
              loginName,
              // ip:ipp,
              password,
              code: loginRes.code
            },
            method: 'POST',
            success(res) {
              //console.log(res1);
              //console.log(res1.data.code == 1);
              log.info('[登录]', '[登录]', '[响应]', res.data);
              if (res.data.code == 1) {
                if(res.data.msg!=null && res.data.msg.startsWith("503")){
                  getApp().showNetworkError();
                  return;
                }
                 that.setData({
                   password: null,
                   passwordFocus:true//,
                   //errorMsg: res.data.msg
                 });
                wx.showModal({
                  title: '提示',
                  content:res.data.msg,
                  showCancel:false,
                  confirmColor:'#00AADD',
                });
                log.info('[登录]', '[登录]', '[失败code==1]', res.data);
              } else {
                //返回该用户
                //跳转到上一页
                wx.setStorageSync("userInfo", res.data);
                wx.setStorageSync("isLogin", true);
                //let url = `/pages/ucenter/index/index`;
                log.info('[登录]', '[登录]', '[成功返回上一个界面]', res.data);
                wx.navigateBack();
                //wx.redirectTo({
                // url
                //});
              }
            },
            fail(e){
              getApp().showNetworkError();
            }
          });
        }
      });
  },
  phoneCall(){
    wx.makePhoneCall({
      phoneNumber: this.data.servicetel
    })
  },

  goToVcodeLogin(e){
    let url = '/pages/login/phone_login/phone_login';
    wx.redirectTo({
      url
    })
  },

  goToRegister(e) {
    console.log(e);
    let url = '/pages/register/register';
    wx.redirectTo({
      url
    })
  },

  goToForget(e){
    console.log(e);
    let url = '/pages/forget/forget';
    wx.redirectTo({
      url
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    log.info('[登录]', '[加载完成]');
    wx.request({
      url: 'https://ipv4.icanhazip.com/',
      method: 'POST',
      success(rrr){
        console.info(rrr);
        var query = (rrr == null?'':rrr==null?'':rrr);
        var query = (rrr == null?'':rrr==null?'':rrr);
        let queryip = query.data==null? '':query.data.replace("\n","");
        that.setData({
          queryip
        })
      },
      fail(rrr){
        that.setData({
          queryip: ''
        })
      }
    })


  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})