// pages/phoneLogin.js
import * as UTIL from '../utils/util.js';
import * as jsencrypt from '../utils/jsencrypt.js';
import * as API from '../utils/API.js';
let EVN_CONFIG = require('../env/env');

const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isIPX: app.globalData.isIPX,
    formData:{
      username:'',
      password:'',
      code:'',
    },
    codeUrl:''
  },
  back:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  onChange(event){
    this.setData({
      [event.currentTarget.dataset.value]: event.detail,
    })
  },
  onSubmit(){
    var that = this;
    if (this.data.formData.rememberMe) {
      wx.setStorageSync("username", this.data.formData.username, { expires: 30 });
      wx.setStorageSync('key', data)("password", encrypt(this.data.formData.password), { expires: 30 });
      wx.setStorageSync("rememberMe", this.data.formData.rememberMe, { expires: 30 });
    } else {
      wx.removeStorage("username");
      wx.removeStorage("password");
      wx.removeStorage("rememberMe");
    }
    //账号密码登录
    if (this.data.formData.username == "") {
      UTIL.showToastNoneIcon('账号不能为空')
      return false;
    } else if (this.data.formData.password == "") {
      UTIL.showToastNoneIcon('密码不能为空')
      return false;
    } else if (!this.data.formData.code) {
      UTIL.showToastNoneIcon('图片验证码不能为空')
      return false;
    }
    let form = {
      username:this.data.formData.username,
      password:this.data.formData.password,
      code:this.data.formData.code,
      uuid:this.data.formData.uuid,
    }
    form.username = jsencrypt.encrypt(this.data.formData.username);
    form.password = jsencrypt.encrypt(this.data.formData.password);

    console.log(jsencrypt.encrypt(this.data.formData.username))
    console.log(jsencrypt.encrypt(this.data.formData.password))
    console.log(jsencrypt.decrypt(form.username))
    console.log(jsencrypt.decrypt(form.password))
// return;
    wx.showLoading({
      title: '正在登录',
      mask:true
    })
    UTIL.httpRequestNoneDetal(API.URL_GET_LOGIN,form, "POST", {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          wx.hideLoading();
          wx.setStorageSync('token', res.token);
          getApp().globalData.userInfo.token = res.token;
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }else{
          wx.hideLoading();
          UTIL.showToastNoneIcon(res.msg)
          that.getCode();
        }
      }
    })

    // UTIL.httpRequestNoneDetal(API.URL_GET_GETPHONELOGIN+this.data.form.phone,{}, "GET", {
    //   success: (res) => {
    //     if (res.code == API.SUCCESS_CODE) {
    //       wx.setStorageSync('token', res.token);
    //       getApp().globalData.userInfo.token = res.token;
    //       wx.reLaunch({
    //         url: '/pages/index/index',
    //       })
    //     }else{
    //       UTIL.showToastNoneIcon(res.msg)
    //     }
    //   }
    // })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

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

  },
  getCode(){
    var that = this;
    UTIL.httpRequestNoneDetal(API.URL_GET_GETCODE,{}, "GET", {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.setData({
            ["formData.uuid"]:res.uuid,
            codeUrl:"data:image/gif;base64," + res.img
          })
        }else{
          UTIL.showToastNoneIcon(res.msg)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    UTIL.httpRequestNoneDetal(API.URL_GET_GETCODE,{}, "GET", {
      success: (res) => {
        if (res.code == API.SUCCESS_CODE) {
          that.setData({
            ["formData.uuid"]:res.uuid,
            codeUrl:"data:image/gif;base64," + res.img
          })
        }else{
          UTIL.showToastNoneIcon(res.msg)
        }
      }
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})