<template>
  <div class="main-content">
    <div class="left">
      <!-- <div class="select-box">
					<div class="select-box-header">
						<div class="icon"></div>
						<div class="title">{{ "报告类型" }}</div>
					</div>
					<div class="select-main">
						<div class="checkbox-group">
							<el-checkbox v-for="type in reportTypeList" :key="type.id" v-model="selectedReportTypeList"
								:label="type.id" class="filter-checkbox" @change="handleGetThinkDynamicsReport()">
								{{ type.typeName }}
							</el-checkbox>
						</div>
					</div>
				</div> -->
      <div class="select-research-box">
        <div class="select-box-header">
          <div class="icon"></div>
          <div class="title">{{ "科技领域" }}</div>
        </div>
        <div class="select-main">
          <div class="checkbox-group">
            <el-checkbox class="filter-checkbox" :model-value="isGroupAllSelected(researchTypeIds)"
              @change="val => handleToggleAll(val, researchTypeIds)">
              全部领域
            </el-checkbox>
            <el-checkbox v-for="type in researchTypeList" :key="type.id" v-model="selectedResearchIds" :label="type.id"
              class="filter-checkbox" @change="handleGetThinkDynamicsReport()">
              {{ type.name }}
            </el-checkbox>
          </div>
        </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">
          <div class="checkbox-group">
            <el-checkbox class="filter-checkbox" :model-value="isGroupAllSelected(researchTimeIds)"
              @change="val => handleToggleAll(val, researchTimeIds)">
              全部时间
            </el-checkbox>
            <el-checkbox v-for="type in researchTimeList" :key="type.id" v-model="selectedResearchTimeIds"
              :label="type.id" class="filter-checkbox" @change="handleGetThinkDynamicsReport()">
              {{ type.name }}
            </el-checkbox>
          </div>
        </div>
        <!-- <div class="input-main">
						<el-input placeholder="输入作者名字" v-model="author" @input="handleGetThinkDynamicsReport()" />
					</div> -->
      </div>
    </div>
    <div class="right">
      <div class="card-box">
        <div class="footer-card" v-for="(item, index) in curFooterList" :key="index"
          @click="handleToReportDetail(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">{{ item.times }}</div>
            <div class="from">{{ item.thinkTankName }}</div>
          </div>
        </div>
      </div>
      <div class="right-footer">
        <div class="info">
          共 {{ total }} 篇智库报告
        </div>
        <div class="page-box">
          <el-pagination :page-size="12" background layout="prev, pager, next" :total="total"
            @current-change="handleCurrentChange" :current-page="currentPage" />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { computed, ref, toRefs, watch } from "vue";

const props = defineProps({
  researchTypeList: {
    type: Array,
    default: () => []
  },
  researchTimeList: {
    type: Array,
    default: () => []
  },
  selectedFilters: {
    type: Object,
    default: () => ({
      researchTypeIds: [],
      researchTimeIds: [],
      researchHearingIds: []
    })
  },
  curFooterList: {
    type: Array,
    default: () => []
  },
  total: {
    type: Number,
    default: 0
  },
  currentPage: {
    type: Number,
    default: 1
  }
});

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

// 解构 props，保持模板里变量名不变
const { researchTypeList, researchTimeList, curFooterList, total, currentPage } = toRefs(props);

const selectedResearchIds = ref([]);
const selectedResearchTimeIds = ref([]);

// 父组件更新时同步到子组件
watch(
  () => props.selectedFilters,
  val => {
    selectedResearchIds.value = val?.researchTypeIds ? [...val.researchTypeIds] : [];
    selectedResearchTimeIds.value = val?.researchTimeIds ? [...val.researchTimeIds] : [];
  },
  { immediate: true, deep: true }
);

const buildSelectedFiltersPayload = () => ({
  researchTypeIds: [...selectedResearchIds.value],
  researchTimeIds: [...selectedResearchTimeIds.value],
  researchHearingIds: []
});

const researchTypeIds = computed(() => (researchTypeList.value || []).map(item => item.id));
const researchTimeIds = computed(() => (researchTimeList.value || []).map(item => item.id));

