<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">
          <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 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">
          <el-checkbox-group class="checkbox-group" :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 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 hearingData" :key="item.id">
            <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)">
                  <span v-html="highlightText(item.title)"></span>
                </div>
                <div class="card-item-time">
                  <span v-html="highlightText(item.time + ' · ' + item.content)"></span>
                  <img src="../images/image open.png" alt="open icon" class="card-open-image" />
                </div>
                <div class="card-item-category">
                  <AreaTag v-for="(val, idx) in item.category" :key="idx" :tagName="val" />
                </div>
              </div>

            </div>
            <div class="divider" v-if="index !== hearingData.length - 1"></div>
          </div>

        </div>
      </div>
      <div class="right-footer">
        <div class="info">
          共 {{ total }} 篇国会听证会
        </div>
        <div class="page-box">
          <el-pagination :page-size="10" background layout="prev, pager, next" :total="total"
            @current-change="handleCurrentChange" :current-page="currentPage" />
        </div>
      </div>

    </div>
  </div>
</template>
<script setup>
import { ref, toRefs, watch, computed } from "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: () => []
  },
  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
  },
  searchKeyword: {
    type: String,
    default: ""
  }
});

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([RESOURCE_FILTER_ALL_AREA]);
const selectedResearchTimeIds = ref([RESOURCE_FILTER_ALL_TIME]);
const selectedResearchHearingIds = ref([RESOURCE_FILTER_ALL_DEPT]);

const escapeHtml = (text) => {
  return String(text ?? "")
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&#39;");
};

const escapeRegExp = (text) => {
  return String(text ?? "").replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};

const highlightText = (text) => {
  const safeText = escapeHtml(text);
  const keyword = (props.searchKeyword || "").trim();
  if (!keyword) return safeText;
  const pattern = new RegExp(`(${escapeRegExp(keyword)})`, "gi");
  return safeText.replace(pattern, '<span class="keyword-highlight">$1</span>');
};

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

const buildSelectedFiltersPayload = () => ({
  researchTypeIds: stripAllAreaForRequest(selectedResearchIds.value),
  researchTimeIds: stripAllTimeForRequest(selectedResearchTimeIds.value),
  researchHearingIds: stripAllDeptForRequest(selectedResearchHearingIds.value)
});

const handleAreaGroupChange = (val) => {
  selectedResearchIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_AREA);
  handleGetThinkDynamicsReport();
};

const handleTimeGroupChange = (val) => {
  selectedResearchTimeIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_TIME);
  handleGetThinkDynamicsReport();
};

const handleDeptGroupChange = (val) => {
  selectedResearchHearingIds.value = normalizeExclusiveAllOption(val, RESOURCE_FILTER_ALL_DEPT);
  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;
  height: 100%;

  margin-bottom: 80px;




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

      .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: "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-hearing-box {
      margin-top: 16px;
      width: 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: Microsoft YaHei;
          font-size: 16px;
          font-weight: 700;
          line-height: 24px;
          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);
      }
    }
  }

  :deep(.keyword-highlight) {
    background-color: #fff59d;
  }

  .right {
    width: 1224px;

    .card-box {



      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;

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

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





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

          .card-item-text {

            flex-direction: column;


            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;
              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;
              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;
                cursor: pointer;
              }
            }

            .card-item-category {

              box-sizing: border-box;

              display: inline-flex;

              gap: 8px;

              text-align: left;
              width: fit-content;
            }
          }


        }

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


      }

    }


  }

  .right-footer {


    margin-top: 35px;
    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;

}
</style>