<template>
  <div class="home-main-footer-main">
    <div class="left">
      <div class="select-box-science">
        <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="selectedTypeIds" @change="handleAreaGroupChange">
            <el-checkbox class="filter-checkbox" :label="RESOURCE_FILTER_ALL_AREA">
              {{ RESOURCE_FILTER_ALL_AREA }}
            </el-checkbox>
            <el-checkbox class="filter-checkbox" v-for="t in (researchTypeList || [])" :key="t.id" :label="t.id">
              {{ t.name }}
            </el-checkbox>
          </el-checkbox-group>
        </div>
      </div>

      <div class="select-box-publish">
        <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="selectedYearIds" @change="handleYearGroupChange">
            <el-checkbox class="filter-checkbox" :label="RESOURCE_FILTER_ALL_TIME">
              {{ RESOURCE_FILTER_ALL_TIME }}
            </el-checkbox>
            <el-checkbox class="filter-checkbox" v-for="y in (researchTimeList || [])" :key="y.id" :label="y.id">
              {{ y.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 list" :key="item.id ?? index">
              <div class="card-item" @click="emit('item-click', item)">
                <div class="card-item-img">
                  <img :src="item.imageUrl" alt="" />
                </div>
                <div class="card-item-text">
                  <div class="card-item-title">{{ item.name }}</div>
                  <div class="card-item-time">
                    <span class="info-text">{{ formatDate(item.times) }} · {{ item.thinkTankName }} · {{ item.reportName
                    }}</span>
                    <div class="card-open-image" @click.stop="handleOpenReportOriginal(item)">
                      <img src="@/views/thinkTank/ThinkTankDetail/thinkDynamics/images/image open.png" alt="" />
                    </div>
                  </div>
                  <div class="card-item-category" v-if="item.tagList && item.tagList.length">
                    <AreaTag v-for="d in item.tagList" :key="d" :tagName="d" />
                  </div>

                  <div class="file-box">
                    <div class="file" v-for="sv in item.billInfoList" :key="sv.id || sv.name">
                      <div class="type">法案</div>
                      <div class="title">{{ sv.name }}</div>
                      <div class="more" @click.stop="handleBillMoreClick(sv)">
                        <img src="../assets/images/image-right.png" alt="" />
                      </div>
                    </div>
                    <div class="file" v-for="(sv, index) in item.administrativeOrderInfoVOList" :key="index">
                      <div class="type">政令</div>
                      <div class="title">{{ sv.content }}</div>
                      <div class="more" @click.stop="handleAdministrativeMoreClick(sv)">
                        <img src="../assets/images/image-right.png" alt="" />
                      </div>
                    </div>

                  </div>
                </div>
              </div>
              <div class="divider" v-if="index !== list.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="p => emit('page-change', p)" :current-page="currentPage" />
        </div>
      </div>
    </div>
  </div>
</template>

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

const props = defineProps({
  researchTypeList: { type: Array, default: () => [] },
  researchTimeList: { type: Array, default: () => [] },
  list: { type: Array, default: () => [] },
  total: { type: Number, default: 0 },
  currentPage: { type: Number, default: 1 },
  pageSize: { type: Number, default: 7 },
  loading: { type: Boolean, default: false },
});

const emit = defineEmits(["filter-change", "page-change", "item-click"]);

const router = useRouter();

/** 打开报告原文：新标签页打开 /thinkTank/reportOriginal/:id（优先 reportId） */
const handleOpenReportOriginal = (item) => {
  const reportId = item?.reportId || item?.report_id || item?.id
  if (!reportId) return
  const route = router.resolve({
    name: "ReportDetail",
    params: { id: String(reportId) }
  })
  window.open(route.href, "_blank")
}
const formatDate = (str) => {
  if (!str) return ''
  const [y, m, d] = str.split('T')[0].split('-')
  return `${y}年${+m}月${+d}日`
};
/** 政策建议关联法案：新标签页打开法案介绍页，billId 随接口 id 变化 */
const handleBillMoreClick = (bill) => {
  const billId = bill?.id;
  if (!billId) {
    return;
  }
  const route = router.resolve({
    path: "/billLayout/bill/introduction",
    query: { billId: String(billId) }
  });
  window.open(route.href, "_blank");
};
/** 政策建议关联法案：新标签页打开政令介绍页*/
const handleAdministrativeMoreClick = (ad) => {
  const id = ad?.bcId;
  if (!id) {
    return;
  }
  const route = router.resolve({
    path: "/decreeLayout/overview/introduction",
    query: { id: String(id) }
  });
  window.open(route.href, "_blank");
};
const selectedTypeIds = ref([RESOURCE_FILTER_ALL_AREA]);
const selectedYearIds = ref([RESOURCE_FILTER_ALL_TIME]);

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

const emitFilterToParent = () => {
  emit("filter-change", {
    researchTypeIds: stripAllAreaForRequest(selectedTypeIds.value),
    researchTimeIds: stripAllTimeForRequest(selectedYearIds.value),
  });
};

const handleAreaGroupChange = (val) => {
  selectedTypeIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_AREA);
  emitFilterToParent();
};

const handleYearGroupChange = (val) => {
  selectedYearIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_TIME);
  emitFilterToParent();
};
</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(25, 69, 130, 0.1);
    background: rgba(255, 255, 255, 1);

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

      .select-box-header {
        display: flex;

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

        .title {
          margin-left: 17px;
          font-family: "Source Han Sans CN";
          font-weight: 700;
          font-size: 16px;
          line-height: 24px;
          letter-spacing: 1px;
          text-align: left;
          color: rgba(5, 95, 194, 1);
        }
      }

      .select-main {
        margin-top: 12px;
      }

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

        .filter-checkbox {
          width: 160px;
          margin: 0;
          height: 24px;
          font-family: "Source Han Sans CN", sans-serif;
          font-weight: 400;
          font-size: 16px;
          line-height: 24px;
          letter-spacing: 0px;
          text-align: justify;
        }
      }
    }

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

      .select-box-header {
        display: flex;

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

        .title {
          margin-left: 17px;
          font-family: "Source Han Sans CN";
          font-weight: 700;
          font-size: 16px;
          line-height: 24px;
          letter-spacing: 1px;
          text-align: left;
          color: rgba(5, 95, 194, 1);
        }
      }

      .select-main {
        margin-top: 12px;
      }

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

        .filter-checkbox {
          width: 160px;
          margin: 0;
          height: 24px;
          font-family: "Source Han Sans CN", sans-serif;
          font-weight: 400;
          font-size: 16px;
          line-height: 24px;
          letter-spacing: 0px;
          text-align: justify;
        }
      }
    }
  }

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


    .card-box {
      width: 100%;
      padding-bottom: 32px;
      ;
      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;

        .divider {
          height: 1px;
          background: rgb(234, 236, 238);
          margin: 16px 0;

        }

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



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

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

          .card-item-text {
            flex: 1;
            min-width: 0;
            display: flex;
            flex-direction: column;
            gap: 7px;

            .card-item-title {
              color: rgb(59, 65, 75);
              font-family: "Source Han Sans CN";
              font-size: 18px;
              font-weight: 700;
              line-height: 22px;

              display: inline-flex;
            }

            .card-item-time {
              color: rgb(95, 101, 108);
              font-family: "Source Han Sans CN";
              font-size: 14px;
              font-weight: 400;
              line-height: 22px;

              text-align: left;
              display: inline-flex;

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

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


            }


          }

          .card-item-category {
            gap: 8px;
            height: 24px;
            display: flex;
            overflow: hidden;
            white-space: nowrap;
          }

          .tag-box {

            display: flex;
            gap: 8px;

            .tag {
              height: 22px;
              padding: 0 8px;
              box-sizing: border-box;
              border: 1px solid rgba(230, 231, 232, 1);
              border-radius: 4px;
              background: rgba(247, 248, 249, 1);
              color: rgba(95, 101, 108, 1);
              font-family: Microsoft YaHei;
              font-size: 14px;
              font-weight: 400;
              line-height: 20px;
            }
          }

          .file-box {

            display: flex;
            gap: 8px;

            .file {
              height: 32px;

              display: flex;
              justify-content: center;
              align-items: center;
              gap: 12px;

              border-radius: 4px;
              background: rgba(246, 250, 255, 1);
              padding-left: 8px;
              padding-right: 8px;

              .type {
                height: 22px;

                padding: 0 4px;
                border-radius: 4px;
                background: rgba(231, 243, 255, 1);
                color: rgba(5, 95, 194, 1);
                font-family: "Source Han Sans CN";
                font-size: 14px;
                font-weight: 400;
                line-height: 22px;

              }

              .title {
                color: rgba(5, 95, 194, 1);
                font-family: "Source Han Sans CN";
                font-size: 16px;
                font-weight: 400;
                line-height: 24px;

              }

              .more {
                width: 20px;
                height: 20px;
                display: flex;
                margin-top: 2px;
                cursor: pointer;

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



    .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;
      }
    }
  }
}
</style>
