// index.ts
// 获取应用实例
const app = getApp<IAppOption>();
import { getAction, postAction } from '../../../../api/base';

// import { Dayjs } from 'dayjs/index';
const dayjs = require('dayjs');
Page({
  data: {
    calendarAllList: [],
    recordListAll: [],
    recordList: [],
    show: false,
    selectDate: '' as any,
    selectDateString: '' as any
  },
  getSelectDate(e: any) {
    //用户点击日期项
    let status;
    if (dayjs(e.detail).format('YYYY-MM-DD') === dayjs().format('YYYY-MM-DD')) {
      //是否为当天的时间
      status = true;
    } else {
      status = false;
    }
    let day = '';
    switch (dayjs(e.detail).day()) {
      case 0:
        day = '日';
        break;
      case 1:
        day = '一';
        break;
      case 2:
        day = '二';
        break;
      case 3:
        day = '三';
        break;
      case 4:
        day = '四';
        break;
      case 5:
        day = '五';
        break;
      case 6:
        day = '六';
        break;
    }

    let recordList = [] as any;
    this.data.recordListAll.map((x: any) => {
      console.log(dayjs(e.detail).format('YYYY-MM-DD'), x.signInDate);
      console.log(dayjs(e.detail).format('YYYY-MM-DD') === x.signInDate);
      if (dayjs(e.detail).format('YYYY-MM-DD') === x.signInDate) {
        recordList.push(x);
      }
    });
    this.setData({
      selectDate: dayjs(e.detail).format('YYYY-MM-DD'),
      selectDateString: dayjs(e.detail).format('MM月DD日 周') + day,
      recordList: recordList
    });
    this.getCheckStatus(dayjs(e.detail).format('YYYY-MM-DD'));
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  selectMonth(e: any) {
    //用户切换月份
    console.log(e);
    let { date, calendarAllList, isShow } = e.detail;
    this.getMonthCheckRecord(date, calendarAllList, isShow);
  },
  getMonthCheckRecord(
    month: number | string,
    calendarAllList: any,
    isShow: boolean
  ) {
    let parms = {
      month: month
    };
    getAction('api/driverSignIn/monthList', parms).then((res: any) => {
      let result: any = res.result;
      let findIndex = calendarAllList.findIndex(
        (x: any) => dayjs(x.month).format('YYYY-MM') === month
      );
      calendarAllList[findIndex].list.forEach((dateItem: any) => {
        //isClock 0不显示红点或蓝点 1签到展示红点  2未签到展示红点
        for (let i = 0; i < result.length; i++) {
          if (
            result[i].signInDate === dayjs(dateItem.date).format('YYYY-MM-DD')
          ) {
            dateItem.isClock = 1;
          }
        }
      });
      console.log(calendarAllList);
      this.setData({
        calendarAllList
      });

      if (!isShow) {
        this.selectComponent('#selectDate').getCurrentWeek();
      }
    });
  },
  getCheckStatus(date: string) {
    let parms = {
      signInDate: date
    };
    getAction('api/driverSignIn/list', parms).then((res: any) => {
      if (res.code == 200) {
        res.result.map((x: any) => {
          x.signInTime = x.signInTime.split(' ')[1];
        });
        let recordList = [] as any;
        console.log(123);
        res.result.map((x: any) => {
          console.log(x);
          if (
            dayjs(this.data.selectDate).format('YYYY-MM-DD') === x.signInDate
          ) {
            console.log(123);
            recordList.push(x);
          }
        });
        this.setData({
          recordListAll: res.result,
          recordList: recordList
        });
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none'
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 防止切后台后拿不到数据，所以重新初始化
    this.selectComponent('#selectDate').data.selectedIndex = '';
    this.selectComponent('#selectDate').initDate();
    this.selectComponent('#selectDate').getMonthDate();
    // this.getSelectDate({ detail: new Date() });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    Promise.all([this.getSelectDate({ detail: new Date() })]).then(() => {
      // this.selectComponent('#selectDate').data.selectedIndex = '';
      // this.selectComponent('#selectDate').initDate();
      // this.selectComponent('#selectDate').getMonthDate();
      this.getCheckStatus(dayjs(new Date()).format('YYYY-MM-DD'));
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

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

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

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

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
