<template>
  <u-popup 
    :show="visible" 
    mode="bottom" 
    round="20"
    :closeable="false"
    @close="close"
    :safeAreaInsetBottom="true"
    customStyle="height: 80vh;"
  >
    <view class="time-picker-container">
      <!-- 标题栏 -->
      <view class="picker-header">
        <text class="picker-title">期望上门时间</text>
        <text class="close-btn" @tap="close">×</text>
      </view>
      
      <!-- 主要内容区域：左侧日期 + 右侧时间 -->
      <view class="main-content">
        <!-- 左侧日期选择 -->
        <scroll-view class="date-sidebar" scroll-y>
          <view 
            v-for="(date,index) in dateList" 
            :key="date.value"
            class="date-item"
            :class="{ 
              active: selectedDate === date.value,
              today: date.isToday
            }"
            @tap="selectDate(date.value,index)"
          >
            <view class="date-content">
              <text class="date-name" 
				  :class="{ 
					active: selectedDate === date.value,
					today: date.isToday
					}">{{ date.label }}</text>
              <text class="date-info">{{ date.month }}月{{ date.day }}日</text>
              <text class="date-week">{{ date.week }}</text>
            </view>
          </view>
        </scroll-view>
        
        <!-- 右侧时间选择 -->
        <scroll-view class="time-content" scroll-y>
          <!-- 推荐时间段（仅今天显示） -->
          <view v-if="selectedDate === todayDate && withinOneHourSlot" class="recommend-section">
            <view class="recommend-header">
              <text class="recommend-title">一小时内</text>
            </view>
            <view 
              class="recommend-time"
              :class="{ active: selectedTime === withinOneHourSlot.value && selectedDay ===  selectedCurrentDay}"
              @tap="selectTime(withinOneHourSlot.value, true)"
            >
              <text class="time-value">{{ withinOneHourSlot.label }}</text>
            </view>
          </view>
          
          <!-- 普通时间段列表 -->
          <view class="time-section">
            <view class="section-title">可选时间段</view>
            <view v-if="loading" class="loading">加载中...</view>
            <view v-else class="time-list">
              <view 
                v-for="timeSlot in timeSlots" 
                :key="timeSlot.value"
                class="time-item"
                :class="{ 
                  active: selectedTime === timeSlot.value && selectedDay ===  selectedCurrentDay,
                  disabled: timeSlot.disabled,
                  night: timeSlot.night
                }"
                @tap="!timeSlot.disabled && selectTime(timeSlot.value, false)"
              >
                <text class="time-text">{{ timeSlot.label }}</text>
                <text v-if="timeSlot.tag" class="time-tag">{{ timeSlot.tag }}</text>
              </view>
            </view>
          </view>
          
          <!-- 提示信息 -->
          <view class="notice-section">
            <text class="notice-text">
              <!-- 部分区域支持夜间上门，具体以快递员联系为准 -->
            </text>
          </view>
        </scroll-view>
      </view>
      
      <!-- 操作按钮 -->
 <!--     <view class="action-bar">
        <button 
          class="confirm-btn" 
          :class="{ active: selectedTime }"
          @tap="confirm"
        >
          {{ buttonText }}
        </button>
      </view> -->
    </view>
  </u-popup>
</template>

<script setup>
import { ref, computed, watch, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  visible: { type: Boolean, default: false },
  days: { type: Number, default: 3 },
  startTime: { type: Number, default: 9 },
  endTime: { type: Number, default: 20 },
  autoSelect: { type: Boolean, default: true }
})

const emit = defineEmits(['update:visible', 'close', 'confirm'])

// 当前时间（每分钟更新）
const currentTime = ref(new Date())
const selectedDate = ref('')
const selectedTime = ref('')
const selectedDay = ref('')
const selectedCurrentDayPosition = ref(0)
const selectedCurrentDay = ref('')
const loading = ref(false)
const isWithinOneHour = ref(false)

// ----- 精确时间计算 -----
const oneHourLater = computed(() => {
  const now = currentTime.value
  return now.getHours() + now.getMinutes() / 60 + 1
})

