// pages/login/index.js
const app = getApp()
const { sm2Encrypt } = require('../../utils/secret') //sm2Encrypt(formData.password)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account: '',
    password: '',
    showPass: false,
    code: '' //获取code用于传给后台获取OpenID
  },
  back: function () {
    wx.navigateBack()
  },
  toRegister: function () {
    wx.navigateTo({
      url: '../register/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //this.getCodeFun()
  },

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

  // 密码显示隐藏
  toggleShowPass() {
    this.setData({
      showPass: !this.data.showPass
    })
  },

  //审核
  toLink: function (e) {
    let { url } = e.currentTarget.dataset
    wx.navigateTo({
      url: url
    })
  },

  //授权手机号注册
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      app.request.POST({
        url: app.API.register,
        params: {
          'code': e.detail.code
        },
        page: this,
        isLoadingTxt: '注册中...',
        isToken: false,
        successFun: true
      }).then(res => {
        wx.showModal({
          title: '注册成功!',
          content: '默认密码为123456',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      })
    }
  },

  //提交登录事件
  formSubmit: function (e) {
    let formData = e.detail.value;
    var warn = ""; //弹框时提示的内容
    if (formData.account == '') {
      warn = "请输入账号！";
    } else if (formData.password == '') {
      warn = "请输入密码！";
    }
    if (warn != '') {
      app.util.checkForm(warn);
    } else {
      app.request.POST({
        url:app.API.login,
        params:{
          "account": formData.account,
          "password":sm2Encrypt(formData.password)
        },
        page:this,
        isToken:false,
        isLoadingTxt:'登录中...',
        successFun:true
      }).then(res=>{
        wx.setStorageSync('loginStatus', true); //登录状态
        wx.setStorageSync('Authorization', res.data.data); //token
        //获取字典数据
        app.request.GET({
          url:app.API.getTree,
          page:this,
          successFun:true
        }).then(res=>{
          app.globalData.treeArr = res.data.data
        })
        return app.request.GET({
          url:app.API.getUserInfo,
          isLoading:false,
          page:this,
          successFun:true
        })
      }).then(res=>{
        let { id, account, name, nickname, phone, unloadImg, roleCodeList, buttonCodeList } = res.data.data
        wx.setStorageSync('id', id); //用户id
        wx.setStorageSync('account', account); //用户姓名
        wx.setStorageSync('name', name); //用户姓名
        wx.setStorageSync('nickname', nickname); //昵称-车牌
        wx.setStorageSync('phone', phone); //电话
        wx.setStorageSync('unloadImg', unloadImg); //证书图片
        wx.setStorageSync('roleCodeList', roleCodeList.join(',')); //用户类型
        wx.setStorageSync('buttonCodeList', buttonCodeList); //用户类型
        wx.switchTab({
          url: '/pages/workstand/index',
        })
        // if (roleCodeList.join(',')=='bizAdmin') {
        //   wx.switchTab({
        //     url: '/pages/queueCar/index',
        //   })
        // } else if (roleCodeList.join(',')=='send') {
        //   wx.switchTab({
        //     url: '/pages/deliveryConfirm/index',
        //   })
        // } else if (roleCodeList.join(',')=='sale') {
        //   wx.switchTab({
        //     url: '/pages/salesOrderMan/index',
        //   })
        // } else{
        //   wx.switchTab({
        //     url: '/pages/index/index',
        //   })
        // }

      })

    }
  },

  /**
   * 获取code
   * 发送 res.code 到后台换取 openId, sessionKey, unionId
   */
  getCodeFun: function () {
    let _this = this;
    wx.login({
      success: res => {
        if (res.code) {
          _this.setData({
            code: res.code
          })
        }
      }
    })
  },

})