<script setup>
import { computed, toRefs, ref } from "vue";
import { useNamespace, useLocale } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
import { couponCardProps, couponCardEmits } from "./coupon-card";
import { YhButton } from "../../button";
import { YhCheckbox } from "../../checkbox";
defineOptions({ name: "YhCouponCard" });
const props = defineProps(couponCardProps);
const emit = defineEmits(couponCardEmits);
const {
  title,
  description,
  amount,
  symbol,
  threshold,
  validPeriod,
  status,
  actionText,
  selectable,
  selected,
  variant,
  badge,
  badgeType,
  percent,
  percentText,
  rules,
  ruleTitle
} = toRefs(props);
const rulesExpanded = ref(false);
const ns = useNamespace("coupon-card");
const { t } = useLocale();
const { themeStyle } = useComponentTheme(
  "coupon-card",
  computed(() => props.themeOverrides)
);
const isAvailable = computed(() => status.value === "available");
const isUsed = computed(() => status.value === "used");
const isExpired = computed(() => status.value === "expired");
const thresholdText = computed(() => {
  const val = Number(threshold.value);
  if (!isNaN(val) && val > 0) {
    return t("couponcard.limit", { threshold: val });
  }
  if (typeof threshold.value === "string" && threshold.value) {
    return threshold.value;
  }
  return t("couponcard.noThreshold");
});
const statusSealText = computed(() => {
  if (isUsed.value) return t("couponcard.used");
  if (isExpired.value) return t("couponcard.expired");
  return "";
});
const finalActionText = computed(() => {
  if (actionText.value) return actionText.value;
  if (isUsed.value) return t("couponcard.used");
  if (isExpired.value) return t("couponcard.expired");
  return t("couponcard.available");
});
const finalRuleTitle = computed(() => {
  if (ruleTitle.value) return ruleTitle.value;
  return t("couponcard.ruleTitle");
});
const handleActionClick = (e) => {
  if (!isAvailable.value) return;
  e.stopPropagation();
  emit("action", e);
};
const handleCardClick = (e) => {
  emit("click", e);
};
const handleCheckChange = (val) => {
  emit("update:selected", !!val);
};
</script>

<template>
  <div
    :class="[ns.b(), ns.is(status), ns.m(variant), ns.is('selectable', selectable), ns.is('selected', selected)]"
    :style="themeStyle"
    @click="handleCardClick"
  >
    <!-- 主体内容区域包裹 -->
    <div :class="ns.e('content')">
      <!-- 左侧金额区域 -->
      <div :class="ns.e('amount-side')">
        <div :class="ns.e('amount')">
          <span :class="ns.e('symbol')">{{ symbol }}</span>
          <span>{{ amount }}</span>
        </div>
        <div :class="ns.e('threshold')">{{ thresholdText }}</div>
      </div>

      <!-- 右侧内容区域 -->
      <div :class="ns.e('main-side')">
        <div :class="ns.e('info')">
          <div :class="ns.e('title-row')">
            <slot name="title">
              <div :class="ns.e('title')">{{ title }}</div>
            </slot>
            <div v-if="selectable" :class="ns.e('checkbox')" @click.stop>
              <YhCheckbox :model-value="selected" @update:model-value="handleCheckChange" />
            </div>
          </div>
          <slot name="description">
            <div v-if="description" :class="ns.e('description')">{{ description }}</div>
          </slot>

          <!-- 疯抢进度条 -->
          <div v-if="percent !== void 0" :class="ns.e('progress')">
            <div :class="ns.e('progress-bar')">
              <div :class="ns.e('progress-inner')" :style="{
  width: percent + '%'
}"></div>
            </div>
            <div v-if="percentText" :class="ns.e('progress-text')">{{ percentText }}</div>
          </div>
        </div>

        <div :class="ns.e('footer')">
          <div :class="ns.e('valid-period')">{{ validPeriod }}</div>
          <div :class="ns.e('action-area')" @click.stop>
            <slot name="action">
              <YhButton
                v-if="finalActionText"
                type="primary"
                size="small"
                :disabled="!isAvailable"
                :class="ns.e('action-btn')"
                @click="handleActionClick"
              >
                {{ finalActionText }}
              </YhButton>
            </slot>
          </div>
        </div>
      </div>

      <!-- 浮动角标 -->
      <slot name="badge">
        <div v-if="badge" :class="[ns.e('badge'), ns.em('badge', badgeType)]">
          {{ badge }}
        </div>
      </slot>

      <!-- 状态水印 -->
      <slot v-if="statusSealText" name="seal" :status="status">
        <div :class="[ns.e('seal'), ns.is(status)]">
          {{ statusSealText }}
        </div>
      </slot>
    </div>

    <!-- 底部使用规则展开区 -->
    <div v-if="rules || $slots.rules" :class="[ns.e('rules'), ns.is('expanded', rulesExpanded)]">
      <div :class="ns.e('rules-trigger')" @click.stop="rulesExpanded = !rulesExpanded">
        <span>{{ finalRuleTitle }}</span>
        <svg
          :class="[ns.e('rules-icon'), ns.is('expanded', rulesExpanded)]"
          viewBox="0 0 1024 1024"
          width="12"
          height="12"
        >
          <path
            d="M512 704c-12.8 0-25.6-5.12-35.84-15.36L155.52 368c-19.84-19.84-19.84-51.84 0-71.68s51.84-19.84 71.68 0L512 600.96l284.8-284.8c19.84-19.84 51.84-19.84 71.68 0s19.84 51.84 0 71.68L547.84 688.64C537.6 698.88 524.8 704 512 704z"
            fill="currentColor"
          />
        </svg>
      </div>
      <!-- 折叠面板内容 -->
      <div v-show="rulesExpanded" :class="ns.e('rules-content')">
        <slot name="rules">
          <div style="white-space: pre-wrap">{{ rules }}</div>
        </slot>
      </div>
    </div>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-coupon-card {
  --yh-coupon-primary-color: var(--yh-color-primary);
  --yh-coupon-gray-color: var(--yh-text-color-placeholder);
  --yh-coupon-bg: var(--yh-bg-color-overlay);
  --yh-coupon-border-radius: 12px;
  --yh-coupon-amount-size: 28px;
  --yh-coupon-title-size: 16px;
  --yh-coupon-desc-size: 12px;
  position: relative;
  width: 100%;
  min-height: 100px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  background-color: var(--yh-coupon-bg);
  border: 1px solid var(--yh-border-color-light);
  border-radius: var(--yh-coupon-border-radius);
  overflow: hidden;
  box-shadow: var(--yh-shadow-light);
  cursor: pointer;
  transition: transform var(--yh-transition-base), box-shadow var(--yh-transition-base);
}
.yh-coupon-card * {
  box-sizing: border-box;
}
.yh-coupon-card__content {
  display: flex;
  width: 100%;
  height: 100px;
  flex-shrink: 0;
  position: relative;
  overflow: hidden;
}

