// pages/login/login.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    account:'',
    password:'',
    type:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      type:options.type
    })
  },
  entercount(e){
    this.setData({
      account:e.detail.value
    })
  },
  enterpass(e){
    this.setData({
      password: e.detail.value
    })
  },
  login(){
    if(this.data.type==1){
      this.businesslogin()
    }else{
      this.countrylogin()
    }
  },
  // 营业场所登记
  businesslogin(){
    let that = this;
    var url = '/api/office/office_login';
    var params = {
      username: this.data.account,
      password: this.data.password

    }
    app.post(url, params, "post").then((res) => {
      console.log(res);
      wx.showToast({
        title: '登录成功',
        icon: "none"
      })
      wx.setStorageSync("office_token", res.office_token.token)

      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/logining/logining',
        })
      }, 1500)


    }).catch((err) => {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })


    })
   
  },

  // 乡镇登录
  countrylogin(){
    let that = this;
    var url = '/api/user/admin_login';
    var params = {
      username: this.data.account,
      password: this.data.password

    }
    app.post(url, params, "post").then((res) => {
      console.log(res);
      wx.showToast({
        title: '登录成功',
        icon: "none"
      })
      wx.setStorageSync("usertoken", res.admin_token.token)

      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/backpeoplelist/backpeoplelist',
        })
      }, 1500)


    }).catch((err) => {
      wx.showToast({
        title: err.data.msg,
        icon: 'none'
      })


    })
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let token = wx.getStorageSync("token");
    if (token == '') {
      wx.showToast({
        title: '请先授权登录',
        icon: "none"
      })

      setTimeout(function () {
        wx.navigateTo({
          url: '/pages/start/start',
        })
      }, 1500)
     
      
      let url = '/pages/login/login?type='+this.data.type;
      wx.setStorageSync("url", url)
      wx.setStorageSync("nologin", true)
    }

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})