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

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

      <div class="select-hearing-box">
        <div class="select-box-header">
          <div class="icon"></div>
          <div class="title">{{ "听证会部门" }}</div>
        </div>
        <div class="select-main">
          <el-checkbox-group class="checkbox-group hearing-grid" :model-value="selectedResearchHearingIds"
            @change="handleDeptGroupChange">
            <el-checkbox class="filter-checkbox" :label="RESOURCE_FILTER_ALL_DEPT">
              {{ RESOURCE_FILTER_ALL_DEPT }}
            </el-checkbox>
            <el-checkbox v-for="type in researchHearingList" :key="type.id" class="filter-checkbox" :label="type.id">
              {{ type.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="card-content">
            <div v-for="(item, index) in hearingData" :key="item.id ?? index">
              <div class="card-item">
                <img class="card-item-img" :src="item.coverImgUrl" alt="report image" />
                <div class="card-item-text">
                  <div class="card-item-title" @click="emit('report-click', item)">
                    {{ item.titleZh }}
                  </div>
                  <div class="card-item-time">
                    {{ item.testimonyDate + ' · ' + item.thinkTankName + ' · ' + item.committeeZh }}
                    <img src="../ThinkTankDetail/thinkDynamics/images/image open.png" alt="open icon"
                      class="card-open-image" />
                  </div>

                  <div class="card-item-category"
                    v-if="Array.isArray(item.domains) && item.domains.some(v => String(v || '').trim())">
                    <div v-for="(value, index) in item.domains" :key="`domain-${index}`">
                      <AreaTag :key="`cat-${item.id}`" :tagName="value" />
                    </div>
                  </div>
                </div>
              </div>
              <div class="divider" v-if="index !== hearingData.length - 1"></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="pageSize" :page-count="pageCount" background layout="prev, pager, next"
            @current-change="handlePageChange" :current-page="currentPage" />
        </div>
      </div>

    </div>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import AreaTag from "@/components/base/AreaTag/index.vue";
import {
  RESOURCE_FILTER_ALL_AREA,
  RESOURCE_FILTER_ALL_TIME,
  RESOURCE_FILTER_ALL_DEPT,
  RESOURCE_FILTER_EARLIER,
  normalizeExclusiveAllOption,
  stripAllAreaForRequest,
  stripAllTimeForRequest,
  stripAllDeptForRequest,
  matchesEarlierChineseDate
} from "../utils/resourceLibraryFilters";

const props = defineProps({
  researchTypeList: { type: Array, default: () => [] },
  researchTimeList: { type: Array, default: () => [] },
  selectedAreaList: { type: Array, default: () => [] },
  selectedPubTimeList: { type: Array, default: () => [] },
  hearingData: { 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",
  "page-change",
  "report-click"
]);

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

const selectedResearchIds = computed(() => (
  Array.isArray(props.selectedAreaList) && props.selectedAreaList.length
    ? props.selectedAreaList
    : [RESOURCE_FILTER_ALL_AREA]
));
const selectedResearchTimeIds = computed(() => (
  Array.isArray(props.selectedPubTimeList) && props.selectedPubTimeList.length
    ? props.selectedPubTimeList
    : [RESOURCE_FILTER_ALL_TIME]
));
const selectedResearchHearingIds = ref([RESOURCE_FILTER_ALL_DEPT]);



const researchHearingList = ref([

]);

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

const handleTimeGroupChange = (val) => {
  emit("update:selectedPubTimeList", normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_TIME));
  emit("page-change", 1);
  emit("filter-change");
};

const handleDeptGroupChange = (val) => {
  selectedResearchHearingIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_DEPT);
  currentPage.value = 1;
};





const handlePageChange = page => {
  emit("page-change", page);
  emit("filter-change");
};
</script>

<style lang="scss" scoped>
/* 统一资源库国会听证会内所有 el-checkbox 文本为 16px */
:deep(.el-checkbox__label) {
  font-size: 16px;
}

/* 外层对齐智库报告资源库布局 */
.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-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-research-box {
    width: 360px;
    height: 284px;
    margin-top: 16px;
  }

  .select-time-box {
    margin-top: 16px;
    width: 360px;

  }

  .select-hearing-box {
    margin-top: 16px;
    width: 360px;
  }

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

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

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

    .hearing-grid {
      grid-template-columns: 160px;
      width: 259px;
      gap: 4px;
    }
  }

  .right {
    width: 1224px;
    display: flex;
    flex-direction: column;


    .card-box {
      width: 100%;
      flex: 1;
      display: flex;
      background: rgba(255, 255, 255, 1);
      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);
      padding-right: 36px;

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

      .card-content {
        width: 1211px;

        margin-top: 33px;
        margin-left: 37px;
        padding-bottom: 27px;

      }
    }

    .right-footer {
      margin-top: 43px;
      display: flex;
      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;
      }
    }
  }



  .card-item {
    width: 100%;
    height: 77px;
    display: flex;


    .card-item-img {
      width: 56px;
      height: 77px;
      margin-right: 22px;
      flex-shrink: 0;
    }

    .card-item-text {


      display: flex;
      flex-direction: column;

      .card-item-title {
        color: rgb(59, 65, 75);
        font-family: "Source Han Sans CN";
        font-size: 18px;
        font-weight: 700;
        line-height: 22px;
        margin-bottom: 2px;
        display: inline-flex;
        cursor: pointer;
      }

      .card-item-time {
        color: rgb(95, 101, 108);
        font-family: "Source Han Sans CN";
        font-size: 14px;
        font-weight: 400;
        line-height: 22px;
        margin-bottom: 7px;
        text-align: left;
        display: inline-flex;

        .card-open-image {
          width: 16px;
          height: 16px;
          margin-left: 9px;
          margin-top: 3px;
          cursor: pointer;
        }
      }

      .card-item-category {
        gap: 8px;
        height: 24px;
        display: flex;


      }
    }
  }

  .divider {
    height: 1px;
    background: rgb(234, 236, 238);
    margin: 16px 0;
  }
}
</style>
