<script setup>
import axios from '@axios';
import { onBeforeMount, onMounted, onUnmounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useStore } from 'vuex';
const store = useStore()
const router = useRouter()
const route = useRoute()
const cartEncoded = ref(
    {
        "gender": "male",
        "doctor_visit": "89.00",
        "products": [
            {
            "product_id": 6616,
            "qty": 1,
            "subscription": true,
            "onetime": false
            },
            {
            "product_id": 6618,
            "qty": 1,
            "subscription": true,
            "onetime": false
            },
            {
            "product_id": 6620,
            "qty": 1,
            "subscription": false,
            "onetime": true
            }
        ]
    }
);
const cartJson = ref()
const gender = ref(null)
const doctor_visit = ref(null)
const cart = route.query.cart
const plans = ref([]);
onBeforeMount(async () => {
    await store.dispatch('getLabKits', {})
    console.log('Labkit ', store.getters.getLabOrderProductList)
    localStorage.setItem('labkits', JSON.stringify(store.getters.getLabOrderProductList))
    // Example usage
    const encodedCart = encodeCartJson();
    console.log('Encoded:', encodedCart);

    decodeCartJson(cart);
    console.log('Decoded:', cartJson.value);
});
onMounted(async () => {
    const fullRoute = {
        path: route.path,
        params: route.params,
        query: route.query,
        name: route.name,
        fullPath: route.fullPath,
    }

    console.log('Current route:', fullRoute.fullPath)
    localStorage.setItem('go_checkout', fullRoute.fullPath)
    let plansapi = await axios.post('/api/plans', {})
    console.log('Plans Data', plansapi.data)
    const finalArray = cartJson.value.map(cartItem => {
        const matchedProduct = plansapi.data.find(plan => plan.id === cartItem.product_id);
        if (matchedProduct) {
            return {
                ...matchedProduct,
                qty: cartItem.qty,
                subscription: cartItem.subscription,
                onetime: cartItem.onetime
            };
        }
        return null;
    }).filter(item => item !== null);
    const finalArrayJson = JSON.stringify(finalArray);

    // Save the JSON string to localStorage
    localStorage.setItem('cart_products', finalArrayJson);
    localStorage.setItem('gender', gender.value);
    localStorage.setItem('doctor_visit', doctor_visit.value);
    router.push('/pre-register');
    // plans.value = plansapi.data

});
onUnmounted(() => { });
const encodeCartJson = () => {
    const jsonStr = JSON.stringify(cartEncoded.value);
    const encodedStr = btoa(jsonStr);
    return encodedStr;
}

const decodeCartJson = (encodedStr) => {
    console.log(encodedStr)
    const jsonStr = atob(encodedStr);
    const decodedJson = JSON.parse(jsonStr);
    console.log('decodedJson',decodedJson)
    cartJson.value = decodedJson.products;
    gender.value = decodedJson.gender
    doctor_visit.value = decodedJson.doctor_visit
}



</script>

<template>
    <!-- Template content -->
</template>
