var util = require('../../utils/util.js')
var app = getApp()

Page({
  data: {
    isHiden: true,
    userurl: 'https://kptyun.cn/', //正式地址
    // userurl: 'http://192.168.1.57:8084/', //刘欢地址
    // userurl: 'http://210.16.189.72:8099/', //测试地址
    username: '',
    password: '',
    // username: '00020',
    //  password: '123456',
    isChecked: true,
    openid: '',
  },
  onLaunch: function () {},
  onLoad: function (option) {
    var that = this

    var url2 = this.data.userurl + 'getData'
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: url2,
            data: {
              code: res.code,
            },
            method: 'GET',
            header: {
              'Content-Type': 'json',
            },
            success: function (res) {
              console.log(res)
              var openid = res.data.openid //获取到的openid
              that.setData({
                openid: openid,
              })
            },
            fail: function (error) {
              console.log(error)
            },
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      },
    })

    var username = wx.getStorageSync('username')
    var password = wx.getStorageSync('password')
    if (username !== '' && password !== '') {
      this.setData({
        username: username,
        password: password,
      })
    }
  },
  //记住密码
  on_checked: function (e) {
    console.log(username, password)
    var check = e.detail.checked
    var username = this.data.username
    var password = this.data.password

    if (check) {
      console.log('选中')
      this.setData({ isChecked: true })
      wx.setStorageSync('username', username)
      wx.setStorageSync('password', password)
    } else {
      console.log('没选中')
      this.setData({ isChecked: false })
      wx.setStorageSync('username', '')
      wx.setStorageSync('password', '')
    }
  },

  formSubmit: function (e) {
    var username = e.detail.value.username
    var password = e.detail.value.password
    var userurl = this.data.userurl
    var openid = this.data.openid
    if (username == '' || password == '') {
      this.setData({
        tips: '用户名、密码或地址不能为空',
      })
    } else {
      var url = userurl + 'auth'
      var data = {
        username: username,
        password: password,
        openid: openid,
      }
      this.setData({
        username: username,
        password: password,
      })
      console.log('url:', url)
      util.ajax(url, data, 'POST', this.getUserData)
    }
  },

  getUserData: function (e) {
    console.log(e)

    wx.setStorageSync('usertoken', e.data.token)
    if (e.msg != 'ok') {
      var msg = e.data
      this.setData({
        tips: msg,
      })
    } else {
      var g_token = e.data.token
      // var g_infoname = e.data.info.userMsg["登录人名称"]
      var userurl = this.data.userurl
      var username = this.data.username
      var password = this.data.password

      var g_url = this.data.userurl
      app.globalData.g_url = g_url
      app.globalData.g_token = g_token
      // app.globalData.g_infoname = g_infoname
      wx.setStorageSync('userinfo', {
        userurl: g_url,
        username: this.data.username,
        password: this.data.password,
      })

      if (this.data.isChecked) {
        wx.setStorageSync('userurl', userurl)
        wx.setStorageSync('username', username)
        wx.setStorageSync('password', password)
      } else {
        wx.setStorageSync('username', '')
        wx.setStorageSync('password', '')
      }

      wx.setStorageSync('g_url', g_url)
      wx.setStorageSync('g_token', g_token)

      var that = this

      util.getDataByName(
        { name: 'findByUserPastureName', parammaps: { jwt_username: username } },
        function (e) {
          console.log(e)
          wx.setStorageSync('g_userId', e.data.list[0].employeId)

          var g_lgName = e.data.list[0].employeName
          var g_countName = e.data.list[0].username
          var g_depName = e.data.list[0].dname
          var g_lgNameId = e.data.list[0].empid
          var g_createrId = e.data.list[0].employeId
          var g_userId = e.data.list[0].employeId
          var g_roleId = e.data.list[0].roleId

          app.globalData.g_countName = g_countName
          app.globalData.g_depName = g_depName
          app.globalData.g_lgName = g_lgName
          app.globalData.g_lgNameId = g_lgNameId
          app.globalData.g_createrId = g_createrId
          app.globalData.g_userId = g_userId
          app.globalData.g_roleId = g_roleId

          that.linkJump()
        },
      )
    }
  },

  linkJump: function () {
    wx.switchTab({
      url: '../workbench/workbench',
    })
  },

  getUrl: function (e) {
    this.setData({
      userurl: e.detail.value,
    })
  },
})
