const app = getApp()
const util = require("../../../utils/util");
const api = require('../../../api/api.js');


import todo from '../../../lib/calendar/plugins/todo'
import selectable from '../../../lib/calendar/plugins/selectable'
import solarLunar from '../../../lib/calendar/plugins/solarLunar/index'
import timeRange from '../../../lib/calendar/plugins/time-range'
import week from '../../../lib/calendar/plugins/week'
import holidays from '../../../lib/calendar/plugins/holidays/index'
import plugin from '../../../lib/calendar/plugins/index'

var calendar;
plugin.use(todo).use(solarLunar).use(selectable).use(week).use(timeRange).use(holidays)

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hasLogin: false,
    calendarConfig: {
      theme: 'elegant'
    },
    list: [],
    curDate: util.formatDate(new Date()).substring(0, 7),
    selected: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getList(month) {
    let that = this;
    that.setData({
      curDate: month
    })

    wx.showLoading();
    util.request(api.NucleicList, {
      'month': month
    }).then(res => {
      //console.info(res.data)
      wx.hideLoading();
      that.setData({
        list: res.data
      })
      that.setScheduling();
    }).catch(res => {
      wx.hideLoading();
      util.showErrorToast(res.errmsg);
    })
  },
  goNucleic() {
    wx.navigateTo({
      url: '/pages/servant/nucleic/index',
    })
  },
  setScheduling() { //设置日历显示
    let that = this;
    //获取当月的所有日期 
    if (typeof (calendar) == 'undefined') {
      return;
    }

    //赋值显示 同一日期 合并成一条显示（采样时间+体温)
    let todos = [];
    that.data.list.forEach(item => {
        console.log("item:"+JSON.stringify(item)+",books:"+JSON.stringify(todos));
      let books = todos.filter(f => {
        return f.nucleicDate == item.nucleicDate
      });
      if (books.length == 0) { 
        todos.push({
          nucleicDate: item.nucleicDate,
          books: [item]
        });  
      }
      else{
        books[0].books.push(item);
      } 
    }) 

    console.info(todos)
    todos.forEach(todo => {
      that.calenderTodo(todo.nucleicDate, todo.books)
    });
  },
  whenChangeMonth(e) {
    console.log('whenChangeMonth', e.detail)
    const {
      year,
      month
    } = calendar.getCurrentYM();

    let m = month;
    if (m < 10) m = String(0) + m;
    this.getList(year + '-' + m);
  },
  whenChangeWeek(e) {
    console.log('whenChangeWeek', e.detail)
  },
  takeoverTap(e) {
    console.log('takeoverTap', e.detail)
  },
  afterCalendarRender(e) {
    console.log('afterCalendarRender', e);
    //初始化完成后，将插件变量赋值
    calendar = this.selectComponent('#calendar').calendar;
    const {
      year,
      month
    } = calendar.getCurrentYM();
    let m = month;
    if (m < 10) m = String(0) + m;
    this.getList(year + '-' + m);
  },
  afterTapDate(e) { //选中某个日期
    console.log('afterTapDate', e.detail.date)
    let that = this;
    let selected = '';
    var date = String(e.detail.date);
    this.data.list.forEach(item => {
      if (Number(item.nucleicDate.substring(8,10))== date&&selected=='') {
        selected = item; 
      }
    })
    that.setData({
      selected: selected
    })
  },
  onSwipe(e) {
    console.log('onSwipe', e)
  },
  calendarSetEnabledDates(dates) { //设置可选择的日期   
  },
  calenderTodo(date, title) { //设置指定日期  
    const {
      year,
      month
    } = calendar.getCurrentYM();
    const dates = [{
      year,
      month,
      date: date,
      todoText: title
    }]

    calendar['setTodos']({
      showLabelAlways: false,
      dates
    })
    //console.log('set todo: ', dates) ;
  },

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

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getList(this.data.curDate);
  },

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

  },

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

  },

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

  },

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

  },

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

  }
})