<template>
  <div
    class="relative min-h-0 overscroll-none w-full h-dvh overflow-hidden bg-[linear-gradient(205deg,#E8FFF1_0%,rgba(138,227,252,0)_20%),linear-gradient(155deg,#fef7e1_0%,rgba(254,247,225,0)_20%),radial-gradient(48%_48%_at_61%_118%,#ffffff_0%,rgba(255,255,255,0)_100%),linear-gradient(180deg,rgba(238,248,255,0)_0%,#F0F8F3_50%),#F0F8F3]">
    <ChatMainList />

    <!-- 日历组件 -->
    <Calender :visible="calendarVisible" mode="single" :default-value="selectedDate" @close="handleCalendarClose"
      @select="handleDateSelect" />

    <!-- 更多服务 -->
    <MoreService />

    <!-- 抽屉组件 -->
    <DrawerSection ref="drawerRef" @close="closeDrawer" />
  </div>
</template>

<script setup lang="ts">
import { ref, onUnmounted, onMounted } from "vue";
import { emitter } from '@/utils/events'

// import { getUrlParams } from "@/utils/UrlParams";
// import { checkToken } from "@/hooks/useGoLogin";
import ChatMainList from "./components/ChatMainList/index.vue";
import MoreService from "./components/MoreService/index.vue";
import DrawerSection from "./components/DrawerSection/index.vue";
import Calender from "@/components/Calender/index.vue";

const calendarVisible = ref(false);
const selectedDate = ref("");

// 处理日历关闭
const handleCalendarClose = () => {
  calendarVisible.value = false;
};

// 处理日期选择
const handleDateSelect = (data: any) => {
  selectedDate.value = data.date;
  calendarVisible.value = false;
  console.log("选择的日期:", data.date);
  emitter.emit("selectCalendarDate", selectedDate.value); // 传回父组件
};

const openCalendar = () => {
  calendarVisible.value = true;
};

type DrawerExpose = {
  open: () => void | Promise<void>;
  close: () => void;
};

const drawerRef = ref<DrawerExpose | null>(null);

const handleShowDrawer = () => {
  drawerRef.value?.open();
};

const closeDrawer = () => {
  drawerRef.value?.close();
};


onMounted(() => {
  emitter.on("SHOW_DRAWER", handleShowDrawer);
  emitter.on("openCalendar", openCalendar);
});

onUnmounted(() => {
  emitter.off("SHOW_DRAWER", handleShowDrawer);
  emitter.off("openCalendar", openCalendar);
});
</script>
