<template>
  <view class="poi-search-page" :class="{ 'host-navigation': shouldUseHostNavigation }">
    <view v-if="!shouldUseHostNavigation" class="search-header">
      <view class="search-header-copy">
        <text class="page-title">点位搜索</text>
        <text class="page-subtitle">Search Museum Locations</text>
      </view>
      <view class="map-link" @tap="handleBackToMap">
        <text class="map-link-text">返回地图</text>
      </view>
    </view>

    <view class="search-panel-shell">
      <PoiSearchPanel
        :initial-keyword="initialKeyword"
        variant="page"
        autofocus
      />
    </view>
  </view>
</template>

<script setup lang="ts">
import { computed, onMounted, onUnmounted, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import PoiSearchPanel from '@/components/search/PoiSearchPanel.vue'
import { isEmbeddedInWechatMiniProgram } from '@/utils/hostEnvironment'

const initialKeyword = ref('')
const shouldUseHostNavigation = computed(() => isEmbeddedInWechatMiniProgram())

onLoad((options: any) => {
  const keyword = typeof options.keyword === 'string'
    ? decodeURIComponent(options.keyword)
    : ''

  initialKeyword.value = keyword
})

const handleBackToMap = () => {
  uni.navigateBack({
    delta: 1,
    fail: () => {
      uni.reLaunch({
        url: '/pages/index/index?tab=guide'
      })
    }
  })
}

// #ifdef H5
let removeSearchViewportResizeListener: (() => void) | null = null

const updateSearchViewportHeight = () => {
  if (typeof document === 'undefined') return

  const viewportHeight = window.visualViewport?.height || window.innerHeight
  document.documentElement.style.setProperty('--poi-search-page-height', `${viewportHeight}px`)
}

onMounted(() => {
  updateSearchViewportHeight()

  const target = window.visualViewport || window
  target.addEventListener('resize', updateSearchViewportHeight)
  removeSearchViewportResizeListener = () => {
    target.removeEventListener('resize', updateSearchViewportHeight)
  }
})

onUnmounted(() => {
  removeSearchViewportResizeListener?.()
  removeSearchViewportResizeListener = null

  if (typeof document !== 'undefined') {
    document.documentElement.style.removeProperty('--poi-search-page-height')
  }
})
// #endif
</script>

<style scoped lang="scss">
.poi-search-page {
  --museum-h5-page-width: min(100vw, 430px);
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  min-height: 100vh;
  min-height: 100dvh;
  padding: calc(env(safe-area-inset-top) + 18px) 0 0;
  box-sizing: border-box;
  background: #f5f5ed;
  color: #262421;
  font-family: '鸿蒙黑体', 'HarmonyOS Sans SC', 'HarmonyOS Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.poi-search-page::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 210px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.96) 0 64px, rgba(224, 225, 0, 0.2) 65px, rgba(21, 23, 19, 0.36) 100%);
  pointer-events: none;
}

/* #ifdef H5 */
.poi-search-page::before {
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.96) 0 64px, rgba(0, 0, 0, 0.18) 65px, rgba(0, 0, 0, 0.5) 100%),
    url('/static/guide/app-launch-loading.webp') center top / cover no-repeat;
}

.poi-search-page {
  height: var(--poi-search-page-height, 100dvh);
  min-height: var(--poi-search-page-height, 100dvh);
  max-width: var(--museum-h5-page-width);
  margin: 0 auto;
  overscroll-behavior: none;
}
/* #endif */

.poi-search-page.host-navigation {
  padding-top: 0;
}

.poi-search-page.host-navigation::before {
  display: none;
}

.search-header {
  position: relative;
  z-index: 1;
  min-height: 48px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 18px 118px;
}

.search-panel-shell {
  position: relative;
  z-index: 1;
  min-height: 0;
  flex: 1;
  display: flex;
  padding: 18px 14px calc(env(safe-area-inset-bottom) + 14px);
  box-sizing: border-box;
  background: #fffef0;
  border-radius: 22px 22px 0 0;
  overflow: hidden;
  box-shadow: 0 -10px 28px rgba(0, 0, 0, 0.08);
}

.search-header-copy {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.page-title {
  font-size: 24px;
  line-height: 30px;
  font-weight: 800;
  letter-spacing: 0;
  color: #050704;
}

.page-subtitle {
  font-size: 11px;
  line-height: 15px;
  font-weight: 500;
  color: #4f5149;
  white-space: nowrap;
}

.map-link {
  min-width: 76px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 10px;
  box-sizing: border-box;
  background: #050704;
  border: 1px solid #050704;
  border-radius: 8px;
}

.map-link-text {
  font-size: 13px;
  line-height: 18px;
  font-weight: 700;
  color: var(--museum-accent);
  white-space: nowrap;
}

@media (min-width: 768px) {
  .poi-search-page {
    max-width: 430px;
    margin: 0 auto;
  }
}

@media (max-width: 360px) {
  .poi-search-page {
    padding-top: calc(env(safe-area-inset-top) + 14px);
  }

  .search-header {
    margin: 0 14px 108px;
  }

  .search-panel-shell {
    padding-left: 12px;
    padding-right: 12px;
  }

  .page-title {
    font-size: 23px;
    line-height: 28px;
  }

  .map-link {
    min-width: 68px;
    height: 32px;
    padding: 0 8px;
  }
}
</style>
