<template>
  <div class="home-main-footer-main">
    <div class="left">
      <div class="select-box">
        <div class="header">
          <div class="icon"></div>
          <div class="title">{{ "科技领域" }}</div>
        </div>
        <div class="select-main">
          <el-checkbox-group class="checkbox-group" :model-value="selectedAreaList" @change="handleAreaGroupChange">
            <el-checkbox class="filter-checkbox all-checkbox" :label="RESOURCE_FILTER_ALL_AREA">
              {{ RESOURCE_FILTER_ALL_AREA }}
            </el-checkbox>
            <el-checkbox v-for="research in areaList" :key="research.id" class="filter-checkbox" :label="research.id">
              {{ research.name }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
      </div>

      <div class="select-box">
        <div class="header">
          <div class="icon"></div>
          <div class="title">{{ "发布时间" }}</div>
        </div>
        <div class="select-main">
          <el-checkbox-group class="checkbox-group" :model-value="selectedPubTimeList" @change="handleTimeGroupChange">
            <el-checkbox class="filter-checkbox all-checkbox" :label="RESOURCE_FILTER_ALL_TIME">
              {{ RESOURCE_FILTER_ALL_TIME }}
            </el-checkbox>
            <el-checkbox v-for="time in pubTimeList" :key="time.id" class="filter-checkbox" :label="time.id">
              {{ time.name }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
      </div>
    </div>

    <div class="right" v-loading="loading">
      <div class="card-box">
        <template v-if="hasData">
          <div class="footer-card" v-for="(item, index) in curFooterList" :key="index"
            @click="emit('report-click', item)">
            <div class="footer-card-top">
              <img :src="item.imageUrl" alt="" />
            </div>
            <div class="footer-card-title">
              {{ item.name }}
            </div>
            <div class="footer-card-footer">
              <div class="time">{{ formatDate(item.times) }}</div>
              <div class="from">{{ item.thinkTankName }}</div>
            </div>
          </div>
        </template>
        <template v-else-if="!loading">
          <div class="right-empty">
            <el-empty class="right-el-empty" description="暂无数据" :image-size="100" />
          </div>
        </template>
      </div>
      <div class="right-footer">
        <div class="info">共 {{ total }} 篇智库报告</div>
        <div class="page-box">
          <el-pagination :page-size="12" :page-count="pageCount" background layout="prev, pager, next"
            @current-change="emit('page-change', $event)" :current-page="currentPage" />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from "vue";
import {
  RESOURCE_FILTER_ALL_AREA,
  RESOURCE_FILTER_ALL_TIME,
  normalizeExclusiveAllOption
} from "../utils/resourceLibraryFilters";

const props = defineProps({
  areaList: { type: Array, default: () => [] },
  selectedAreaList: { type: Array, default: () => [] },
  pubTimeList: { type: Array, default: () => [] },
  selectedPubTimeList: { type: Array, default: () => [] },
  curFooterList: { type: Array, default: () => [] },
  total: { type: Number, default: 0 },
  currentPage: { type: Number, default: 1 },
  loading: { type: Boolean, default: false }
});

const emit = defineEmits([
  "update:selectedAreaList",
  "update:selectedPubTimeList",
  "filter-change",
  "report-click",
  "page-change"
]);

const handleAreaGroupChange = (val) => {
  emit("update:selectedAreaList", normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_AREA));
  emit("filter-change");
};

const handleTimeGroupChange = (val) => {
  emit("update:selectedPubTimeList", normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_TIME));
  emit("filter-change");
};
const formatDate = (str) => {
  if (!str) return ''
  const [y, m, d] = str.split('T')[0].split('-')
  return `${y}年${+m}月${+d}日`
};

const hasData = computed(() => Array.isArray(props.curFooterList) && props.curFooterList.length > 0);
const pageCount = computed(() => {
  const size = 12;
  const total = Number(props.total || 0) || 0;
  return Math.max(1, Math.ceil(total / size));
});
</script>