.yh-coupon-card.is-selectable:hover {
  box-shadow: var(--yh-shadow-base);
}

.yh-coupon-card {
  /* Side Notch Decoration */
}
.yh-coupon-card::before, .yh-coupon-card::after {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  background-color: var(--yh-bg-color);
  border-radius: 50%;
  z-index: 10;
}
.yh-coupon-card--indent::before {
  top: 50%;
  left: 0;
  transform: translate(-50%, -50%);
}
.yh-coupon-card--indent::after {
  top: 50%;
  right: 0;
  transform: translate(50%, -50%);
}

.yh-coupon-card--circle::before, .yh-coupon-card--circle::after {
  display: block;
}

.yh-coupon-card--scallop::before, .yh-coupon-card--scallop::after {
  display: none;
}
.yh-coupon-card--scallop {
  mask: radial-gradient(circle at 0 20px, transparent 8px, black 9px) 0 0/100% 20px repeat-y;
  -webkit-mask: radial-gradient(circle at 0 20px, transparent 8px, black 9px) 0 0/100% 20px repeat-y;
}

.yh-coupon-card__amount-side {
  width: 30%;
  height: 100%;
  min-height: 100px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: var(--yh-coupon-primary-color);
  color: white;
  position: relative;
  padding: 0 8px;
}
.yh-coupon-card__amount-side::after {
  content: "";
  position: absolute;
  top: 10px;
  bottom: 10px;
  right: -1px;
  border-right: 2px dashed rgba(255, 255, 255, 0.4);
}

.yh-coupon-card__amount {
  font-size: var(--yh-coupon-amount-size);
  font-weight: var(--yh-font-weight-bold);
  line-height: 1;
  display: flex;
  align-items: baseline;
}
.yh-coupon-card__symbol {
  font-size: 14px;
  margin-right: 2px;
  font-weight: var(--yh-font-weight-normal);
}

.yh-coupon-card__threshold {
  font-size: 10px;
  margin-top: 4px;
  opacity: 0.9;
}

.yh-coupon-card__main-side {
  flex: 1;
  height: 100%;
  min-height: 100px;
  display: flex;
  padding: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
}

.yh-coupon-card__info {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  flex-shrink: 0;
}

