<script setup>
import { ref } from 'vue'
import InquireForm from '@/components/InquireForm.vue'

definePage({ layout: 'default' })

const feature = ref('')
const IMMERSIVE_NAVBAR_ROUTE = 'pages/inquire'

// 获取产品信息
const featureData = ref({})

onLoad(async (query) => {
  const q = query || {}
  feature.value = String(q.feature || '')
  if (q.out_trade_no) {
    uni.navigateTo({
      url: `/pages/report-result-webview?out_trade_no=${encodeURIComponent(String(q.out_trade_no))}`,
    })
    return
  }
  await getProduct()
})

async function getProduct() {
  if (!feature.value)
    return
  const { data } = await useApiFetch(`/product/en/${feature.value}`)
    .get()
    .json()

  if (data.value) {
    featureData.value = data.value.data
  }
}

onPageScroll((event) => {
  uni.$emit('immersive-navbar-change', {
    route: IMMERSIVE_NAVBAR_ROUTE,
    solid: event.scrollTop > 120,
  })
})

onUnload(() => {
  uni.$emit('immersive-navbar-change', {
    route: IMMERSIVE_NAVBAR_ROUTE,
    solid: false,
  })
})
</script>

<template>
  <InquireForm type="normal" :feature="feature" :feature-data="featureData" />
</template>
