Page({
  data:{
    dates:['2025-11-07','2025-11-08','2025-11-09'],
    currentDate:'2025-11-07',
    schedules:[],
    activityName: '',
    fromCity: '',
    toCity: '',
    selectedType: '',
    tripType: '',
    tripLabel: '',
    venue: ''
  },
  onLoad(options){
    const { activityId, type, from, to, date, activityName, tripType, tripLabel, venue } = options;
    this.setData({
      selectedType: type,
      fromCity: decodeURIComponent(from || ''),
      toCity: decodeURIComponent(to || ''),
      currentDate: date || this.data.currentDate,
      activityName: decodeURIComponent(activityName || '活动'),
      tripType: tripType || '',
      tripLabel: decodeURIComponent(tripLabel || ''),
      venue: decodeURIComponent(venue || '')
    });
    
    // 生成日期范围
    this.generateDates(date);
    this.loadSchedules(this.data.currentDate);
  },
  
  generateDates(selectedDate) {
    const dates = [];
    const baseDate = selectedDate ? new Date(selectedDate) : new Date();
    
    for(let i = -1; i <= 5; i++) {
      const date = new Date(baseDate);
      date.setDate(baseDate.getDate() + i);
      const dateStr = `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2,'0')}-${date.getDate().toString().padStart(2,'0')}`;
      dates.push(dateStr);
    }
    
    this.setData({ dates });
  },
  
  loadSchedules(date){
    // 根据出行方式生成不同的班次数据
    let schedules = [];
    
    if (this.data.selectedType === 'business-charter') {
      // 包车服务
      schedules = [
        {id:'charter1',time:'任意时间',model:'奔驰V级商务车',price:1200,total:6,left:6,fromLoc:'指定地点接送',toLoc:'活动现场',type:'business-charter',duration:'约4小时',features:['专车接送','随时出发','豪华内饰']},
        {id:'charter2',time:'任意时间',model:'奥迪A6L',price:800,total:4,left:4,fromLoc:'指定地点接送',toLoc:'活动现场',type:'business-charter',duration:'约4小时',features:['商务专车','灵活时间','舒适体验']}
      ];
    } else {
      // 拼车服务
      schedules = [
        {id:'1',time:'09:00',model:'宇通大巴',price:88,total:46,left:12,fromLoc:'东直门地铁站A口',toLoc:'上海体育馆',type:'bus',duration:'约5小时',refundPolicy:'行程前72小时之前退票无手续费'},
        {id:'2',time:'13:30',model:'奔驰商务',price:168,total:6,left:2,fromLoc:'北京南站',toLoc:'上海体育馆',type:'business-share',duration:'约4.5小时',refundPolicy:'行程前72小时之前退票无手续费'},
        {id:'3',time:'15:00',model:'宇通大巴',price:88,total:46,left:8,fromLoc:'东直门地铁站A口',toLoc:'上海体育馆',type:'bus',duration:'约5小时',refundPolicy:'行程前72小时之前退票无手续费'},
        {id:'4',time:'18:00',model:'金龙客车',price:78,total:50,left:25,fromLoc:'北京南站',toLoc:'上海体育馆',type:'bus',duration:'约5小时',refundPolicy:'行程前72小时之前退票无手续费'}
      ];
    }
    
    this.setData({ schedules });
  },
  
  onDateChange(e){
    const d = e.currentTarget.dataset.date;
    this.setData({currentDate:d});
    this.loadSchedules(d);
  },
  
  book(e){
    const scheduleId = e.currentTarget.dataset.id;
    const schedule = this.data.schedules.find(s => s.id === scheduleId);
    
    wx.navigateTo({
      url:`/pages/order/order?scheduleId=${scheduleId}&activityName=${this.data.activityName}&type=${schedule.type}`
    });
  }
});