// pages/theClasses/theClasses.js
const app = getApp()
const util = require("../../utils/util")
const urlDef = require("../../utils/urls")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aheadTime: 0,
    eventList: [],
    holidayList: [],
    dateList: [],
    date: null,
    week: 0,
    weekMap: ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'],
    tipList: [
      '1.每月仅有2次调课机会。如撤销,默认是为使用一次调整机会\r\n\r\n',
      '2.允许调课至原课程前后一周内。例如:今天11-30日,可调整到11.23到12.07之间的课程\r\n\r\n',
      '3.可提前7天进行调课\r\n\r\n',
      '4.调入后的课程,不可再调及请假'
    ],
    transferDay: 0,
    transferCount: 2,
    monthCount: 0,
    curDate: null,
    bd: null
  },

  getTip: function () {
    var str = ''
    for (var i in this.data.tipList) {
      str += this.data.tipList[i]
    }
    wx.showModal({
      title: '调课说明',
      content: str,
      showCancel: false,//是否显示取消按钮
      cancelText: "否",//默认是“取消”
      cancelColor: '#999999',//取消文字的颜色
      confirmText: "我知道了",//默认是“确定”
      // confirmColor: 'skyblue',//确定文字的颜色
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = options.endDate;
    if (date) {
      this.setData({ bd: date })
    }
    const urls = urlDef.urls;
    let stu = wx.getStorageSync('student');
    if (stu) {
      util.apiPost(urls.get_sys_params + '&q.orgId=' + stu.orgId + '&q.id=1000_8').then((rs) => {
        if (rs && rs.length > 0) {
          this.setData({ transferDay: parseInt(rs[0].sysVal, 10) })
        }
      });

      util.apiPost(urls.get_sys_params, { 'q.orgId': stu.orgId, 'q.id': 'f8158acabeeb44ec9ff651aade6b295f' }).then((rs) => {
        if (rs && rs.length > 0) {
          // 将分钟转为毫秒
          this.setData({ 'aheadTime': parseInt(rs[0].sysVal, 10) * 60 * 1000 })
        }
      });
      util.apiPost(urls.get_schedule_month, { 'q.studentId': stu.studentId }).then((rs) => {
        if (rs && rs.length > 0) {
          rs.map(o => {
            o.id = o.studentId
          })
          this.setData({ 'eventList': rs })
        }
      });
      util.apiPost(urls.get_holidays, { 'q.companyId': stu.orgId }).then((rs) => {
        if (rs && rs.length > 0) {
          this.setData({ 'holidayList': rs })
        }
      });
    }
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let date = this.data.bd
    if (date) {
      let t = this;
      setTimeout(() => { t.selectDate(date) }, 500)
    }
  },

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

  },

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

  },

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

  },

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

  },

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

  },
  selectDate: function (v) {
    this.setData({ curDate: v, bd: v })
    let vd = new Date(v);
    let week = this.data.weekMap[vd.getDay()];
    let list = this.data.eventList.filter(o => {
      return o.attenceDate == v;
    });

    let endDate = new Date(new Date().getTime() + 604800000)
    if (this.data.transferDay > 0) {
      const temDate = new Date();
      temDate.setDate(temDate.getDate() + this.data.transferDay);
      endDate = temDate;
    }
    let today = util.curTime().substring(0, 10).replace(/\//g, '-')
    endDate = util.formatTime(endDate).substring(0, 10).replace(/\//g, '-')
    let stu = wx.getStorageSync('student');
    let urls = urlDef.urls;
    util.apiPost(urls.get_attence_count + '&q.status=3&q.studentId=' + stu.studentId + '&q.month=' + v.substring(0, 7)).then((rs) => {
      this.setData({ monthCount: rs })


      list.map(o => {
        o.bt = o.beginTime.substring(11, 16);
        o.et = o.endTime.substring(11, 16);
        if (o.status == 0 && o.isAttend == 0) {
          if (this.data.monthCount >= this.data.transferCount) { // 本月内调过2次课的,不允许再调课
            o.transferFlag = false;
          } else {
            // 晚于当前时间才能调课(当天都不行) 2周内
            if (o.attenceDate > today && o.attenceDate <= endDate) {
              o.transferFlag = true;
            } else {
              o.transferFlag = false;
            }
          }
        }
      })
      this.setData({ 'date': v, 'week': week, 'dateList': [{ items: list, date: v, week: week }] });
    }).catch(e => { });
  },
  callSelectDate: function (d) {
    let v = d.detail.dateString;
    this.selectDate(v);
  }
})