<script setup>
import { computed } from "vue";
import { useNamespace } from "@yh-ui/hooks";
import { useConfig } from "@yh-ui/hooks";
import { useComponentTheme } from "@yh-ui/theme";
defineOptions({
  name: "YhCard"
});
const props = defineProps({
  header: { type: String, required: false },
  bodyStyle: { type: Object, required: false },
  headerStyle: { type: Object, required: false },
  shadow: { type: null, required: false, default: "always" },
  bordered: { type: Boolean, required: false, default: true },
  hoverable: { type: Boolean, required: false, default: false },
  size: { type: String, required: false, default: "default" },
  loading: { type: Boolean, required: false, default: false },
  bodyPadding: { type: Boolean, required: false, default: true },
  themeOverrides: { type: null, required: false }
});
const ns = useNamespace("card");
const { globalSize } = useConfig();
const { themeStyle } = useComponentTheme(
  "card",
  computed(() => props.themeOverrides)
);
const cardClasses = computed(() => [
  ns.b(),
  ns.m(props.shadow),
  ns.m(props.size || globalSize.value || "default"),
  ns.is("bordered", props.bordered),
  ns.is("hoverable", props.hoverable),
  ns.is("loading", props.loading)
]);
const headerClasses = computed(() => [ns.e("header")]);
const bodyClasses = computed(() => [
  ns.e("body"),
  !props.bodyPadding && ns.em("body", "no-padding")
]);
const footerClasses = computed(() => [ns.e("footer")]);
</script>

<template>
  <div :class="cardClasses" :style="themeStyle">
    <!-- 加载状态 -->
    <div v-if="loading" :class="ns.e('loading')">
      <div :class="ns.e('loading-content')">
        <div :class="ns.e('skeleton-header')"></div>
        <div :class="ns.e('skeleton-paragraph')">
          <div :class="ns.e('skeleton-line')"></div>
          <div :class="ns.e('skeleton-line')"></div>
          <div :class="ns.e('skeleton-line')"></div>
        </div>
      </div>
    </div>

    <!-- 正常内容 -->
    <template v-else>
      <!-- 卡片头部 -->
      <div
        v-if="$slots.header || header || $slots.extra"
        :class="headerClasses"
        :style="headerStyle"
      >
        <div :class="ns.e('header-wrapper')">
          <div :class="ns.e('header-title')">
            <slot name="header">{{ header }}</slot>
          </div>
          <div v-if="$slots.extra" :class="ns.e('header-extra')">
            <slot name="extra" />
          </div>
        </div>
      </div>

      <!-- 卡片主体 -->
      <div :class="bodyClasses" :style="bodyStyle">
        <slot />
      </div>

      <!-- 卡片底部 -->
      <div v-if="$slots.footer" :class="footerClasses">
        <slot name="footer" />
      </div>
    </template>
  </div>
</template>

<style>
@charset "UTF-8";
/**
 * YH-UI Sass Variables
 * 原具体的 CSS 变量输出规则 (:root, html.dark 等) 已移至 root.scss，以防止各组件样式编译时产生重复的全局变量 CSS 冗余。
 */
.yh-card {
  --yh-card-bg-color: var(--yh-bg-color-overlay, #ffffff);
  --yh-card-border-color: var(--yh-border-color-light, #ebeef5);
  --yh-card-border-radius: var(--yh-border-radius-base, 4px);
  --yh-card-shadow: var(--yh-box-shadow-light, 0 2px 12px 0 rgba(0, 0, 0, 0.1));
  --yh-card-header-padding: 18px 20px;
  --yh-card-body-padding: 20px;
  --yh-card-footer-padding: 18px 20px;
  position: relative;
  background-color: var(--yh-card-bg-color);
  border-radius: var(--yh-card-border-radius);
  transition: var(--yh-transition-base);
  overflow: hidden;
  color: var(--yh-text-color-primary);
}
.yh-card.is-bordered {
  border: 1px solid var(--yh-card-border-color);
}

.yh-card--always {
  box-shadow: var(--yh-card-shadow);
}

.yh-card--hover:hover {
  box-shadow: var(--yh-card-shadow);
}

.yh-card--never {
  box-shadow: none;
}

.yh-card.is-hoverable {
  cursor: pointer;
  transition: all var(--yh-duration-base) var(--yh-timing-ease-in-out);
}
.yh-card.is-hoverable:hover {
  transform: translateY(-4px);
  box-shadow: var(--yh-shadow-lg);
}

.yh-card--small {
  font-size: var(--yh-font-size-sm);
  --yh-card-header-padding: 12px 16px;
  --yh-card-body-padding: 16px;
}

.yh-card--default {
  font-size: var(--yh-font-size-base);
}

.yh-card--large {
  font-size: var(--yh-font-size-md);
  --yh-card-header-padding: 24px 30px;
  --yh-card-body-padding: 30px;
}

.yh-card.is-loading {
  pointer-events: none;
}

.yh-card__header {
  padding: var(--yh-card-header-padding);
  border-bottom: 1px solid var(--yh-card-border-color);
  box-sizing: border-box;
}

.yh-card__header-wrapper {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.yh-card__header-title {
  flex: 1;
  font-size: var(--yh-font-size-md);
  font-weight: var(--yh-font-weight-semibold);
  color: var(--yh-text-color-primary);
}

.yh-card__header-extra {
  margin-left: 16px;
  color: var(--yh-text-color-regular);
}

.yh-card__body {
  padding: var(--yh-card-body-padding);
  box-sizing: border-box;
}
.yh-card__body--no-padding {
  padding: 0;
}

.yh-card__footer {
  padding: var(--yh-card-footer-padding);
  border-top: 1px solid var(--yh-card-border-color);
  box-sizing: border-box;
}

.yh-card__loading {
  padding: var(--yh-card-body-padding);
}

.yh-card__loading-content {
  animation: yh-card-loading 1.5s ease infinite;
}

.yh-card__skeleton-header {
  height: 20px;
  width: 40%;
  background: linear-gradient(90deg, var(--yh-fill-color) 25%, var(--yh-fill-color-light) 37%, var(--yh-fill-color) 63%);
  background-size: 400% 100%;
  animation: yh-skeleton-loading 1.4s ease infinite;
  border-radius: var(--yh-border-radius-small);
  margin-bottom: 16px;
}

.yh-card__skeleton-paragraph {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.yh-card__skeleton-line {
  height: 16px;
  background: linear-gradient(90deg, var(--yh-fill-color) 25%, var(--yh-fill-color-light) 37%, var(--yh-fill-color) 63%);
  background-size: 400% 100%;
  animation: yh-skeleton-loading 1.4s ease infinite;
  border-radius: var(--yh-border-radius-small);
}
.yh-card__skeleton-line:nth-child(1) {
  width: 100%;
}
.yh-card__skeleton-line:nth-child(2) {
  width: 80%;
}
.yh-card__skeleton-line:nth-child(3) {
  width: 60%;
}

@keyframes yh-skeleton-loading {
  0% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0 50%;
  }
}
@keyframes yh-card-loading {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.6;
  }
}
</style>
