// pages/login/login.js
import WxValidate from '../../utils/wxValidate'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    frameStyle: { useTop: true, name: '比赛信息', leftArrow: true, useBar: false },
    // 主体高度
    infoHeight: '',
    // 数据id
    id: '',
    //详情数据
    info: {},
    // 现场图片
    match_file: [],
    showModal: false,
    //修改比分
    form: [],
  },
  //验证是否输入
  initValidate() {
    const rules = { blue_branch: { required: true }, red_branch: { required: true, },blue_integral: { required: true }, red_integral: { required: true, } }
    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = { blue_branch: { required: '请输入蓝方比分', }, red_branch: { required: '请输入红方比分', },blue_integral: { required: '请输入蓝方积分', }, red_integral: { required: '请输入红方积分', } };
    this.WxValidate = new WxValidate(rules, messages)
  },
  back: function () {
    wx.navigateBack({ url: '/pages/home/index' })
  },
  //显示对话框
  clickme: function () {
    this.setData({showModal: true})
  },
  //关闭弹窗
  go: function () {
    this.setData({showModal: false})
  },
  //上传图片
  imgUpload: function (e) {
    const that = this;
    let data = that.data.match_file;
    let id = that.data.id;
    data.push(e.detail)
    wx.request({
      url: `${app.globalData.publicUrl}/courtAdmin/api/schedule/${id}`, //接口地址
      method: 'post',
      data: { match_file: data },
      success(res) {
        if (res.data.errcode == 0) {
          wx.showToast({
            title: '添加成功',
            duration: 2000,
            icon: 'success',
          })
          that.setData({ match_file: data })
        } else {
          wx.showToast({ title: res.data.errmsg, icon: 'error', duration: 2000 })
        }
      }
    })
  },
  //删除图片
  imgDel: function (e) {
    const that = this;
    let data = that.data.match_file;
    let id = that.data.id;
    let arr = data.filter((i, index) => index != e.detail.index)
    wx.request({
      url: `${app.globalData.publicUrl}/courtAdmin/api/schedule/${id}`, //接口地址
      method: 'post',
      data: { match_file: arr },
      success(res) {
        if (res.data.errcode == 0) {
          wx.showToast({
            title: '删除成功',
            duration: 2000,
            icon: 'success',
          })
          that.setData({ match_file: arr })
        } else {
          wx.showToast({ title: res.data.errmsg, icon: 'error', duration: 2000 })
        }
      }
    })
  },
  // 提交登录
  formSubmit: function (e) {
    const that = this;
    let id = that.data.id;
    const params = e.detail.value;
    params.match_file = that.data.match_file;
    if (!this.WxValidate.checkForm(params)) {
      const error = this.WxValidate.errorList[0];
      wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
      return false
    } else {
      wx.request({
        url: `${app.globalData.publicUrl}/courtAdmin/api/schedule/${id}`, //接口地址
        method: 'post',
        data: params,
        success(res) {
          if (res.data.errcode == 0) {
            wx.showToast({
              title: '保存成功',
              duration: 2000,
              icon: 'success',
              success: (res) => { that.back() }
            })
          } else {
            wx.showToast({ title: res.data.errmsg, icon: 'error', duration: 2000 })
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    that.setData({ id: options.id })
    //验证规则函数
    this.initValidate();
    // 计算高度
    this.searchHeight();
    // 监听用户是否登录
    this.watchLogin();
  },
  // 监听用户是否登录
  watchLogin: async function () {
    const that = this;
    let id = that.data.id;
    wx.getStorage({
      key: 'token',
      success: async res => {
        const arr = await app.$get(`/courtAdmin/api/schedule/${id}`);
        if (arr.errcode === 0) {
          that.setData({ info: arr.data })
          that.setData({ match_file: arr.data.match_file })
        }
      },
      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 - (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 () {

  }
})