<template>
  <uni-popup ref="popup" type="bottom">
    <view class="pBoxUp">			
      <view class="textCenter f32 psTitle border-bottom">
        请选择配送时间
      </view>
      <view class="shopPopup bgff justify-between default-flex">
        <view class="f30 t666 lBox">
          <view v-for="(item, index) in jmhDay" @click="selectLDate(item)" :class="sLDate === item.formatDate ? 'ed' : ''" class="boxLDate" :key="index">
            {{ item.desc }}({{ item.name }})
          </view>
        </view>
        <view>
          <view v-for="(item, index) in timeList" @click="selectRDate(item)" :class="[sRightDate === item.time ? 'ed' : '', item.disabled ? 'disabled' : '']" :key="index" class="rBox">
            {{ item.time }}{{ item.disabled ? ' (超出配送时间)' : '' }}
          </view>
          <view class="empty f28 text-center" style="color: #999;margin-top: 40rpx;" v-if="timeList.length < 1">
            今天没有时间了,看看明天吧
          </view>	
        </view>
      </view>
      <view class="cancelButton" @click="close()">
        取消
      </view>	
    </view>	 
  </uni-popup>
</template>

<script>
import { getDate } from './utils.js';

export default {
  name: "distributionBox",
  props: {
    getTime: String // 接收父组件传来的初始值
  },
  data() {
    return {
      sendTime: '',
      sRightDate: '',
      sLDate: '',
      jmhDay: [],
      timeList: [
        { disabled: false, time: '10:00-12:00', id: 1 },
        { disabled: false, time: '12:00-14:00', id: 2 },
        { disabled: false, time: '14:00-16:00', id: 3 },
        { disabled: false, time: '16:00-18:00', id: 4 },
        { disabled: false, time: '18:00-20:00', id: 5 }
      ]
    };
  },
  mounted() {
    this.getNextDay();
  },
  methods: {
    close() {
      this.$refs.popup.close();
    },
    getWeekDate(day) {
      const weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
      return weeks[day];
    },
    getNextDay() {
      const day = this.getWeekDate(new Date().getDay());
      const isDay = getDate().fullDate;
      const nextDay = this.getWeekDate(new Date(new Date().getTime() + 24 * 60 * 60 * 1000).getDay());
      const nDate = getDate(new Date(new Date().getTime() + 24 * 60 * 60 * 1000)).fullDate;
      const HDay = this.getWeekDate(new Date(new Date().getTime() + 48 * 60 * 60 * 1000).getDay());
      const hDate = getDate(new Date(new Date().getTime() + 48 * 60 * 60 * 1000)).fullDate;
      const d1 = hDate.substr(hDate.indexOf('-') + 1);
      this.sLDate = isDay;
      this.selectLDate(1);
      this.jmhDay = [
        { name: day, id: 1, formatDate: isDay, desc: '今天' },
        { name: nextDay, id: 2, formatDate: nDate, desc: '明天' },
        { name: HDay, id: 3, formatDate: hDate, desc: d1.substr(0, d1.indexOf('-')) + '月' + d1.substr(d1.indexOf('-') + 1) + '日' }
      ];
    },
    showBox() {
      this.$refs.popup.open();
    },
    selectRDate(item) {
      if (item.disabled) {
        return;
      }
      this.sRightDate = item.time;
      this.sendTime = `${this.sLDate} ${item.time}`;
      this.$emit('update:getTime', this.sendTime); // 更新父组件的 sendTime
      this.$emit('timeSelected', this.sendTime); // 新增：通知父组件已选择的时间
      this.$refs.popup.close();
    },
    selectLDate(val) {
      if (val !== 1) {
        this.sLDate = val.formatDate;
      }
      let t = [
       { disabled: false, time: '10:00-12:00', id: 1 },
       { disabled: false, time: '12:00-14:00', id: 2 },
       { disabled: false, time: '14:00-16:00', id: 3 },
       { disabled: false, time: '16:00-18:00', id: 4 },
       { disabled: false, time: '18:00-20:00', id: 5 }
      ];
      if (val.desc === '今天' || val === 1) {
        const currentHour = new Date().getHours();
        if (currentHour <= 10) {
          this.timeList = t;
        } else if (currentHour <= 12) {
          t[0].disabled = true;
          this.timeList = t;
        } else if (currentHour <= 14) {
          t[0].disabled = true;
          t[1].disabled = true;
          this.timeList = t;
        } else if (currentHour <= 16) {
          t[0].disabled = true;
          t[1].disabled = true;
          t[2].disabled = true;
          this.timeList = t;
        } else if (currentHour <= 18) {
          t[0].disabled = true;
          t[1].disabled = true;
          t[2].disabled = true;
          t[3].disabled = true;
          this.timeList = t;
        } else if (currentHour <= 20) {
          t[0].disabled = true;
          t[1].disabled = true;
          t[2].disabled = true;
          t[3].disabled = true;
          t[4].disabled = true;
          this.timeList = t;
        }
      } else {
        this.timeList = t;
      }
      this.sRightDate = '';
    }
  }
};
</script>


<style lang="scss" scoped>
 .pBoxUp{
		background: #f5f5f5;
		.psTitle{
			line-height: 100rpx; 
			text-align: center;
			border-bottom: 1rpx solid #f7f7f7;
		}
		.shopPopup{
			backgound:#ffffff;
			font-size: 32rpx;
			display: flex;
			justify-content: space-between;
		}
		.boxLDate{
			font-size: 24rpx;
			height:80rpx;
			line-height: 80rpx;
			width:273rpx;
			text-align: center;
		}
		.lBox{
			color:#666666;
			background: #f5f5f5;
			font-size: 30rpx;
		}
		.boxLDate.ed{
			color:#1DA337;
			background: #fff;
		}
		.rBox.ed{
			color:#1DA337;
		}
		.rBox{
			width: 239px;
			padding-left: 20rpx;
			font-size: 24rpx;
			color: #333;
			height:80rpx;
			background: #fff;
			line-height: 80rpx;
		}
		.cancelButton{
			font-size: 36rpx;
			color: #999;
			text-align: center;
			height: 100rpx;
			line-height: 100rpx; 
			border-top: 1px solid #e5e5e5;
			background: #FFF;
		}
	}
</style>