// pages/clockIn/index.js
import request from '~/api/request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [],
    planId: '',
  },
  
  async getList(){
    const res = await request('patient/follow_plans', 'get', {
      page: 1,
      page_size: 100,
      type: 2
    })
    
    // Process the list to check dates
    const processedList = res.list.map(item => {
      const planDate = new Date(item.plan_date);
      const today = new Date();
      const diffTime = planDate - today;
      const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
      // 早于今天且 status 为 1，变为 2
      if (diffDays < 0 && item.status === 1) {
        return { ...item, status: 2 };
      }
      // If within 7 days and status is 1, update to 2
      if (diffDays <= 7 && diffDays >= 0 && item.status === 1) {
        return { ...item, status: 3 };
      }
      return item;
    });
    this.setData({
      dataList: processedList
    })
    const actObj = processedList.find(item => item.status === 2);
    if (actObj) {
      this.setData({
        planId: actObj.id
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    
  },

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

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

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

  },

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

  },

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

  },

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

  },

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

  },
  toQuestionnaire(e) {
    const { id, time, name } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/followUp/index?planId=${id}&name=${name}&time=${time}` ,
    })
  },
  goQuestionnaire(e) {
    const data = this.data.dataList[0]
    wx.navigateTo({
      url: `/pages/followUp/index?planId=${data.id}&name=${data.plan_name}&time=${data.plan_date}` ,
    })
  },
  toRegister() {
    wx.navigateTo({
      url: '/pages/register/index',
    })
  },
  toEmergency(){
    wx.navigateTo({
      url: '/pages/emergency/index',
    })
  }
})