<style lang="scss" scoped>
.home-main-footer-main {
  margin: 0 auto;
  margin-top: 36px;
  width: 1600px;
  display: flex;
  gap: 16px;

  .left {
    width: 360px;
    height: 100%;
    padding-bottom: 24px;
    box-sizing: border-box;
    border: 1px solid rgba(234, 236, 238, 1);
    border-radius: 10px;
    box-shadow: 0px 0px 20px 0px rgba(94, 95, 95, 0.1);
    background: rgba(255, 255, 255, 1);
    position: relative;

    .select-box {
      margin-top: 16px;

      .header {
        display: flex;
        gap: 17px;

        .icon {
          margin-top: 4px;
          width: 8px;
          height: 16px;
          background: var(--color-main-active);
          border-radius: 0 4px 4px 0;
        }

        .title {
          height: 24px;
          color: var(--color-main-active);
          font-family: "Source Han Sans CN";
          font-size: 16px;
          font-weight: 700;
          line-height: 24px;
          letter-spacing: 1px;
          text-align: left;
        }
      }

      .select-main {
        margin-left: 24px;
        margin-top: 12px;

        .checkbox-group {
          display: grid;
          grid-template-columns: repeat(2, 160px);
          gap: 8px 4px;

          .all-checkbox {
            width: 160px;
            height: 24px;
            margin: 0;
          }

          .filter-checkbox {
            width: 160px;
            height: 24px;
            margin-right: 0 !important;
          }
        }

      }

      .select-main1 {
        width: 100px;

        .checkbox-group {
          display: grid;
          grid-template-columns: repeat(2, 160px);
          gap: 8px 4px;

          .filter-checkbox {
            width: 160px;
            height: 24px;
            margin-right: 0 !important;
          }
        }
      }
    }
  }

  .right {
    width: 1284px;
    max-height: 1377px;
    display: flex;
    flex-direction: column;

    .card-box {
      width: 1226px;
      flex: 1;
      display: flex;
      flex-wrap: wrap;
      gap: 16px 16px;

      .right-empty {
        width: 100%;
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 24px;
      }

      .footer-card {
        width: 398px;
        height: 300px;
        border-radius: 10px;
        box-shadow: 0px 0px 20px 0px rgba(25, 69, 130, 0.1);
        background: rgba(255, 255, 255, 1);
        cursor: pointer;

        .footer-card-top {
          width: 364px;
          height: 180px;
          margin: 0 auto;
          margin-top: 15px;

          img {
            width: 100%;
            height: 100%;
          }
        }

        .footer-card-title {
          margin: 0 auto;
          margin-top: 13px;
          width: 360px;
          height: 48px;
          /* 修改高度为两行的高度 */
          color: rgba(59, 65, 75, 1);
          font-family: Microsoft YaHei;
          font-size: 18px;
          font-weight: 700;
          line-height: 24px;
          overflow: hidden;
          /* 隐藏超出部分 */
          text-overflow: ellipsis;
          /* 显示省略号 */
          display: -webkit-box;
          /* 使用弹性盒模型 */
          -webkit-line-clamp: 2;
          /* 限制显示两行 */
          -webkit-box-orient: vertical;
          /* 设置盒模型方向为垂直 */
        }

        .footer-card-footer {
          margin: 0 auto;
          margin-top: 5px;
          width: 360px;
          height: 22px;
          display: flex;
          justify-content: space-between;
          color: rgba(95, 101, 108, 1);
          font-family: Microsoft YaHei;
          font-size: 14px;
          font-weight: 400;
          line-height: 22px;
        }
      }
    }

    .right-footer {
      height: 50px;
      margin-top: 43px;
      display: flex;
      width: 1226px;
      justify-content: space-between;

      .info {
        height: 19px;
        color: rgba(132, 136, 142, 1);
        font-family: Microsoft YaHei;
        font-size: 14px;
        font-weight: 400;
        line-height: 18px;
        letter-spacing: 0px;
        text-align: left;
      }
    }
  }
}



.filter-checkbox {
  width: 160px;
  height: 24px;
  margin: 0;
}
</style>
