<template>
  <div class="bill-list">
    <div v-if="loading" class="bill-list-loading">
      <div v-for="i in 3" :key="i" class="right-main-box skeleton">
        <div class="bill-cover skeleton-cover" />
        <div class="bill-content">
          <div class="header">
            <div class="skeleton-line w50" />
            <div class="skeleton-line w60" />
          </div>
          <div class="main">
            <div class="skeleton-line w30" />
            <div class="skeleton-line w40" />
            <div class="skeleton-line w50" />
            <div class="skeleton-line w35" />
          </div>
        </div>
      </div>
    </div>

    <div v-else-if="bills.length === 0" class="bill-list-empty">
      <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#bfbfbf" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
        <path d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
      </svg>
      <p>暂无相关法案</p>
    </div>

    <template v-else>
      <div class="right-main-box" v-for="item in mappedBills" :key="item.billId">
        <div v-if="item.riskSignal" class="risk-tag" :class="getRiskTagClass(item.riskSignal)">{{ item.riskSignal }}</div>
        <div class="bill-cover">
          <img class="bill-image" :src="item.imageUrl || defaultBillImage" alt="" @error="e => { e.target.src = defaultBillImage }" />
          <div class="bill-id" :title="item.billId">{{ item.billId || "-" }}</div>
        </div>
        <div class="bill-content">
          <div class="header">
            <div class="title" @click="handleBillMoreClick(item.billId)" :title="item.name">{{ item.name }}</div>
            <div class="en-title" :title="item.eName">{{ item.eName }}</div>
          </div>
          <div class="main">
            <div class="item"><div class="item-left">提案人：</div><div class="item-right">{{ item.tcr }}</div></div>
            <div class="item"><div class="item-left">委员会：</div><div class="item-right">{{ item.wyh }}</div></div>
            <div class="item"><div class="item-left">相关领域：</div><div class="item-right1"><AreaTag v-for="(val, idx) in item.areaList" :key="`${item.billId}-${val}-${idx}`" :tagName="val" /></div></div>
            <div class="item"><div class="item-left">最新动议：</div><div class="item-right"><CommonPrompt :content="item.zxdy" /></div></div>
            <div class="item">
              <div class="item-left">法案进展：</div>
              <div class="item-right2">
                <div class="tag" v-for="(val, idx) in getReversedProgress(item.progress)" :key="`${item.billId}-${val}-${idx}`" :style="{ zIndex: item.progress.length - idx }">{{ val }}</div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <div v-if="total > 0" class="right-footer">
        <div class="footer-left">{{ `共 ${total} 项` }}</div>
        <div class="footer-right">
          <el-pagination
            @current-change="$emit('page-change', $event)"
            :page-size="pageSize"
            :current-page="currentPage"
            background
            layout="prev, pager, next"
            :total="total"
          />
        </div>
      </div>
    </template>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from "vue-router"
import AreaTag from '@/components/base/AreaTag/index.vue'
import CommonPrompt from '@/views/bill/commonPrompt/index.vue'
import DocumentPreview from './DocumentPreview.vue'
import defaultBillImage from '@/views/bill/assets/images/image1.png'

const router = useRouter()

const props = defineProps({
  bills: { type: Array, required: true },
  loading: { type: Boolean, default: false },
  total: { type: Number, default: 0 },
  currentPage: { type: Number, default: 1 },
  pageSize: { type: Number, default: 10 },
  progressStages: { type: Array, default: () => [] },
})
defineEmits(['page-change'])

// 将 historyBill 接口数据映射为 right-main 模板需要的格式
const mappedBills = computed(() => {
  return props.bills.map(bill => ({
    billId: bill.billId,
    name: bill.name,
    eName: bill.ename,
    tcr: bill.sponsorList?.[0] || '',
    wyh: bill.congressName || '',
    areaList: bill.industryList?.map(i => i.industryName) || [],
    zxdy: bill.latestAction?.replace(/\bnull\b/gi, '至今').trim() || '',
    progress: bill.stageList?.map(s => s.name || s) || [],
    riskSignal: bill.riskSignal || '',
    imageUrl: bill.imageUrl || '',
  }))
})

const handleBillMoreClick = (billId) => {
  if (!billId) return
  const routeData = router.resolve({
    path: "/billLayout/bill/introduction",
    query: { billId: String(billId) }
  })
  window.open(routeData.href, "_blank")
}

