// pages/startPage/startPage.js
const api = require('../../api/api');
var monitApi = require("../../utils/util.js");
const { md5 } = require('../../utils/util.js');
const app = getApp()
Page({
  data: {
    jump: '',
  },
  onLoad(options) {
    var that = this
    that.setData({
      loginName: wx.getStorageSync('loginName'),
      password: wx.getStorageSync('password'),
      sysType: wx.getStorageSync('sysType'),
    })
    if (that.data.loginName && that.data.password) {
      if(that.data.sysType =='2'){
        that.data.jump = setTimeout(function () {
          that.servLogin()
        }, 1000);
      }else{
        that.data.jump = setTimeout(function () {
          that.login()
        }, 1000);
      }
    } else {
      that.data.jump = setTimeout(function () {
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }, 1500);
    }
    
  },
  /** 登录按钮 **/
  loginBox(e) {
    clearTimeout(this.data.jump);
    wx.navigateTo({
      url: '/pages/index/index',
    })
  },
  /** 代理商登录 **/
  login(data) {
    const that = this;
    wx.showLoading({
      title: '登录中',
    })
    var data = {}
    data.from = 'RROJECT_MANAGE';
    data.loginName = that.data.loginName;
    data.password = that.data.password;
    api.login(data).then(res => {
      wx.hideLoading()
      if (res.isLogin == 200) {
        wx.setStorageSync('loginName', data.loginName)
        wx.setStorageSync('password', data.password)
        wx.setStorageSync('userId', res.userId)
        wx.setStorageSync('userName', res.userName)
        wx.setStorageSync('userType', res.userType);
        wx.setStorageSync('traderType', res.traderType) //代理商等级
        //代理商员工角色菜单权限控制
        if (res.userType == "TRADER_EMP" && res.menuList) {
          let menuList = JSON.parse(res.menuList), obj={};
          if(menuList.length){
            menuList.forEach(itm=>{
              obj[itm] = true;
            });
          }
          wx.setStorageSync('permission', obj)
        }
        //平台列表按钮权限控制
        if (res.userType == "PLATFORM_EMP") {
          wx.setStorageSync('PTpermission', res.menus.split(","))
        }
        wx.switchTab({
          url: '/pages/home/home',
        })
      } else {
        that.setData({
          loginBox: false
        })
        wx.showToast({
          icon: 'none',
          title: res.errMsg || res.msg || '登录失败，请联系管理员',
        })
        setTimeout(()=>{
          wx.clearStorage()
          wx.reLaunch({
            url: '/pages/index/index',
          })
        },1500);
      }
    })
  },
  /**
   * 
   * 监控登录
   * 
   * @param {*} data 
   */
  servLogin() {
    wx.showLoading({
      title: '登录中',
    })
    var data = {
      grant_type: 'password',
      username: this.data.loginName,
      password: md5(this.data.password),
    }
    monitApi.req('oauth/miniapp/token', data, 'post').then(res => {
      wx.hideLoading()
      if (res.code == '200') {     
        monitApi.toast('登录成功', 1500, 'success')
        wx.setStorageSync('loginName', this.data.loginName)
        wx.setStorageSync('password', this.data.password)

        wx.setStorageSync('userId', res.data.user_id)
        wx.setStorageSync('name', res.data.real_name)
        wx.setStorageSync('TOKEN', res.data.access_token)
        wx.setStorageSync('RTOKEN', res.data.refresh_token)
        wx.setStorageSync('userType', res.data.token_type)
        wx.setStorageSync('sysType', '2')         
        wx.reLaunch({
          url: '/monitor/pages/home/home',
        })
      } else {
        monitApi.toast(res.msg, 1500, 'none')
      }
    }).catch(res => {
      console.log(res)
    })   
  },

  onShareAppMessage() {

  },
  onShareTimeline() {

  }
})