<template>
  <BasicModal type="bottom" v-model:open="getOpen" :isMack="true">
    <view class="select-coupon-modal">
      <!-- 顶部标题栏 -->
      <view class="select-coupon-modal__header">
        <text class="header-title">优惠券</text>
        <view class="header-close" @click="handleClose">
          <image :src="getFileUrl('img/icon/close.png')" mode="aspectFill" class="close-icon"></image>
        </view>
      </view>

      <!-- 优惠券列表 -->
      <scroll-view class="select-coupon-modal__list" scroll-y>
        <view 
          v-for="(coupon, index) in couponList" 
          :key="coupon.id || index" 
          class="coupon-card"
          @click="handleSelect(coupon, index)"
        >
          <!-- 左侧金额信息 -->
          <view class="coupon-card__left">
            <text class="amount-text">{{ coupon.amount }}元</text>
            <text class="condition-text">满{{ coupon.minAmount }}可用</text>
          </view>

          <!-- 中间信息 -->
          <view class="coupon-card__center">
            <text class="name-text">{{ coupon.name }}</text>
            <text class="expire-text">{{ formatExpireDate(coupon.expireDate) }}到期</text>
          </view>

          <!-- 右侧复选框 -->
          <view class="coupon-card__right">
            <image :src="getFileUrl('img/icon/sele1.png')" mode="widthFix" class="selected-icon" v-show="isSelected(coupon)"></image>
            <image :src="getFileUrl('img/icon/sele2.png')" mode="widthFix" class="selected-icon" v-show="!isSelected(coupon)"></image>
          </view>
        </view>
      </scroll-view>
    </view>
  </BasicModal>
</template>

<script setup>
import { computed, ref } from 'vue';
import BasicModal from '@/components/Modal/BasicModal.vue';
import { getFileUrl } from '@/utils/file.js';

const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  couponList: {
    type: Array,
    default: () => []
  },
  selectedCouponId: {
    type: [Number, String],
    default: null
  }
});

const emit = defineEmits(['update:open', 'select', 'close']);

const getOpen = computed({
  get: () => props.open,
  set: (val) => emit('update:open', val)
});

// 格式化到期日期
const formatExpireDate = (date) => {
  if (!date) return '';
  // 如果是字符串格式的日期，转换为 YYYY/MM/DD 格式
  if (typeof date === 'string') {
    const d = new Date(date);
    if (!isNaN(d.getTime())) {
      const year = d.getFullYear();
      const month = String(d.getMonth() + 1).padStart(2, '0');
      const day = String(d.getDate()).padStart(2, '0');
      return `${year}/${month}/${day}`;
    }
    // 如果已经是 YYYY/MM/DD 格式，直接返回
    if (date.includes('/')) {
      return date;
    }
  }
  return date;
};

// 判断优惠券是否被选中
const isSelected = (coupon) => {
  return props.selectedCouponId !== null && coupon.id === props.selectedCouponId;
};

// 处理选择优惠券
const handleSelect = (coupon, index) => {
  // 如果已经选中，则取消选择；否则选中
  const newSelectedId = isSelected(coupon) ? null : coupon.id;
  emit('select', { coupon, index, selectedId: newSelectedId });
};

// 处理关闭
const handleClose = () => {
  getOpen.value = false;
  emit('close');
};
</script>

<style scoped lang="scss">
.select-coupon-modal {
  width: 100%;
  background: #F2F4F8;
  border-radius: 24rpx 24rpx 0 0;
  padding: 0;
  box-sizing: border-box;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;

  &__header {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 88rpx;
    background: #F2F4F8;

    .header-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #151515;
    }

    .header-close {
      position: absolute;
      right: 30rpx;
      top: 50%;
      transform: translateY(-50%);
      width: 60rpx;
      height: 60rpx;
      display: flex;
      align-items: center;
      justify-content: center;

      .close-icon {
        width: 26rpx;
        height: 26rpx;
      }
    }
  }

  &__list {
    flex: 1;
    padding: 24rpx 30rpx;
    padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
    box-sizing: border-box;
    overflow-y: auto;
    min-height: 0;
  }
}

.coupon-card {
  height: 191rpx;
  background: #FFFFFF;
//   border: 1rpx solid #E5E5E5;
  display: flex;
  align-items: center;
  border-radius: 24rpx;
  padding: 28rpx 24rpx;
  margin-bottom: 22rpx;
  box-sizing: border-box;
  transition: all 0.3s;

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

  &:active {
    opacity: 0.8;
  }

  &__left {
    display: flex;
    flex-direction: column;
    margin-right: 24rpx;
    min-width: 120rpx;
    text-align: center;

    .amount-text {
      font-size: 50rpx;
      font-weight: 600;
      color: #F47D1F;
      line-height: 1.2;
      margin-bottom: 8rpx;
    }

    .condition-text {
      font-size: 24rpx;
      color: #F47D1F;
      line-height: 1.2;
    }
  }

  &__center {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    margin-right: 20rpx;

    .name-text {
      font-size: 28rpx;
      color: #151515;
      font-weight: bold;
      line-height: 1.4;
      margin-bottom: 8rpx;
    }

    .expire-text {
      font-size: 22rpx;
      color: #666666;
      line-height: 1.4;
      margin-top: 20rpx;
    }
  }

  &__right {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 60rpx;

    .selected-icon {
      width: 38rpx;
      height: 38rpx;
    }

  }
}
</style>

