<template>
  <view class="page-container">
    <view class="merchant-list">
      <view
        class="merchant-box"
        v-for="(item, index) in merchantList"
        :key="index"
      >
        <view class="merchant-item" @click="goToMenu(item)">
          <view class="merchant-info">
            <image
              :src="item.avatar"
              class="merchant-avatar"
              mode="aspectFill"
            ></image>
            <view class="merchant-detail">
              <view class="merchant-name">{{ item.name }}</view>
              <view class="merchant-stats">
                <text>月销 {{ item.monthlySales }}</text>
                <text class="business-hours">营业时间 {{ item.businessHours }}</text>
              </view>
            </view>
          </view>
          <view class="status-box" :class="{ 'status-open': item.isOpen }">
            {{ item.isOpen ? '营业中' : '休息中' }}
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      merchantList: [
        {
          id: 1,
          name: '宏雷大厨外卖',
          avatar: '/static/logo.png',
          monthlySales: 1984,
          businessHours: '00:00-23:59',
          isOpen: true
        },
        {
          id: 2,
          name: '年货预订',
          avatar: '/static/logo.png',
          monthlySales: 2,
          businessHours: '00:00-14:59',
          isOpen: false
        }
      ]
    }
  },
  methods: {
    updateMerchantStatus() {
      this.merchantList.forEach(merchant => {
        merchant.isOpen = this.checkIfOpen(merchant.businessHours);
      });
    },
    checkIfOpen(businessHours) {
      // 这是一个简单的判断逻辑
      const [start, end] = businessHours.split('-');
      const now = new Date();
      const currentTime = now.getHours() * 60 + now.getMinutes();
      const [startHour, startMinute] = start.split(':').map(Number);
      const [endHour, endMinute] = end.split(':').map(Number);
      const startTime = startHour * 60 + startMinute;
      const endTime = endHour * 60 + endMinute;

      return currentTime >= startTime && currentTime <= endTime;
    },
    goToMenu(item) {
      uni.navigateTo({
        url: '/pages/advanceOrder/index'
      })
    }
  },

  mounted() {
    this.updateMerchantStatus();
    // 可以设置一个定时器，定期更新营业状态
    setInterval(this.updateMerchantStatus, 60000); // 每分钟更新一次
  }
}
</script>

<style lang="scss" scoped>
.page-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 40rpx;
}

.merchant-list {
  .merchant-box {
    margin-bottom: 20rpx;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .merchant-item {
    background-color: #ffffff;
    border-radius: 12rpx;
    padding: 24rpx;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    position: relative;
  }

  .merchant-info {
    display: flex;
    align-items: center;
    flex: 1;
    margin-right: 20rpx;

    .merchant-avatar {
      width: 88rpx;
      height: 88rpx;
      border-radius: 8rpx;
      margin-right: 20rpx;
    }

    .merchant-detail {
      flex: 1;

      .merchant-name {
        font-size: 28rpx;
        color: #333333;
        margin-bottom: 16rpx;
        font-weight: 500;
      }

      .merchant-stats {
        display: flex;
        font-size: 12px;
        color: #999999;
        flex-direction: column;
        align-items: flex-start;

        .business-hours {
          //margin-left: 24rpx;
        }
      }
    }
  }

  .status-box {
    font-size: 24rpx;
    color: #999999;
    padding: 4rpx 12rpx;
    background-color: #f5f5f5;
    border-radius: 4rpx;
    position: absolute;
    top: 24rpx;
    right: 24rpx;

    &.status-open {
      color: #ff6633;
      background-color: #fff2ef;
    }
  }
}
</style>