import { useQuery } from '@tanstack/react-query'
import { productsData } from '../../services/services'
import productPath from "../../utils/productpath";
import { Link } from 'react-router-dom';

import queryKeys from '../../services/queryKeys'


export default function Products() {

    let reqData = {
        token: localStorage.getItem('token'), // USER TOKEN
        uid: localStorage.getItem('uid') // USER UID
    }
    const {data, isFetching, isError, error} = useQuery({
        queryKey: queryKeys.products,
        queryFn: () => productsData(reqData)
    })
    const products = data?.data?.products_data // PRODUCTS DATA

  return (
    <>
        <div className="card card-statistics h-100 mb-0 panel_round_c1">
            <div className="card-header">
                <h4 className="card-title">My Products</h4>
            </div>
            <div className="card-body pb-0">
            {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 className="row m-b-20">
                    {products && products.map((product, index) => (
                        <div key={product.uid+index} className={`col-xxs-6 col-xl-4 col-xxl-6 mb-2 mb-xxl-0`}>
                            <Link to={productPath(product?.product_id)}  >
                                <div className={`d-flex align-items-center extraProductCard  ${product?.icon_style}`} style={{borderColor:'black', borderWidth: '2px'}} >
                                    <div className="icon-container img-icon m-r-20 bg-light-gray rounded">
                                        <i className={`fa ${product?.product_icon} text-primary`}></i>
                                    </div>
                                    <div className="report-details">
                                        <p><span style={{fontWeight: 'bolder', color: '#00557A'}}>{product?.status_text}</span></p>
                                        <h4><span className='text-truncate' style={{paddingLeft: '10px'}}>{product?.name}</span></h4>
                                    </div>
                                </div>
                            </Link>

                        </div>
                    ))}
                </div>
            }
                <div className="apexchart-wrapper">
                    <div id="ecommerce5" className="chart-fit"></div>
                </div>
            </div>
        </div>
    </>
  )
}