const twentyMinutesLater = computed(() => {
  const now = new Date(currentTime.value)
  now.setMinutes(now.getMinutes() + 3)
  return now
})

// ----- 推荐时段（唯一 value 前缀，不与普通时段冲突）-----
const withinOneHourSlot = computed(() => {
  if (!dateList.value.length || !dateList.value[0].isToday) return null
  
  const later = twentyMinutesLater.value
  let startHour = later.getHours()
  let startMinute = Math.ceil(later.getMinutes() / 10) * 10

  if (startMinute >= 60) {
    startHour += 1
    startMinute = 0
  }
  if (startHour >= props.endTime) return null

  let endHour = startHour + 1
  let endMinute = startMinute
  if (endHour > props.endTime) {
    endHour = props.endTime
    endMinute = 0
  }
  const duration = (endHour - startHour) * 60 + (endMinute - startMinute)
  if (duration < 30) return null

  const startStr = formatTime(startHour, startMinute)
  const endStr = formatTime(endHour, endMinute)
  return {
    start: startHour + startMinute / 60,
    end: endHour + endMinute / 60,
    label: `${startStr}-${endStr}`,
    value: `immediate:${startStr}-${endStr}`, // ✅ 唯一标识
    startHour, startMinute, endHour, endMinute
  }
})

// ----- 日期列表 -----
const todayDate = computed(() => formatDate(new Date()))
const dateList = computed(() => {
  const days = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  const list = []
  const today = new Date()
  for (let i = 0; i < props.days; i++) {
    const date = new Date(today)
    date.setDate(date.getDate() + i)
    const month = date.getMonth() + 1
    const day = date.getDate()
    const week = days[date.getDay()]
    const dateStr = formatDate(date)
    list.push({
      value: dateStr,
      label: i === 0 ? '今天' : i === 1 ? '明天' : '后天',
      week, month, day,
      isToday: i === 0,
      displayDate: dateStr
    })
  }
  return list
})

// ----- 基础整点时段 -----
const baseTimeSlots = computed(() => {
  const slots = []
  for (let hour = props.startTime; hour < props.endTime; hour++) {
    slots.push({
      start: hour,
      end: hour + 1,
      label: `${padZero(hour)}:00-${padZero(hour + 1)}:00`,
      value: `${padZero(hour)}:00-${padZero(hour + 1)}:00`,
      startHour: hour,
      endHour: hour + 1
    })
  }
  return slots
})

// ----- 根据所选日期过滤可用时段（精确到小时+分钟）-----
const timeSlots = computed(() => {
  const slots = []
  const isToday = selectedDate.value === todayDate.value

  if (isToday) {
    const threshold = oneHourLater.value
    baseTimeSlots.value.forEach(slot => {
      if (slot.start >= threshold) {
        slots.push({
          ...slot,
          disabled: false,
          night: slot.start >= 22,
          tag: ''
        })
      }
    })
  } else {
    baseTimeSlots.value.forEach(slot => {
      slots.push({
        ...slot,
        disabled: false,
        night: slot.start >= 22,
        tag: ''
      })
    })
  }
  return slots
})

// ----- 按钮文字 -----
const buttonText = computed(() => selectedTime.value ? '确定' : '请选择上门时间')

// ----- 核心：切换日期（自动根据 autoSelect 选中第一个可用时段）-----
const selectDate = (date,index) => {
  selectedDate.value = date
  selectedCurrentDayPosition.value = index
  selectedCurrentDay.value = dateList.value[index].label
  
  // selectedTime.value = ''
  // isWithinOneHour.value = false

  // 若开启自动选择，立即为该日期选择一个合适的时间
  if (props.autoSelect) {
    // 1. 如果是今天且有推荐时段，优先选中推荐
    if (selectedTime.value == '' && !isWithinOneHour.value) {
      selectedTime.value = withinOneHourSlot.value.value
      isWithinOneHour.value = true
    } else {
	  isWithinOneHour.value = false
      // // 2. 否则选中该日期下第一个可用普通时段
      // const firstSlot = timeSlots.value[0]
      // if (firstSlot) {
      //   selectedTime.value = firstSlot.value
      //   isWithinOneHour.value = false
      // }
    }
  }
}