const getRiskTagClass = (riskSignal) => {
  if (riskSignal?.includes('高')) return 'risk-tag-critical'
  if (riskSignal?.includes('中')) return 'risk-tag-high'
  if (riskSignal?.includes('低')) return 'risk-tag-medium'
  return 'risk-tag-medium'
}

const getReversedProgress = (progress) => Array.isArray(progress) ? [...progress].reverse() : []
</script>

<style scoped>
.bill-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.bill-list-loading {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.bill-list-empty {
  background: var(--bg-white-100);
  border-radius: 10px;
  box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
  padding: 48px;
  text-align: center;
  color: #bfbfbf;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

/* Skeleton */
.skeleton .skeleton-line {
  height: 16px;
  background: #f0f0f0;
  border-radius: 4px;
  animation: pulse 1.5s ease-in-out infinite;
}
.skeleton .skeleton-cover {
  background: #f0f0f0;
  animation: pulse 1.5s ease-in-out infinite;
}
.skeleton-line.w30 { width: 30%; }
.skeleton-line.w35 { width: 35%; }
.skeleton-line.w40 { width: 40%; }
.skeleton-line.w50 { width: 50%; }
.skeleton-line.w60 { width: 60%; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ============ right-main-box 样式（与 billHome ResourceLibrarySection 完全一致） ============ */
.right-main-box {
  position: relative;
  width: 100%;
  height: 320px;
  padding: 12px 16px;
  box-sizing: border-box;
  border-radius: 10px;
  box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
  background: var(--bg-white-100);
  margin-bottom: 16px;
  overflow: hidden;
  display: flex;
  gap: 16px;
  align-items: center;
}

.right-main-box .risk-tag {
  position: absolute;
  top: 12px;
  right: 16px;
  height: 28px;
  border-radius: 20px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  box-sizing: border-box;
  font-family: "Microsoft YaHei";
  font-size: 14px;
  font-weight: 500;
  line-height: 28px;
  white-space: nowrap;
}

.right-main-box .risk-tag::before {
  content: "";
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

.risk-tag-critical {
  background: var(--color-red-10);
  color: var(--color-red-100);
}

.risk-tag-high {
  background: var(--color-orange-10);
  color: var(--color-orange-100);
}

.risk-tag-medium {
  background: var(--color-yellow-10);
  color: var(--color-yellow-100);
}

.right-main-box .bill-cover {
  width: 240px;
  flex-shrink: 0;
  position: relative;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

.right-main-box .bill-cover::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 33.333%;
  background: linear-gradient(to bottom, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.92) 60%, var(--bg-white-100));
  pointer-events: none;
}

.right-main-box .bill-image {
  width: 240px;
  height: 100%;
  object-fit: none;
  display: block;
}

.right-main-box .bill-id {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 33.333%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  box-sizing: border-box;
  color: var(--text-primary-80-color);
  font-family: "Source Han Sans CN";
  font-size: 20px;
  font-weight: 700;
  line-height: 26px;
  letter-spacing: 0px;
  text-align: center;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 1;
}

.right-main-box .bill-content {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  box-sizing: border-box;
}

.right-main-box .header {
  width: 100%;
  border-bottom: 1px solid rgba(234, 236, 238, 1);
  padding-bottom: 14px;
  padding-right: 120px;
}

.right-main-box .header .title {
  cursor: pointer;
  height: 26px;
  color: var(--text-primary-80-color);
  font-family: Microsoft YaHei;
  font-size: 20px;
  font-weight: 700;
  line-height: 26px;
  letter-spacing: 0px;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.right-main-box .header .title:hover {
  color: #1459bb;
}

.right-main-box .header .en-title {
  margin-top: 8px;
  height: 24px;
  color: var(--text-primary-65-color);
  font-family: Microsoft YaHei;
  font-size: var(--font-size-base);
  font-weight: 400;
  line-height: 24px;
  letter-spacing: 0px;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.right-main-box .main {
  width: 100%;
  margin-top: 10px;
}

.right-main-box .main .item {
  margin-top: 12px;
  display: flex;
  align-items: flex-start;
}

.right-main-box .main .item .item-left {
  width: 88px;
  color: var(--text-primary-80-color);
  font-family: "Microsoft YaHei";
  font-size: var(--font-size-base);
  font-weight: 700;
  line-height: 24px;
  letter-spacing: 1px;
  text-align: left;
}

.right-main-box .main .item .item-right {
  max-width: 1000px;
  margin-left: 10px;
  color: var(--text-primary-65-color);
  font-family: "Microsoft YaHei";
  font-size: var(--font-size-base);
  font-weight: 400;
  line-height: 24px;
  letter-spacing: 0px;
  text-align: left;
}

.right-main-box .main .item .item-right1 {
  margin-left: 10px;
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.right-main-box .main .item .item-right1 .tag {
  height: 24px;
  line-height: 24px;
  padding: 0 8px;
  border-radius: 4px;
  background: var(--color-primary-10);
  color: var(--color-main-active);
  font-family: "Microsoft YaHei";
  font-size: 14px;
  font-weight: 400;
}

.right-main-box .main .item .item-right2 {
  margin-left: 10px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.right-main-box .main .item .item-right2 .tag {
  height: 24px;
  line-height: 22px;
  padding: 0 10px 0 30px;
  background: var(--bg-white-100);
  color: var(--text-primary-65-color);
  border-top: 1px solid rgb(234, 236, 238);
  border-bottom: 1px solid rgb(234, 236, 238);
  position: relative;
  margin-left: -10px;
  font-family: "Microsoft YaHei";
  font-size: 14px;
  font-weight: 400;
}

.right-main-box .main .item .item-right2 .tag::after {
  content: "";
  position: absolute;
  top: 50%;
  right: -8.485px;
  width: 16.97px;
  height: 16.97px;
  background: inherit;
  border-top: 1px solid rgb(234, 236, 238);
  border-right: 1px solid rgb(234, 236, 238);
  transform: translateY(-50%) rotate(45deg);
  z-index: 1;
  box-shadow: 2px -2px 2px rgba(0, 0, 0, 0.05);
  box-sizing: border-box;
}

.right-main-box .main .item .item-right2 .tag:first-child {
  margin-left: 0;
  padding-left: 10px;
  border-left: 1px solid rgb(234, 236, 238);
  border-radius: 4px 0 0 4px;
}

.right-main-box .main .item .item-right2 .tag:last-child {
  background: var(--text-primary-80-color);
  color: var(--bg-white-100);
  border-color: var(--text-primary-80-color);
  padding-right: 10px;
  border-radius: 0;
  border-right: none;
}

.right-main-box .main .item .item-right2 .tag:last-child::after {
  display: block;
  border-color: var(--text-primary-80-color);
  box-shadow: none;
}

.right-main-box .main .item .item-right2 .tag:first-child:last-child {
  margin-left: 0;
  padding: 0 10px;
  border-radius: 4px 0 0 4px;
  background: var(--text-primary-80-color);
  color: var(--bg-white-100);
  border: 1px solid var(--text-primary-80-color);
  border-right: none;
}

/* Footer */
.right-footer {
  height: 85px;
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  padding-top: 12px;
}

.right-footer .footer-left {
  color: var(--text-primary-80-color);
  font-family: "Microsoft YaHei";
  font-size: var(--font-size-base);
  font-weight: 400;
  line-height: 32px;
}

.right-footer :deep(.el-pagination) {
  --el-pagination-bg-color: var(--bg-white-100);
  --el-pagination-hover-color: rgba(5, 95, 194, 1);
  padding: 0;
}

.right-footer :deep(.el-pagination.is-background .btn-prev),
.right-footer :deep(.el-pagination.is-background .btn-next),
.right-footer :deep(.el-pagination.is-background .el-pager li) {
  min-width: 32px;
  height: 32px;
  line-height: 32px;
  border-radius: 4px;
  border: 1px solid rgba(234, 236, 238, 1);
  background: var(--bg-white-100);
  color: rgba(59, 65, 75, 1);
  font-size: 14px;
  font-family: 'Microsoft YaHei', sans-serif;
  margin: 0 3px;
}

.right-footer :deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background: var(--bg-white-100);
  color: rgba(5, 95, 194, 1);
  border-color: rgba(5, 95, 194, 1);
}

.right-footer :deep(.el-pagination.is-background .el-pager li:not(.is-disabled):hover) {
  color: rgba(5, 95, 194, 1);
}

.right-footer :deep(.el-pagination.is-background .btn-prev:hover),
.right-footer :deep(.el-pagination.is-background .btn-next:hover) {
  color: rgba(5, 95, 194, 1);
}
</style>
