// pages/appointment/index.js
Page({
  data: {
    appointmentList: [],
    services: [
      { id: 1, name: '面部护理', duration: 60, price: 298 },
      { id: 2, name: '身体按摩', duration: 90, price: 398 },
      { id: 3, name: '美甲服务', duration: 45, price: 158 },
      { id: 4, name: '美发造型', duration: 120, price: 258 }
    ],
    selectedDate: '',
    selectedTime: '',
    selectedService: null,
    timeSlots: ['09:00', '10:00', '11:00', '13:00', '14:00', '15:00', '16:00', '17:00'],
    currentDate: '',
    dateList: []
  },

  onLoad: function (options) {
    this.generateDateList();
    this.setData({
      selectedDate: this.data.dateList[0].date,
      currentDate: this.data.dateList[0].date
    });
    
    // 从本地存储获取预约列表
    const appointmentList = wx.getStorageSync('appointmentList') || [];
    this.setData({ appointmentList });
  },

  onShow: function () {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().init();
    }
    
    // 刷新预约列表
    const appointmentList = wx.getStorageSync('appointmentList') || [];
    this.setData({ appointmentList });
  },
  
  // 下拉刷新
  onPullDownRefresh: function() {
    // 刷新预约列表
    const appointmentList = wx.getStorageSync('appointmentList') || [];
    this.setData({ appointmentList }, () => {
      wx.stopPullDownRefresh();
    });
  },

  // 生成未来7天的日期列表
  generateDateList: function() {
    const dateList = [];
    const today = new Date();
    
    for (let i = 0; i < 7; i++) {
      const date = new Date(today);
      date.setDate(today.getDate() + i);
      
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      const weekday = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()];
      
      dateList.push({
        date: `${year}-${month}-${day}`,
        day: day,
        weekday: weekday
      });
    }
    
    this.setData({ dateList });
  },

  // 选择日期
  selectDate: function(e) {
    const date = e.currentTarget.dataset.date;
    this.setData({ selectedDate: date });
  },

  // 选择时间
  selectTime: function(e) {
    const time = e.currentTarget.dataset.time;
    this.setData({ selectedTime: time });
  },

  // 选择服务
  selectService: function(e) {
    const serviceId = e.currentTarget.dataset.id;
    const service = this.data.services.find(item => item.id === serviceId);
    this.setData({ selectedService: service });
  },

  // 提交预约
  submitAppointment: function() {
    const { selectedDate, selectedTime, selectedService } = this.data;
    
    if (!selectedDate || !selectedTime || !selectedService) {
      wx.showToast({
        title: '请完成所有选择',
        icon: 'none'
      });
      return;
    }
    
    // 创建新预约
    const newAppointment = {
      id: Date.now(),
      date: selectedDate,
      time: selectedTime,
      service: selectedService,
      status: '待确认'
    };
    
    // 更新预约列表
    const appointmentList = [...this.data.appointmentList, newAppointment];
    
    // 保存到本地存储
    wx.setStorageSync('appointmentList', appointmentList);
    
    this.setData({ 
      appointmentList,
      selectedTime: '',
      selectedService: null
    });
    
    wx.showToast({
      title: '预约成功',
      icon: 'success'
    });
  },

  // 取消预约
  cancelAppointment: function(e) {
    const id = e.currentTarget.dataset.id;
    const appointmentList = this.data.appointmentList.filter(item => item.id !== id);
    
    // 更新本地存储
    wx.setStorageSync('appointmentList', appointmentList);
    
    this.setData({ appointmentList });
    
    wx.showToast({
      title: '已取消预约',
      icon: 'success'
    });
  }
})