const getTargetSelection = ids => (ids === researchTypeIds.value ? selectedResearchIds : selectedResearchTimeIds);

const isGroupAllSelected = ids =>
  ids.length > 0 && ids.every(id => getTargetSelection(ids).value.includes(id));

const handleToggleAll = (checked, ids) => {
  if (!ids.length) return;
  const targetSelection = getTargetSelection(ids);
  const nextSelected = new Set(targetSelection.value);
  if (checked) {
    ids.forEach(id => nextSelected.add(id));
  } else {
    ids.forEach(id => nextSelected.delete(id));
  }
  targetSelection.value = [...nextSelected];
  handleGetThinkDynamicsReport();
};

// 保持模板里的方法名不变，但改成通知父组件，直接传入当前选中值避免时序问题
const handleGetThinkDynamicsReport = () => {
  const payload = buildSelectedFiltersPayload();
  emit("update:selectedFilters", payload);
  emit("filter-change", payload);
};

const handleCurrentChange = page => {
  emit("page-change", page);
};

const handleToReportDetail = item => {
  emit("report-click", item);
};
</script>
<style lang="scss" scoped>
/* 统一智库动态智库报告内所有 el-checkbox 文本为 16px */
:deep(.el-checkbox__label) {
  font-size: 16px;
}

.main-content {
  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-research-box {
      width: 360px;
      height: 100%;
      margin-top: 16px;

      .select-box-header {
        display: flex;
        gap: 17px;
        margin-bottom: 12px;

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

        .title {
          height: 26px;
          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;

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

          .filter-checkbox {
            width: 160px;
            height: 24px;

          }
        }

      }

      .select-main1 {
        width: 100px;
      }

      .input-main {
        margin: 10px auto;
        width: 250px;
        height: 34px;
        border-radius: 10px;
        border: 1px solid rgba(230, 231, 232, 1);
      }
    }

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

      .select-box-header {
        display: flex;
        gap: 17px;
        margin-bottom: 12px;

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

        .title {
          height: 26px;
          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;

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

          .filter-checkbox {
            width: 160px;
            height: 24px;
          }

        }

      }

      .select-main1 {
        width: 100px;
      }

      .input-main {
        margin: 10px auto;
        width: 250px;
        height: 34px;
        border-radius: 10px;
        border: 1px solid rgba(230, 231, 232, 1);
      }
    }
  }

  .right {
    width: 1284px;
    height: 1377px;

    .card-box {
      width: 1226px;
      height: 1248px;
      display: flex;

      flex-wrap: wrap;
      gap: 13px;

      .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);
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        position: relative;

        &:hover {
          transform: translateY(-3px);
          box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
        }

        cursor: pointer;

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

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

        .footer-card-title {
          margin: 0 auto;
          margin-top: 12px;
          width: 360px;

          color: rgb(59, 65, 75);
          font-family: "Source Han Sans CN";
          font-size: 18px;
          font-weight: 700;
          line-height: 24px;
          // 多行省略 + 自动换行（兼容写法）
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box; // webkit 私有：开启弹性盒模型
          display: box; // 标准写法（兜底）
          -webkit-line-clamp: 2; // webkit 私有：限制行数
          line-clamp: 2; // 标准属性：解决兼容性提示
          -webkit-box-orient: vertical; // webkit 私有：垂直排列
          box-orient: vertical; // 标准写法（兜底）

          // 基础换行属性（确保文字能换行）
          word-wrap: break-word;
          word-break: break-all;
          white-space: normal;

        }

        .footer-card-footer {
          position: absolute;
          left: 50%;
          bottom: 15px;
          transform: translateX(-50%);
          margin: 0;
          width: 360px;
          height: 22px;
          display: flex;
          justify-content: space-between;
          color: rgb(95, 101, 108);
          font-family: "Source Han Sans CN";
          font-size: 14px;
          font-weight: 400;
          line-height: 22px;
        }
      }
    }

    .right-footer {
      margin-top: 35px;
      display: flex;
      justify-content: space-between;

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

:deep(.el-checkbox) {
  margin-right: 0 !important;

}
</style>