// pages/login/login.js
import WxValidate from '../../utils/wxValidate'
import { btn } from "../../utils/dict";
const app = getApp()
var url = app.globalData.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    frameStyle: { useTop: true, name: '我的', leftArrow: false, useBar: true },
    src: '/image/fen.jpg',
    src3: '/image/huang.png',
    src4: '/image/zan.png',
    showMore: false,
    // 主体高度
    infoHeight: '',
    total: '4',
    //上传比分
    match: [],
    //个人信息详情
    item1: {},
    //团队列表
    list: [],
    //头像
    icon: '',
    jumpList: [],
  },
  // 跳转菜单
  tabPath(e) {
    let { route } = e.detail.detail;
    if (route) wx.redirectTo({ url: `/${route}` })
  },
  //展开
  listToggle: function () {
    this.setData({ showMore: !this.data.showMore })
  },
  //修改个人信息
  modify: function () {
    wx.navigateTo({ url: `/pages/information/index` })
  },
  //上传比分
  upload: function (e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({ url: `/pages/score/index?id=${id}` })
  },
  //创建团队
  create: function () {
    wx.navigateTo({ url: `/pages/createTeam/index` })
  },
  //团队创建人-团队详情
  //个人用户-团队详情
  ToDetails: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({ url: `/pages/teamDetails/detail?id=${id}` })
  },
  // 我的服务-功能按钮
  toCommon: function (e) {
    let { route, method } = e.currentTarget.dataset;
    let list = JSON.stringify(e.currentTarget.dataset.item);
    let id = e.currentTarget.dataset.id;
    if (method == 'signout') {
      wx.showModal({
        title: '提示',
        content: '是否确认退出登录',
        success(res) {
          if (res.confirm) {
            wx.removeStorage({
              key: 'token',
              success(res) {
                return wx.redirectTo({ url: '/pages/index/index', })
              }
            })
          }
        }
      })
    } else {
      wx.navigateTo({ url: `/${route}?id=` + id + `&list=` + list })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 计算高度
    this.searchHeight()
    // 监听用户是否登录
    this.watchLogin();
  },
  // 监听用户是否登录
  watchLogin: function () {
    const that = this;
    wx.getStorage({
      key: 'token',
      success: res => {
        //查询用户信息
        that.searchUser(res.data);
        //查询团队列表
        that.searchTeam(res.data);
      },
      fail: res => {
        wx.redirectTo({ url: '/pages/login/index', })
      }
    })
  },
  //查询用户信息
  searchUser: function (e) {
    const that = this;
    var id = e.id;
    wx.request({
      url: `${app.globalData.publicUrl}/courtAdmin/api/user/${id}`,
      method: "get",
      data: {},
      header: {},
      success: res => {
        that.setData({ item1: res.data.data })
        let type = res.data.data.type;
        let icon = res.data.data.icon[0];
        if (icon) this.setData({ icon: icon.url })
        else this.setData({ icon: '/image/tou.png' });
        // 判断用户身份显示不同功能按钮
        let jump = btn.filter((i) => i.type.includes(type));
        if (jump) that.setData({ jumpList: jump });
      },
      error: err => {
      }
    })
  },
  //查询团队列表
  searchTeam: function (e) {
    let id = e.id;
    let type = e.type
    if (type == 1) {
      //团队管理员-团队列表
      wx.request({
        url: `${app.globalData.publicUrl}/courtAdmin/api/team`,
        method: "get",
        data: { create_id: id },
        header: {},
        success: res => {
          this.setData({ list: res.data.data })
          //查询比赛列表
          this.searchMatch(res.data.data);
        },
        error: err => {
        }
      })
    } else if (type == 2) {
      //个人用户-所在团队列表
      wx.request({
        url: `${app.globalData.publicUrl}/courtAdmin/api/team/userteams`,
        method: "get",
        data: { user_id: id },
        header: {},
        success: res => {
          this.setData({ list: res.data.data })

        },
        error: err => {
        }
      })
    }
  },
  //查询比赛列表
  searchMatch: async function (e) {
    const that = this;
    let id = e[0]._id;
    let data = [];
    const res = await app.$get('/courtAdmin/api/schedule', { red_id: id });
    if (res.errcode === 0) data = res.data;
    const arr = await app.$get('/courtAdmin/api/schedule', { blue_id: id });
    if (arr.errcode === 0) data = [...data, ...arr.data];
    that.setData({ match: data })
  },
  // 计算高度
  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 () {

  }
})