// pages/login/login.js
import WxValidate from '../../utils/wxValidate'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    frameStyle: { useTop: true, name: '申请加入', leftArrow: true, useBar: false },
    // 主体高度
    infoHeight: '',
    singledate: '2018-09-01',
    time: '12:01',
    form: {
      team_id: '1',// 团队id
      team_name: '团队名称', // 团队名称
      apply_time: '2022-03-22',// 申请时间
      apply_user: '申请人',// 申请人
      apply_id: '1',// 申请人id
    },
  },
  // 申请加入
  onSubmit: function (e) {
    let form = this.data.form;
    var team_id = form.team_id;
    var team_name = form.team_name;
    var apply_time = form.apply_time + ' ' + this.data.time;
    var apply_user = form.apply_user;
    var apply_id = form.apply_id;
    const params = {
      "team_id": team_id,
      "team_name": team_name,
      "apply_time": apply_time,
      "apply_user": apply_user,
      "apply_id": apply_id,
    };
    wx.request({
      url: `${app.globalData.publicUrl}/courtAdmin/api/joinapply`, //接口地址
      method: 'post',
      data: params,
      success(res) {
        if (res.data.errcode == 0) {
          wx.showToast({ title: `申请加入成功`, icon: 'success', duration: 2000 }) //创建成功提示
          wx.navigateTo({ url: '/pages/team/apply?id=' + team_id })// 跳转页面
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  //日期选择器
  bindDateChange3: function (e) {
    this.setData({
      singledate: e.detail.value
    })
  },
  //时间选择器
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },
  // 时间段选择  
  bindDateChange(e) {
    let that = this;
    that.setData({
      date: e.detail.value,
    })
  },
  // 时间段选择  
  bindDateChange2(e) {
    let that = this;
    that.setData({
      date2: e.detail.value,
    })
  },
  //返回
  back: function () {
    wx.navigateBack({ url: '/pages/home/index' })
  },
  //跳转页面
  backup: function () {
    let form = this.data.form;
    var team_id = form.team_id;
    wx.navigateTo({ url: '/pages/team/apply?id=' + team_id })// 跳转页面
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 监听用户是否登录
    this.watchLogin();
    // 计算高度
    this.searchHeight()
    //跳转传回数据
    var myObject = JSON.parse(decodeURIComponent(options.obj));
    let forms = {
      team_id: myObject.id,
      team_name: myObject.name,
      apply_time: '2022-03-22',
      apply_user: myObject.user,
      apply_id: myObject.cid
    }
    this.setData({
      form: forms
    });
  },
  // 监听用户是否登录
  watchLogin: function () {
    wx.getStorage({
      key: 'token',
      success: res => {
      },
      fail: res => {
        return wx.redirectTo({ url: '/pages/login/index', })
      }
    })
  },
   // 计算高度
   searchHeight: function () {
    let frameStyle = this.data.frameStyle;
    let client = app.globalData.client;
    let infoHeight = client.windowHeight;
    // 是否去掉状态栏
    if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
    // 是否减去底部菜单
    if (frameStyle.useBar) infoHeight = infoHeight - 50;
    if (infoHeight) this.setData({ infoHeight: infoHeight })
},

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})