<template>
  <ie-page bg-color="white">
    <ie-navbar transparent bg-color="#FFFFFF" :placeholder="false" custom-back :click-hover="false">
      <template #headerLeft>
        <view class="flex items-center gap-7 text-fore-title">
          <view class="text-38 font-bold">{{ orgName }}</view>
        </view>
      </template>
      <template #headerRight="{ isTransparent }">
        <view v-if="userStore.getLocation" class="ml-10 flex items-center gap-x-4" @click="handleChangeLocation">
          <uv-icon name="map" size="16" :color="isTransparent ? '#333' : 'var(--primary-color)'" />
          <text class="text-30 font-medium" :class="[isTransparent ? 'text-fore-title' : 'text-primary']">
            {{ userStore.getLocation }}
          </text>
        </view>
      </template>
    </ie-navbar>
    <view class="absolute top-0 left-0 right-0 z-0 h-[489rpx] bg-gradient-to-b from-[#6ACAFF] to-white"></view>
    <view class="relative z-2" :style="{ paddingTop: baseStickyTop + 10 + 'px' }">
      <index-banner />
      <index-map />
      <index-guide v-if="false" @detail="handleDetail" />
      <index-news @detail="handleDetail" />
    </view>
    <template #tabbar>
      <ie-tabbar :active="0" />
      <index-popup ref="popupRef" />
    </template>
  </ie-page>
</template>

<script lang="ts" setup>
import IndexBanner from './components/index-banner.vue';
import IndexGuide from './components/index-guide.vue';
import IndexNews from './components/index-news.vue';
import IndexMap from "./components/index-map.vue";
import indexPopup from './components/index-popup.vue';

import { useUserStore } from '@/store/userStore';
// @ts-ignore
import { useTransferPage } from '@/hooks/useTransferPage';
import { useNavbar } from '@/hooks/useNavbar';
import { onPageShow } from '@dcloudio/uni-app';

const { routes, transferTo } = useTransferPage();
const { baseStickyTop } = useNavbar();
const scrollTop = ref(0);
const isHide = ref(false);
const userStore = useUserStore();
const orgName = computed(() => userStore.orgInfo.orgName);

const handleDetail = async (id: number | string, title?: string) => {
  if (('' + id).includes(',')) {
    transferTo(routes.newsGroup, {
      data: {
        ids: id,
        title: title
      }
    });
  } else {
    transferTo(routes.newsDetail, {
      data: {
        id: id,
        title: title
      }
    })
  }
}

const popupRef = ref();
const checkProvinceInfo = () => {
  if (!userStore.isLogin && !userStore.tempInfo?.location) {
    popupRef.value.open();
  }
}
const checkTeacherInfo = async () => {
  await userStore.checkInfoComplete();
}
const reloadUserInfo = async () => {
  if (userStore.isLogin) {
    await userStore.getUserInfo();
  }
}
const handleChangeLocation = () => {
  if (userStore.isLogin) {
    return;
  }
  popupRef.value.open();
}
onHide(() => {
  isHide.value = true;
})
onPageScroll((e) => {
  if (!isHide.value) {
    scrollTop.value = e.scrollTop;
  }
});
onShow(() => {
  setTimeout(() => {
    uni.pageScrollTo({
      scrollTop: scrollTop.value,
      duration: 0
    });
  }, 0);
  setTimeout(() => {
    checkProvinceInfo();
    checkTeacherInfo();
    reloadUserInfo();
  }, 500);
  isHide.value = false;
});
</script>

<style lang="scss" scoped></style>
