// pages/login/index.js
import utils from "../../utils/util.js"
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: 'admin',
    password: 'Daofu123456',
    // username: '',
    // password: '',
    showLogin: true,
    newname:'',
    newphone:'',
    newpassword:'',
    address:'',
    showTypePop: false,
    postData:{},
    TypeData: [],
  },

  /**
   * 切换登录/注册
   */
  changeWay: function (e) {
    this.data.showLogin = e.currentTarget.dataset.type;
    this.setData(this.data)
  },

  /**
   * 绑定内容
   */
  inputMethed: function (e) {
    const type = e.currentTarget.dataset.type;
    let value = e.detail.value;
    this.data[type] = value;
    this.setData(this.data)
  },

  /**
   * 普通登录
   */
  login: function (params) {
    const type = params.currentTarget.dataset.type;
    this.data.type = type;
    if (type == 1) {
      if (!this.data.username) {
        wx.showToast({
          title: '请输入用户名',
          icon: 'none'
        })
        return
      }
      if (!this.data.password) {
        wx.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      }
      wx.showLoading({
        title: '登录中...',
      })
      this.handleLogin(this.data);
    } else {
      wx.showLoading({
        title: '登录中...',
      })
      wx.login({
        success: (res) => {
          this.data.code = res.code;
          this.handleLogin(this.data);
        },
      })
    }
  },
  // 注册
  register: function (params) {
    if (!this.data.newname) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (!this.data.newphone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      })
      return
    }
    if (!this.data.newpassword) {
      wx.showToast({
        title: '请输入登陆密码',
        icon: 'none'
      })
      return
    }
    if (!this.data.postData.orgcode) {
      wx.showToast({
        title: '请选择所属区域',
        icon: 'none'
      })
      return
    }
    let paramss = {
      areaCode: this.data.postData.orgcode,
      chinaname: this.data.newname,
      mobiletel: this.data.newphone,
      syspassword: this.data.newpassword,
      sysuserid: utils.uuid(),
    }
    app.axios("POST", "app", "/login/register", paramss).then(res => {
      if (res.code == 1) {
        this.data.showLogin = true;
        this.setData(this.data);
        wx.showToast({
          title: '注册成功',
          icon: 'none'
        })
      }else{
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 实际登录
   */
  handleLogin: function (params) {
    app.axios('POST', 'app', '/login/wxLogin', params, false).then(res => {
      wx.hideLoading()
      if (res.code == 1) {
        app.globalData.userInfo = res.data;
        wx.setStorage({
          key: 'userInfo',
          data: res.data,
        })
        if (res.data.usertype == 1) {
          wx.reLaunch({
            url: '/pages/index/index',
          })
        }else if (res.data.usertype == 2) {
          wx.reLaunch({
            url: '/pages/party-member/internalIndex/index',
          })
        }else if (res.data.usertype == 3) {
          wx.reLaunch({
            url: '/pages/the-masses/index/index',
          })
        }
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        })
      }
    })
  },
    /**
   * 显示/关闭弹窗
   */
  popModal: function (params) {
    const type = params.currentTarget.dataset.type;
    this.data[type] = !this.data[type];
    this.setData(this.data)
  },
   /**
   * 获取社区
   */
  getType: function () {
    app.axios("GET", "common", "/sysorg/getFirstOrg", {
      classify: 2
    }).then(res => {
      if (res.code == 1) {
        console.log(res,'res===>');
        this.data.TypeData = res.data;
        this.setData(this.data)
      }
    })
  },
    /**
   * 地址
   */
  typeChange: function (params) {
    console.log(params,'paramsparams===>');
    const index = params.currentTarget.dataset.index;
    this.data.postData.type = index + 1;
    this.data.postData.orgcode = this.data.TypeData[index].orgcode;
    this.data.showTypePop = false;
    this.setData(this.data)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (app.globalData.userInfo.userId) {
      if (app.globalData.userInfo.usertype == 1) {
        wx.reLaunch({
          url: '/pages/index/index',
        })
      }else if (app.globalData.userInfo.usertype == 2) {
        wx.reLaunch({
          url: '/pages/party-member/internalIndex/index',
        })
      }else if (app.globalData.userInfo.usertype == 3) {
        wx.reLaunch({
          url: '/pages/the-masses/index/index',
        })
      }
    };
    this.getType();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

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

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})