import { pwdLogin, workerLogin, bindWecaht } from '../../api/login'
import util from '../../utils/util'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '',
    userPwd: '',
    showAccountPwd: false , // 是否使用账号密码登录
  },

  onShow() {
    this.setTokenExpireTime()
  },
  setTokenExpireTime () {
    const tokenExpireTime = wx.getStorageSync('tokenExpireTime')
    if (util.getDate() !== tokenExpireTime) {
      wx.clearStorageSync()
    }
    this.handleLogin()
  },
  handleLogin () {
    const accessToken = wx.getStorageSync('accessToken')
    if (accessToken) {
      this.handleTokenLogin()
    } else {
      this.handleWXLogin()
    }
  },
  handleTokenLogin () {
    this.getStorageData()
    this.handleDeviceFn(wx.getStorageSync('device'))
  },
  handleWXLogin () {
    const workerLoginCode = wx.getStorageSync('workerLoginCode')
    if (workerLoginCode == '403') {
      this.handleCode403()
    } else {
      var that = this
      wx.login({
        success (res) {
          if (res.code) {
            that.workerLoginFn(res.code)
          } else {
            wx.showModal({
              content: '登录失败!' + res.errMsg,
              confirmColor: '#333',
              showCancel: false
            })
          }
        }
      })
    }
  },
  workerLoginFn (code) {
    const that = this
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    workerLogin({code: code}).then(res => {
      wx.hideLoading()
      wx.vibrateShort()
      that.setStorageData(res.data)
      that.handleDeviceFn(res.data.device)
      wx.setStorageSync('workerLoginCode', '000')
    }).catch(e => {
      wx.hideLoading()
      if (e.code == '403') {
        wx.setStorageSync('workerLoginCode', '403')
        that.handleCode403()
      } else {
        wx.showModal({
          content: e.msg,
          confirmColor: '#333',
          showCancel: false
        })
      }
    })
  },
  handleCode403 () {
    this.setData({
      showAccountPwd: true
    })
  },
  pwdLoginFn () {
    const that = this
    if (this.data.username == '') {
      wx.showToast({
        title: '请输入账号',
        icon: 'error'
      })
    } else if (this.data.userPwd == '') {
      wx.showToast({
        title: '请输入密码',
        icon: 'error'
      })
    } else {
      var data = {
        username: this.data.username,
        userPwd: this.data.userPwd
      }
      wx.showLoading({
        title: '登录中...',
        mask: true
      })
      pwdLogin(data).then(res => {
        wx.hideLoading()
        const response = res.data
        const isBindWechat = response.isBindWechat
        const device = response.device
        that.setStorageData(response)
        if (!isBindWechat) {
          wx.showModal({
            content: '请绑定微信',
            confirmColor: '#333',
            showCancel: false,
            success (res) {
              if (res.confirm) {
                that.bindWecahtFn(device)
              }
            }
          })
        } else {
          wx.vibrateShort()
          that.handleDeviceFn(device)
        }
      }).catch(e => {
        console.log(e,'eeeee');
        wx.hideLoading()
        wx.showModal({
          content: e.msg,
          confirmColor: '#333',
          showCancel: false
        })
      })
    }
  },
  bindWecahtFn (device) {
    const that = this
    wx.showLoading({
      title: '绑定中...',
      mask: true
    })
    wx.login({
      success (codeRes) {
        if (codeRes.code) {
          bindWecaht({code: codeRes.code}).then(res => {
            wx.hideLoading()
            wx.vibrateShort()
            wx.setStorageSync('workerLoginCode', '000')
            wx.showToast({
              title: '绑定成功',
              icon: 'success'
            })
            that.handleDeviceFn(device)
          }).catch(e => {
            wx.hideLoading()
            wx.showModal({
              content: e.msg,
              confirmColor: '#333',
              showCancel: false
            })
          })
        }
      }
    })
  },
  handleDeviceFn (device) {
    if (device !== null) {
      wx.redirectTo({
        url: '/pages/workbench/workbench?form=login&deviceName=' + device.deviceName
      })
    } else {
      wx.redirectTo({
        url: '/pages/permissions/permissions',
      })
    }
  },
  setStorageData (data) {
    app.globalData.accessToken = data.accessToken
    app.globalData.userName = data.userName
    app.globalData.headImg = data.headImg
    app.globalData.isBindWechat = data.isBindWechat
    app.globalData.workerId = data.workerId
    app.globalData.device = data.device
    wx.setStorageSync('accessToken', data.accessToken)
    wx.setStorageSync('tokenExpireTime', util.getDate())
    wx.setStorageSync('headImg', data.headImg)
    wx.setStorageSync('userName', data.userName)
    wx.setStorageSync('isBindWechat', data.isBindWechat)
    wx.setStorageSync('workerId', data.workerId)
    wx.setStorageSync('device', data.device)
  },
  getStorageData () {
    const accessToken = wx.getStorageSync('accessToken')
    const headImg = wx.getStorageSync('headImg')
    const userName = wx.getStorageSync('userName')
    const isBindWechat = wx.getStorageSync('isBindWechat')
    const workerId = wx.getStorageSync('workerId')
    const device = wx.getStorageSync('device')
    app.globalData.accessToken = accessToken
    app.globalData.userName = userName
    app.globalData.headImg = headImg
    app.globalData.isBindWechat = isBindWechat
    app.globalData.workerId = workerId
    app.globalData.device = device
  }
})