<template>
  <div class="notice-card w-full">
    <div v-if="!detailOpen" class="notice-card__summary bg-FFFBEB rounded-24 w-full">
      <div class="notice-card__summary-title color-B45309 text-[16px] font-900">
        {{ summary.title }}
      </div>
      <div class="notice-card__summary-content color-D97706 text-[14px] font-900">
        {{ summary.content }}
      </div>
      <div class="notice-card__summary-footer flex flex-items-center flex-justify-between">
        <div class="notice-card__summary-time color-D97706 text-[12px] font-900">
          {{ summary.publishTime }}
        </div>
        <div class="notice-card__summary-link color-B45309 text-[14px] font-900" :class="{ 'is-disabled': disabled }"
          @click="openDetail">
          {{ summary.actionText }}
        </div>
      </div>
    </div>

    <div v-else class="notice-card__detail bg-white rounded-24 overflow-hidden w-full">
      <div class="notice-card__detail-head flex flex-items-center">
        <div class="notice-card__back flex flex-items-center flex-justify-center rounded-full flex-shrink-0"
          @click="closeDetail">
          <uni-icons type="left" size="16" color="#CBD5E1"></uni-icons>
        </div>
        <div class="notice-card__head-title color-1E293B text-[18px] font-900">
          {{ detail.navTitle }}
        </div>
      </div>

      <image class="notice-card__image block w-full" :src="detail.image" mode="aspectFill" />

      <div class="notice-card__detail-body">
        <div class="notice-card__detail-title color-1E293B font-size-20 font-900">
          {{ detail.title }}
        </div>
        <div class="notice-card__time-pill flex flex-items-center bg-F8FAFC color-475569 text-[13px] font-900">
          <span class="notice-card__time-icon">{{ detail.timeIcon }}</span>
          <span>{{ detail.publishTime }}</span>
        </div>
        <div class="notice-card__divider"></div>
        <div v-for="paragraph in paragraphs" :key="paragraph.id"
          class="notice-card__paragraph color-475569 text-[14px] font-800">
          <span v-for="segment in paragraph.segments" :key="segment.text"
            :class="{ 'notice-card__strong': segment.strong }">
            {{ segment.text }}
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";

const props = defineProps({
  data: {
    type: Object,
    default: () => ({}),
  },
  disabled: {
    type: Boolean,
    default: false,
  },
});

const emit = defineEmits(["select", "back", "action"]);

const detailOpen = ref(false);
const summary = computed(() => props.data.summary || {});
const detail = computed(() => props.data.detail || {});
const paragraphs = computed(() => detail.value.paragraphs || []);

const openDetail = () => {
  if (props.disabled) return;
  detailOpen.value = true;
  emit("select", props.data);
  emit("action", props.data);
};

const closeDetail = () => {
  detailOpen.value = false;
  emit("back");
};
</script>

<style scoped lang="scss">
@import "./styles/index.scss";
</style>
