<template>
  <view :class="['page-wrapper', { dark: isDark }]">
    <cwg-match-media :max-width="991" v-if="!isLoginPage">
      <cwg-pc-header @open-right-drawer="openRightDrawer" @open-left-drawer="openLeftDrawer" class="header-box"
        :sidebarVisible="sidebarVisible" />
      <view class="sidebar-mask mask-visible" v-if="sidebarVisible" @click="openLeftDrawer">

      </view>
      <view class="fixed"></view>
      <cwg-header v-if=pageTitle class="custom-header" :title="pageTitle" />
    </cwg-match-media>
    <cwg-language style="width: 0;display: none;" />
    <cwg-progress />
    <cwg-confirm-popup />

    <view class="page-content" :style="{ backgroundColor: bgColor }">
      <cwg-match-media :max-width="991" v-if="!isLoginPage">
        <view class="left-sidebar" :class="{ 'sidebar-visible': sidebarVisible }">
          <cwg-sidebar />
        </view>
        <view class="sidebar-mask" v-if="sidebarVisible" @click="openLeftDrawer" :style="{
          opacity: maskVisible ? 1 : 0,
          transition: 'opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1)'
        }">

        </view>
      </cwg-match-media>
      <view class="content-info">
        <!--      完善信息提示，-->
        <PrefectInfo v-if="!isLoginPage" />
        <!--      申请成为ib，-->
        <IbInfo ref="ibRef" v-if="!isLoginPage" />

        <view class="content-wrapper" :class="{ 'content-wrapper-padding': isContentPadding }">
          <!-- <cwg-header /> -->
          <transition name="fade" mode="out-in">
            <view :key="$route.path">
              <slot />
            </view>
          </transition>
          <cwg-custom-footer />
        </view>
      </view>
      <view :style="{ height: isTabBarPage ? '60px' : '0px' }" />
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { onLoad, onShow } from '@dcloudio/uni-app'
import useRoute, { updateRoute } from '@/hooks/useRoute'
import useRouter from '@/hooks/useRouter'
import useUserStore from '@/stores/use-user-store'
import { userApi } from '@/api/user'
import useGlobalStore from '@/stores/use-global-store'
import PrefectInfo from '@/components/PrefectInfo.vue'
import IbInfo from '@/components/IbInfo.vue'

const globalStore = useGlobalStore()
const router = useRouter()
const userStore = useUserStore()

const props = defineProps({
  // 是否固定顶部导航栏
  isHeaderFixed: {
    type: Boolean,
    default: false,
  },
  // 是否登录页，登录页不显示侧边栏和顶部导航,注册忘记密码等页面
  isLoginPage: {
    type: Boolean,
    default: false,
  },
  // 是否含有padding 默认有
  isContentPadding: {
    type: Boolean,
    default: true,
  },
  // 主内容背景颜色
  bgColor: {
    type: String,
    default: '',
  },
  // 顶部导航栏标题
  pageTitle: {
    type: String,
    default: '',
  },
})
const isDark = computed(() => globalStore.theme === 'dark')
const isTabBarPage = ref(false)
const rightDrawerRef = ref<any>(null)
const noticeDrawerRef = ref<any>(null)
const ibRef = ref<any>(null)

// 事件处理函数
const handleOpenIb = () => {
  if (ibRef.value) ibRef.value?.getInfo()
}

const handleOpenNoticeDrawer = () => {
  noticeDrawerRef.value?.open()
}

const handleOpenRightDrawer = () => {
  openRightDrawer()
}

onMounted(() => {
  // 只在组件挂载时注册事件监听器
  uni.$once('open-ib', handleOpenIb)
  uni.$on('open-right-drawer', handleOpenRightDrawer)
})

onUnmounted(() => {
  // 在组件销毁时移除事件监听器
  uni.$off('open-ib', handleOpenIb)
  uni.$off('open-right-drawer', handleOpenRightDrawer)
})

function openRightDrawer() {
  rightDrawerRef.value?.open()
}

const sidebarVisible = ref(false)
const maskVisible = ref(false)

function openLeftDrawer() {
  if (sidebarVisible.value) {
    // 关闭时先隐藏遮罩层
    maskVisible.value = false
    // 等待遮罩层动画结束后再隐藏侧边栏
    setTimeout(() => {
      sidebarVisible.value = false
    }, 200)
  } else {
    // 打开时先显示侧边栏
    sidebarVisible.value = true
    // 等待侧边栏动画结束后再显示遮罩层
    setTimeout(() => {
      maskVisible.value = true
    }, 200)
  }
}

function handleDrawerNavigate(path: string) {
  router.push(path)
}

async function handleDrawerLogout() {
  try {
    const res = await userApi.logout()
    if (res.code === 200) {
      userStore.clearUserInfo()
      router.push('/pages/login/index')
    }
  } catch (error) {
    userStore.clearUserInfo()
    router.push('/pages/login/index')
  }
}

onLoad(() => {
  updateRoute()
})

onShow(() => {
  updateRoute()
})

</script>

<style lang="scss" scoped>
@import "@/uni.scss";

.page-wrapper {
  height: calc(100vh - 56px);
}

.header-box {
  width: 100%;
  height: 56px;
}

.page-content {
  width: 100%;
  height: calc(100vh - 56px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  position: relative;
  box-sizing: border-box;

  .content-info {
    height: calc(100vh - 56px);
    overflow: auto;
  }

}

.left-sidebar {
  width: 0;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  z-index: 1000;
  background-color: #fff;
  transition: width 81ms cubic-bezier(0.4, 0, 0.2, 1);

}

.sidebar-mask {
  position: absolute;
  left: 0;
  top: 0;
  width: 100vw;
  height: calc(100vh - 56px);
  background-color: rgba(0, 0, 0, 0.2);
  z-index: 101;
}

.mask-visible {
  background-color: rgba(0, 0, 0, 0);
  width: 100vw;
  height: 56px;
}

.sidebar-visible {
  width: px2rpx(280);
}

.content-wrapper {
  max-width: px2rpx(1224);
  width: 100%;
  margin: 0 auto;
  padding-top: px2rpx(20);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: calc(100vh - 56px);
}

.fixed {
  // position: fixed;
  // top: 0;
  // left: 0;
  width: 100%;
  height: 56px;
  background-color: var(--color-white);
  z-index: 9;
}

.custom-header {
  background-color: var(--color-white);
  padding: 0 px2rpx(15);
  position: fixed;
  top: 56px;
  left: 0;
}

@media screen and (max-width: 1504px) {
  .content-wrapper-padding {
    padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  }
}

@media screen and (max-width: 991px) {
  .content-wrapper-padding {
    padding: px2rpx(16) px2rpx(16) 0 px2rpx(16);
  }

  .page-wrapper {
    height: 100vh;
  }
}

.mobile-menu-btn {
  width: px2rpx(64);
  height: px2rpx(64);
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 页面切换动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}

/* 侧边栏动画优化 */
.left-sidebar {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-mask {
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* 内容区域动画 */
.content-wrapper {
  animation: slideIn 0.2s ease-out;
}

@keyframes slideIn {
  from {
    transform: translateY(20px);
    opacity: 0;
  }

  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>
