var util = require('../../../utils/util.js');
var 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: {
    calendarConfig: {
      theme: 'elegant'
    },
    list: [],
    board:{}, 
    selected:[],//选中日期的服务单
    selectedDate: '未选择', //选中的日期和时间
    selectedTime: ''
  },
  afterTapDate(e) {//选中某个日期
    console.log('afterTapDate', e.detail)
    let that = this;
    var month = e.detail.month < 10 ? "0" + String(e.detail.month) : String(e.detail.month);
    var date = e.detail.date < 10 ? "0" + String(e.detail.date) : String(e.detail.date);
    that.setData({
      selectedDate:e.detail.year+'-'+month+'-'+date
    })
    that.data.list.forEach(item=>{
      if(item.date==e.detail.date){
        that.setData({
          selected:item.books
        })  
      }
    }) 
  },
  whenChangeMonth(e) {
    console.log('whenChangeMonth', e.detail)
    this.getScheduling();
  },
  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;
    // this.calendarSetEnabledDates(['2021-1-1']);
    this.getScheduling();

  },  
  onSwipe(e) {
    console.log('onSwipe', e)
  },
  getScheduling() { //获取可用排班  
    const {year, month } = calendar.getCurrentYM();
    let that = this;
    util.request(api.Board, {
      'month': year + '-' + month
    }).then(res => {
      if (res.errno != 0) {
        util.showErrorToast(res.errmsg);
        return;
      }
      console.info(res.data)
      that.setData({ 
        board:res.data
      })
      //获取当月的所有日期
      var dates = calendar.getCalendarDates({
        lunar: true
      })
      //一天多个订单 聚集到一个list
      var books=[];
      dates.forEach(function (item, index) {
        var _date = String(item.year) + '-' + String(item.month) + '-' + String(item.date);  
        var bookDate=null; 
        books.forEach(book=>{
          if(book.date==_date) 
            bookDate=book;
        })
       
        //是否包含当前日期
        res.data.details.forEach(element => {
          if (element.date == _date) { 
            if(bookDate==null) {
              bookDate={
                date:item.date,
                books:[]
              }; 
              books.push(bookDate);
            } 
            bookDate.books.push(element); 
          }
        })  
      })   
      //赋值显示
      books.forEach(item=>{
        if(item.books.length>0){  
          that.calenderTodo(item.date,item.books);
        }
      }) 
      that.setData({ 
        list:books
      });   
    }).catch(res => {
      console.info(res)
      util.showErrorToast(res.errmsg);
    })
  },
  calendarSetEnabledDates(dates) { //设置可选择的日期  
    //const calendar = this.selectComponent('#calendar').calendar;
    //console.info(dates); 
    //calendar['enableDates'](dates);
  },
  calenderTodo(date, title) { //设置指定日期  
    const {
      year,
      month
    } = calendar.getCurrentYM();
    const dates = [{
      year,
      month,
      date: date,
      todoText: title
    }]

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

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //type=0 只显示上午  1下午
    console.info(options.type)
    this.setData({
      type: options.type
    })
  },

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

  },

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

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

  },

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

  },

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

  },

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

  },

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

  }
})