// ----- 选择时间（普通时段 / 推荐时段）-----
const selectTime = (time, isImmediate = false) => {
  selectedTime.value = time
  isWithinOneHour.value = isImmediate
  selectedDay.value = selectedCurrentDay.value
  confirm()
}

// ----- 初始化弹窗（打开时）-----
const initSelection = () => {
  if (dateList.value.length) {
    // 直接调用 selectDate，复用自动选择逻辑
    selectDate(dateList.value[selectedCurrentDayPosition.value].value,selectedCurrentDayPosition.value)	
	selectedDay.value = selectedCurrentDay.value
  }
}

// ----- 确认选择 -----
const confirm = () => {
  if (!selectedTime.value) return

  const selectedDateObj = dateList.value.find(d => d.value === selectedDate.value)
  let selectedTimeSlot

  if (isWithinOneHour.value) {
    selectedTimeSlot = withinOneHourSlot.value
  } else {
    selectedTimeSlot = baseTimeSlots.value.find(s => s.value === selectedTime.value)
  }

  if (!selectedDateObj || !selectedTimeSlot) return

  const startHour = isWithinOneHour.value
    ? withinOneHourSlot.value.start
    : selectedTimeSlot.start
  const endHour = isWithinOneHour.value
    ? withinOneHourSlot.value.end
    : selectedTimeSlot.end

  const startTimeStr = formatDateTime(selectedDate.value, startHour)
  const endTimeStr = formatDateTime(selectedDate.value, endHour)

  const displayText = isWithinOneHour.value
    ? `${selectedDateObj.label} 一小时内`
    : `${selectedDateObj.label} ${selectedTime.value}`

  emit('confirm', {
    date: selectedDate.value,
    time: selectedTime.value,
    dateLabel: selectedDateObj.label,
    timeLabel: selectedTime.value,
    fullLabel: displayText,
    displayText,
    startTime: startTimeStr,
    endTime: endTimeStr,
    isToday: selectedDateObj.isToday,
    isImmediate: isWithinOneHour.value,
    isNight: selectedTimeSlot.start >= 18,
    startHour,
    endHour
  })
  close()
}

// ----- 关闭弹窗 -----
const close = () => {
  emit('close')
  emit('update:visible', false)
}

// ----- 工具函数 -----
const formatDate = (date) => {
  const y = date.getFullYear()
  const m = String(date.getMonth() + 1).padStart(2, '0')
  const d = String(date.getDate()).padStart(2, '0')
  return `${y}-${m}-${d}`
}
const formatTime = (hour, minute) => {
  return `${String(hour).padStart(2, '0')}:${String(minute).padStart(2, '0')}`
}
const padZero = (num) => String(num).padStart(2, '0')
const formatDateTime = (dateStr, hourDecimal) => {
  const date = new Date(dateStr)
  const wholeHour = Math.floor(hourDecimal)
  const minutes = Math.round((hourDecimal - wholeHour) * 60)
  date.setHours(wholeHour, minutes, 0, 0)
  const y = date.getFullYear()
  const m = String(date.getMonth() + 1).padStart(2, '0')
  const d = String(date.getDate()).padStart(2, '0')
  const hh = String(date.getHours()).padStart(2, '0')
  const mm = String(date.getMinutes()).padStart(2, '0')
  return `${y}-${m}-${d} ${hh}:${mm}:00`
}

// ----- 监听弹窗显示，初始化 -----
watch(() => props.visible, (newVal) => {
  if (newVal) {
    currentTime.value = new Date()
    initSelection()
  }
})

// ----- 每分钟刷新当前时间（仅弹窗可见时）-----
let timer = null
onMounted(() => {
  timer = setInterval(() => {
    if (props.visible) {
      currentTime.value = new Date()
    }
  }, 60000)
})
onUnmounted(() => {
  if (timer) clearInterval(timer)
})
</script>

