const app = getApp();
const request = require('../../utils/request.js');
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';

Page({
  data: {
    isInfo: false,
    user: '',
    pwd: ''
  },
  onuserChange(e) {
    this.setData({
      user: e.detail
    })
  },
  onpwdChange(e) {
    this.setData({
      pwd: e.detail
    })
  },
  async login() {
    this.setData({
      usererr: this.data.user ? '' : '账号不能为空',
      pwderr: this.data.pwd ? '' : '密码不能为空',
    })
    if (this.data.user && this.data.pwd) {
      let options = {
        url: 'resource/miniapp/user/login',
        data: {
          loginName: this.data.user,
          password: this.data.pwd,
          accountId:  wx.getStorageSync('userId'),
          openId: app.globalData.openid
        },
        method: 'post'
      }
      let res = await request.query(options);
      console.log(res,'0000000000000')
      if (res.data.code==200) {
        wx.setStorageSync('openid', app.globalData.openid);
        this.getInfo();
        console.log('1111111111111');
      }else{
        wx.setStorageSync('openid', app.globalData.openid);
        Toast.fail(res.data.msg);
      }
    }
  },
  onLoad(data) {
    // if(!wx.getStorageSync('userId')){
    //   wx.showModal({
    //     title: '提示',
    //     content: '请扫描二维码进入小程序',
    //     showCancel: false,
    //     success: (res) => {
    //       if (res.confirm) {
    //         wx.navigateTo({
    //           url: '/pages/login/login',
    //         })
    //       }
    //     }
    //   })
    //   return false;
    // }
    if (data.user && data.pwd) {
      this.setData({
        user: data.user,
        pwd: data.pwd
      })
    }
    // this.getOpenId();
  },
  async getInfo() {
    let options = {
      url: 'resource/miniapp/user/getUserInfo',
      data: {
        openId: wx.getStorageSync('openid')
      },
      method: 'post'
    }
    let res = await request.query(options);
    console.log(res,'11111111111111111')
    if (res.data.code === 200) {
      let id = res.data.data.accountid? res.data.data.accountid:'';
      wx.setStorageSync('userId', id);
      app.globalData.userId = id;
      if(res.data.data.name){
        wx.switchTab({
          url: '/pages/warnLogs/warnLogs',
        })
      }else{
        wx.navigateTo({
          url: '/pages/info/info',
        })
      }
    }
  },
  register() {
    wx.navigateTo({
      url: '/pages/register/register',
    })
  }
})