<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 class="select-hearing-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(researchHearingIds)"
              @change="val => handleToggleAll(val, researchHearingIds)">
              全部部门
            </el-checkbox>
            <el-checkbox v-for="type in researchHearingList" :key="type.id" v-model="selectedResearchHearingIds"
              :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="card-content">
          <div v-for="(item, index) in displayList" :key="item.id">
            <div class="card-item">
              <img class="card-item-img" src="../images/img congress.png" alt="report image" />
              <div class="card-item-text">
                <div class="card-item-title">

                  {{ item.title }}
                </div>
                <div class="card-item-time">
                  {{ item.time + ' · ' + item.content }}
                  <img src="../images/image open.png" alt="open icon" class="card-open-image" />
                </div>
                <div class="card-item-category"> {{ item.category }}</div>
              </div>

            </div>
            <div class="divider" v-if="index !== displayList.length - 1"></div>
          </div>
        </div>
      </div>
      <div class="right-footer">
        <div class="info">
          共 {{ filteredHearingData.length }} 篇智库报告
        </div>
        <div class="page-box">
          <el-pagination :page-size="10" background layout="prev, pager, next" :total="filteredHearingData.length"
            @current-change="handleCurrentChange" :current-page="currentPage" />
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref, toRefs, watch, computed } from "vue";

const props = defineProps({
  researchTypeList: {
    type: Array,
    default: () => []
  },
  researchTimeList: {
    type: Array,
    default: () => []
  },
  researchHearingList: {
    type: Array,
    default: () => []
  },
  hearingData: {
    type: Array,
    default: () => []
  },
  selectedFilters: {
    type: Object,
    default: () => ({
      researchTypeIds: [],
      researchTimeIds: [],
      researchHearingIds: []
    })
  },
  selectedYear: {
    type: Number,
    default: 1
  },
  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, researchHearingList, hearingData, selectedYear } = toRefs(props);

const pageSize = 10;
function getDateYearsAgo(years) {
  const currentDate = new Date();
  return new Date(currentDate.getFullYear() - years, currentDate.getMonth(), currentDate.getDate());
}

function parseChineseDate(dateStr) {
  if (!dateStr) return null;
  const match = String(dateStr).match(/(\d{4})年(\d{1,2})月(\d{1,2})日/);
  if (!match) return null;
  return new Date(Number(match[1]), Number(match[2]) - 1, Number(match[3]));
}

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

watch(
  () => props.selectedFilters,
  val => {
    selectedResearchIds.value = val?.researchTypeIds ? [...val.researchTypeIds] : [];
    selectedResearchTimeIds.value = val?.researchTimeIds ? [...val.researchTimeIds] : [];
    selectedResearchHearingIds.value = val?.researchHearingIds ? [...val.researchHearingIds] : [];
  },
  { immediate: true, deep: true }
);

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

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

const getTargetSelection = ids => {
  if (ids === researchTypeIds.value) return selectedResearchIds;
  if (ids === researchTimeIds.value) return selectedResearchTimeIds;
  return selectedResearchHearingIds;
};

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 filteredHearingData = computed(() => {
  const rangeStart = getDateYearsAgo(selectedYear.value || 1);
  return (hearingData.value || []).filter(item => {
    const itemDate = parseChineseDate(item.time);
    const matchTopRange = itemDate ? itemDate >= rangeStart : true;
    const matchYear =
      selectedResearchTimeIds.value.length === 0 ||
      selectedResearchTimeIds.value.some(year => String(item.time || "").startsWith(year));
    const matchDepartment =
      selectedResearchHearingIds.value.length === 0 ||
      selectedResearchHearingIds.value.some(department =>
        String(item.content || "").includes(department) || String(item.title || "").includes(department)
      );
    const matchType =
      selectedResearchIds.value.length === 0 ||
      selectedResearchIds.value.some(typeId =>
        String(item.category || "").includes(typeId) || String(item.title || "").includes(typeId)
      );
    return matchTopRange && matchYear && matchDepartment && matchType;
  });
});

// 只展示当前页的数据
const displayList = computed(() => {
  const list = filteredHearingData.value || [];
  const start = (currentPage.value - 1) * pageSize;
  return list.slice(start, start + pageSize);
});

// 保持模板里的方法名不变，但改成通知父组件，直接传入当前选中值避免时序问题
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>
.main-content {
  display: flex;
  gap: 16px;



  .left {
    width: 360px;
    height: 874px;
    padding-bottom: 36px;
    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: 284px;
      margin-top: 19px;

      .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: Microsoft YaHei;
          font-size: 20px;
          font-weight: 700;
          line-height: 26px;
          letter-spacing: 1px;
          text-align: left;
        }
      }

      .select-main {

        margin-left: 24px;


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

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

        }

      }




    }

    .select-time-box {
      margin-top: 44px;
      width: 360px;
      height: 156px;

      .select-box-header {
        margin-bottom: 12px;
        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: 26px;
          color: var(--color-main-active);
          font-family: Microsoft YaHei;
          font-size: 20px;
          font-weight: 700;
          line-height: 26px;
          letter-spacing: 1px;
          text-align: left;
        }
      }

      .select-main {

        margin-left: 24px;


        .checkbox-group {
          display: grid;
          grid-template-columns: repeat(2, 1fr);
          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-hearing-box {
      margin-top: 36px;
      width: 360px;

      .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: Microsoft YaHei;
          font-size: 20px;
          font-weight: 700;
          line-height: 26px;
          letter-spacing: 1px;
          text-align: left;
        }
      }

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

        .checkbox-group {
          display: grid;
          width: 259px;
          gap: 4px;

          .filter-checkbox {
            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: 1224px;
    height: 1377px;

    .card-box {
      width: 100%;
      height: 1248px;
      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);
      background: rgba(255, 255, 255, 1);

      .card-content {
        width: 1211px;
        height: 1067px;
        margin-top: 33px;

        margin-left: 37px;

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




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

          .card-item-text {
            flex: 1;
            min-width: 0;

            flex-direction: column;
            justify-content: space-between;

            display: flex;

            .card-item-title {

              color: rgb(59, 65, 75);
              font-family: "Source Han Sans CN";
              font-size: 18px;
              font-weight: 700;
              line-height: 22px;
              letter-spacing: 0px;
              text-align: left;
              margin-bottom: 2px;
              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;
              letter-spacing: 0px;
              margin-bottom: 7px;
              text-align: left;
              display: inline-flex;

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

            .card-item-category {

              padding: 1px 8px;
              box-sizing: border-box;
              border: 1px solid rgb(231, 243, 255);
              border-radius: 4px;
              display: inline-flex;

              background-color: rgb(247, 248, 249);
              color: rgba(132, 136, 142, 1);
              font-family: Microsoft YaHei;
              font-size: 14px;
              font-weight: 400;
              line-height: 20px;
              letter-spacing: 0px;
              text-align: left;
              width: fit-content;
            }
          }


        }

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


      }

    }

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

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

}

:deep(.el-checkbox__inner) {
  border-radius: 4px !important;
}
</style>