import React, {useState} from "react";
import { useQuery } from '@tanstack/react-query'
import BreadcrumbComBS from "../breadcrumb/BreadcrumbComBS";
// import getImage from "../../utils/getImage";
import ProductStart from "./ProductStart";
import { useLocation } from 'react-router-dom';
import {MyProductData, productData} from "../../services/services";
import queryKeys from "../../services/queryKeys";
import ProductActive from "./ProductActive";
import ProductProvision from "./ProductProvision";
import {productConst} from "../../constants/products";

export default function ProductFactory(){
    const location = useLocation();
    const pathname = location.pathname;
    const [productStatus, setProductStatus] = useState(0);

    //productConst.PRODUCT_ACTIVE


    // Split the pathname by '/' and get the last element
    const lastPart = pathname.split('/').pop();
    console.log(lastPart)

    const {data, isFetching, isError, error} = useQuery({
        queryKey: queryKeys.product,
        queryFn: () => MyProductData(lastPart)
    })



    const myproduct_data = data?.data?.myproduct_data
    //setProductStatus(myproduct_data?.status)

    const product_name =  myproduct_data?.product_name;

    const product_status  = myproduct_data?.status;

    return(
        <>
            <BreadcrumbComBS title={product_name} paths={['Dashboard', 'Product']} />
            <div className="row">
                {isFetching ?
                    <>
                        <div className="row">
                            <div className="col-12">
                                <p className='text-mute'>Loading...</p>
                            </div>
                        </div>
                    </>
                    : isError ?
                        <div className="row">
                            <div className="col-12">
                                <p className='text-danger'>{error.message}</p>
                            </div>
                        </div>
                        :
                <div>
                    {(product_status <= productConst.PRODUCT_AVAILABLE)?
                        <ProductStart productData={myproduct_data} />
                        :<></> }

                     {(product_status === productConst.PRODUCT_PROVISIONING)?
                        <ProductProvision productData={myproduct_data} />
                        :<></> }

                    {(product_status === productConst.PRODUCT_ACTIVE)?
                        <ProductActive productData={myproduct_data} />
                        :<></> }

                </div>
               }
            </div>
        </>
    )
}