<script setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import InquireForm from "@/components/InquireForm.vue";

const route = useRoute();
const feature = ref(route.params.feature);

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

onMounted(async () => {
    isFinishPayment();
    await getProduct();
});

function isFinishPayment() {
    const query = new URLSearchParams(window.location.search);
    let orderNo = query.get("out_trade_no");
    if (orderNo) {
        router.push({ path: "/report", query: { orderNo } });
    }
}

async function getProduct() {
    const { data, error } = await useApiFetch(`/product/en/${feature.value}`)
        .get()
        .json();

    if (data.value) {
        featureData.value = data.value.data;
        // 确保 FLXG0V4B 排在首位
        if (
            featureData.value.features &&
            featureData.value.features.length > 0
        ) {
            featureData.value.features.sort((a, b) => {
                if (a.api_id === "FLXG0V4B") return -1;
                if (b.api_id === "FLXG0V4B") return 1;
                return 0;
            });
        }
    }
}
</script>

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