<style scoped lang="less">
.time-picker-container {
  height: 80vh;
  display: flex;
  flex-direction: column;
}
.picker-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 32rpx 32rpx 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
  flex-shrink: 0;
}
.picker-title {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
}
.close-btn {
  font-size: 50rpx;
  color: #999;
  line-height: 40rpx;
  padding: 10rpx;
}
.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}
.date-sidebar {
  width: 220rpx;
  background-color: #f8f8f8;
  border-right: 1rpx solid #f0f0f0;
}
.date-item {
  padding: 32rpx 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}
.date-item.active {
  background-color: #fff;
  position: relative;
}
.date-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 6rpx;
  height: 40rpx;
  background-color: #1B64F0;
  border-radius: 0 4rpx 4rpx 0;
}
.date-item.today .date-name {
  // color: #1B64F0;
  color: #333;
}
.date-item.active .date-content {
  color: #1B64F0;
  font-weight: 600;
}
.date-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}
.date-name {
  font-size: 32rpx;
  color: #333;
  margin-bottom: 8rpx;
  &.active{  
	color: #1B64F0;
  }
}
.date-info {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 4rpx;
}
.date-week {
  font-size: 22rpx;
  color: #999;
}
.time-content {
  flex: 1;
  padding: 0 32rpx;
  overflow-y: auto;
}
.recommend-section {
  margin-top: 32rpx;
  padding-bottom: 24rpx;
  border-bottom: 1rpx solid #f0f0f0;
}
.recommend-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}
.recommend-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
}
/* 推荐时段：未选中时灰色边框，无背景 */
.recommend-time {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 28rpx 24rpx;
  border-radius: 16rpx;
  border: 2rpx solid #e8e8e8;
  transition: all 0.2s;
}
.recommend-time .time-value {
  font-size: 32rpx;
  font-weight: 500;
  color: #333;
}
/* 推荐时段：选中时蓝色背景 + 白色文字 */
.recommend-time.active {
  // background-color: #1B64F0;
  border-color: #1B64F0;
}
.recommend-time.active .time-value {
  // color: white;
}
.time-section {
  margin-top: 32rpx;
  margin-bottom: 32rpx;
}
.section-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  margin-bottom: 24rpx;
}
.time-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}
.time-item {
  padding: 28rpx 24rpx;
  border: 2rpx solid #e8e8e8;
  border-radius: 12rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: all 0.2s ease;
}
.time-item.active {
  border-color: #1B64F0;
  background-color: #fff;
}
.time-item.night {
  border-color: #4a5cff;
}
.time-item:not(.disabled):active {
  transform: scale(0.98);
  opacity: 0.8;
}
.time-item.disabled {
  opacity: 0.5;
}
.time-text {
  font-size: 28rpx;
  color: #333;
}
.time-item.active .time-text {
  color: #1B64F0;
  font-weight: 500;
}
.time-item.night .time-text {
  color: #4a5cff;
}
.time-item .time-tag {
  font-size: 20rpx;
  padding: 4rpx 12rpx;
  border-radius: 12rpx;
  background: #f5f5f5;
  color: #666;
}
.time-item.active .time-tag {
  background: #1B64F0;
  color: white;
}
.time-item.night .time-tag {
  background: #4a5cff;
  color: white;
}
.notice-section {
  padding: 24rpx;
  margin-bottom: 32rpx;
  background: #f8f8f8;
  border-radius: 12rpx;
}
.notice-text {
  font-size: 24rpx;
  color: #666;
  line-height: 1.4;
}
.action-bar {
  padding: 24rpx 32rpx;
  padding-top: 0;
  background: #fff;
  border-top: 1rpx solid #f0f0f0;
  flex-shrink: 0;
}
.confirm-btn {
  width: 100%;
  height: 88rpx;
  background: #f5f5f5;
  color: #999;
  border-radius: 44rpx;
  font-size: 32rpx;
  font-weight: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  transition: all 0.2s ease;
}
.confirm-btn.active {
  background: #1B64F0;
  color: white;
}
.confirm-btn.active:active {
  opacity: 0.9;
  transform: scale(0.99);
}
.loading {
  text-align: center;
  padding: 60rpx;
  font-size: 28rpx;
  color: #999;
}
</style>