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


export default function Products() {
    const getProductsData = useMutation({
        mutationFn: (reqData) => {
            return productsData(reqData)
        },
        onError: (error) => {
            console.log(error)
        },
        onSuccess: (res) => {
            if(res?.data?.resultCode != '0'){
                throw({message: 'Something went wrong'})
            }
        }
    })

    useEffect(()=>{
      let reqData = {
        token: localStorage.getItem('token'), // USER TOKEN
        uid: localStorage.getItem('uid') // USER UID
      }
      getProductsData.mutate(reqData)
    },[])
    
    const products = getProductsData?.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">
            {getProductsData?.isPending ? 
                <>
                    <div className="row">
                        <div className="col-12">
                            <p className='text-mute'>Loading...</p>
                        </div>
                    </div>
                </>
                : getProductsData?.isPending ?
                <div className="row">
                    <div className="col-12">
                        <p className='text-danger'>{getProductsData?.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">
                                    <div className="icon-container img-icon m-r-20 bg-light-gray rounded">
                                        <i className="fa fa-cart-plus text-primary"></i>
                                    </div>
                                    <div className="report-details">
                                        <p>{product?.status_text}</p>
                                        <h4>{product?.name}</h4>
                                    </div>
                                </div>
                            </Link>

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