<script lang="ts" setup>
import { isLoggedIn } from './utils/auth.ts'

const DEFAULT_HOME_PATH = 'pages/home/home'
const LOGIN_PAGE_URL = '/pages/login/login?from=appLaunch'

const beforeAppRouteCallback = (event : BeforeAppRouteEvent) => {
  if (event.openType != 'appLaunch') {
    return
  }

  console.log(`[dynamic-homepage] appLaunch: ${event.path}`)

  // 只处理默认首页,保留扫码、分享等直接进入二级页面的启动方式。
  if (event.path == DEFAULT_HOME_PATH && !isLoggedIn()) {
    uni.rewriteRoute({
      url: LOGIN_PAGE_URL,
      success: () => {
        console.log(
          `[dynamic-homepage] rewrite success: ${event.path} -> pages/login/login`
        )
      },
      fail: (error) => {
        console.error(
          `[dynamic-homepage] rewrite fail: ${error.errMsg}`
        )
      },
    })
  }
}

onLaunch(() => {
  // appLaunch 事件不会补发,应在 onLaunch 中尽早注册。
  uni.onBeforeAppRoute(beforeAppRouteCallback)
})
</script>

<style>
page {
  background-color: #f5f5f5;
  color: #1f2329;
}

.page {
  flex: 1;
  padding: 24px;
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: #1f2329;
}

.description {
  margin-top: 12px;
  font-size: 15px;
  line-height: 24px;
  color: #646a73;
}

.status {
  margin-top: 24px;
  padding: 16px;
  border-radius: 6px;
  background-color: #ffffff;
}

.status-label {
  font-size: 13px;
  color: #8f959e;
}

.status-value {
  margin-top: 8px;
  font-size: 17px;
  font-weight: 600;
  color: #1f2329;
}

.primary-button {
  margin-top: 28px;
  background-color: #007aff;
  color: #ffffff;
}

.secondary-button {
  margin-top: 12px;
}
</style>