.yh-coupon-card__title {
  margin: 0;
  font-size: var(--yh-coupon-title-size);
  color: var(--yh-text-color-primary);
  font-weight: var(--yh-font-weight-medium);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.yh-coupon-card__description {
  margin: 0;
  font-size: var(--yh-coupon-desc-size);
  color: var(--yh-text-color-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

.yh-coupon-card__footer {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-shrink: 0;
  margin-top: auto;
}

.yh-coupon-card__valid-period {
  font-size: 10px;
  color: var(--yh-text-color-placeholder);
  white-space: nowrap;
}

.yh-coupon-card__action-area {
  z-index: 5;
}

.yh-coupon-card__action-btn {
  padding: 2px 8px;
  border-radius: var(--yh-radius-round);
  background-color: var(--yh-coupon-primary-color);
  color: white;
  font-size: 10px;
  cursor: pointer;
  transition: opacity var(--yh-transition-base);
}
.yh-coupon-card__action-btn:hover {
  opacity: 0.8;
}

.yh-coupon-card {
  /* Status Styles */
}
.yh-coupon-card.is-used .yh-coupon-card__amount-side, .yh-coupon-card.is-expired .yh-coupon-card__amount-side {
  background-color: var(--yh-coupon-gray-color);
}

.yh-coupon-card.is-used .yh-coupon-card__action-btn, .yh-coupon-card.is-expired .yh-coupon-card__action-btn {
  background-color: var(--yh-fill-color-darker);
  color: var(--yh-text-color-placeholder);
  cursor: not-allowed;
}

.yh-coupon-card.is-used .yh-coupon-card__title, .yh-coupon-card.is-expired .yh-coupon-card__title {
  color: var(--yh-text-color-placeholder);
}

.yh-coupon-card__seal {
  position: absolute;
  top: -10px;
  right: -10px;
  width: 60px;
  height: 60px;
  border: 2px solid var(--yh-border-color);
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: rotate(15deg);
  opacity: 0.3;
  pointer-events: none;
  font-weight: bold;
  font-size: 12px;
  z-index: 20;
}
.yh-coupon-card__seal.is-used {
  border-color: var(--yh-color-success);
  color: var(--yh-color-success);
}

.yh-coupon-card__seal.is-expired {
  border-color: var(--yh-color-danger);
  color: var(--yh-color-danger);
}

.yh-coupon-card__checkbox {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 25;
}

.yh-coupon-card__badge {
  position: absolute;
  top: 6px;
  left: -24px;
  background-color: var(--yh-color-danger);
  color: white;
  font-size: 10px;
  width: 90px;
  padding: 2px 0;
  transform: rotate(-45deg);
  font-weight: var(--yh-font-weight-medium);
  box-shadow: var(--yh-shadow-base);
  z-index: 20;
  text-align: center;
  line-height: 1;
}
.yh-coupon-card__badge--primary {
  background-color: var(--yh-color-primary);
}

.yh-coupon-card__badge--warning {
  background-color: var(--yh-color-warning);
}

.yh-coupon-card__badge--success {
  background-color: var(--yh-color-success);
}

.yh-coupon-card__badge--danger {
  background-color: var(--yh-color-danger);
}

.yh-coupon-card__progress {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  width: 100%;
  max-width: 160px;
}

.yh-coupon-card__progress-bar {
  flex: 1;
  height: 4px;
  background-color: var(--yh-fill-color-light);
  border-radius: 2px;
  overflow: hidden;
}

.yh-coupon-card__progress-inner {
  height: 100%;
  background: linear-gradient(90deg, var(--yh-color-danger-light-3), var(--yh-color-danger));
  border-radius: 2px;
  transition: width 0.3s ease;
}

.yh-coupon-card__progress-text {
  font-size: 10px;
  color: var(--yh-color-danger);
  white-space: nowrap;
  line-height: 1;
}

.yh-coupon-card__rules {
  background-color: var(--yh-bg-color-overlay);
  border-top: 1px dashed var(--yh-border-color-light);
  font-size: 10px;
  color: var(--yh-text-color-secondary);
  width: 100%;
  border-bottom-left-radius: var(--yh-coupon-border-radius);
  border-bottom-right-radius: var(--yh-coupon-border-radius);
}

.yh-coupon-card__rules-trigger {
  padding: 8px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  cursor: pointer;
  user-select: none;
  transition: background-color var(--yh-transition-base);
}
.yh-coupon-card__rules-trigger:hover {
  background-color: var(--yh-fill-color-light);
}

.yh-coupon-card__rules-icon {
  transition: transform var(--yh-transition-base);
}
.yh-coupon-card__rules-icon.is-expanded {
  transform: rotate(180deg);
}

.yh-coupon-card__rules-content {
  padding: 0 12px 12px;
  line-height: 1.5;
}
</style>
