import React, { useState } from 'react'
import { useQuery } from "@tanstack/react-query";

import BreadcrumbCom from '../breadcrumb/BreadcrumbCom'
import TablePaginatedWrapper from '../tableWrapper/TablePaginatedWrapper'
import Icons from '../Icons'

import Avatar from '../../assets/user_avatar.jpg'
import queryKeys from '../../services/queryKeys'
import { getOffers } from '../../services/siteServices'
import getDateFromDateString from '../../helpers/GetDateFromDateString';
import formatNumber from '../../helpers/formatNumber';

export default function OffersCom() {

    const [page, setPage] = useState(1)

    const {data, isFetching, isError, error} = useQuery({
        queryKey: [...queryKeys.offers, page],
        queryFn: () => getOffers({page}),
        staleTime: 0,
        // placeholderData: keepPreviousData,
    })

    const offers = data?.data?.offers // LOAN CHARGES LIST
    const pagination = data?.data?.pagination
    console.log('offers', offers)

    return (
        <div className='w-full flex flex-col gap-8'>
            <BreadcrumbCom title='Offers' paths={['Dashboard', 'Offers']} />

            <div className='box bg-white dark:bg-black-box text-black-body dark:text-white-body'>
                {isFetching ? 
                <>
                    <p className='text-slate-800'>Loading...</p>
                </>
                : isError ?
                    <p className='text-red-500'>{error.message}</p>
                :
                    <TablePaginatedWrapper data={offers} isFetching={isFetching} setPage={setPage} itemsPerPage={pagination?.limit} pagination={pagination}>
                    {({ data }) => (
                        <>
                            <table className="py-2 w-full text-sm">
                                <thead className="py-2 text-sm text-slate-500 text-left">
                                    <tr>
                                        <th scope="col" className="px-1 py-1">
                                            ID
                                        </th>
                                        <th scope="col" className="px-1 py-1">
                                            Name
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Interest Rate
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Insurance Rate
                                        </th>
                                        <th scope="col" className="px-1 text-right">
                                            Mgt. Rate
                                        </th>
                                        <th scope="col" className="px-1 text-right">
                                            Vat. Rate
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Max/Min Amount
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Tenor
                                        </th>
                                        {/*<th scope="col" className="px-2 text-right">*/}
                                        {/*    Action*/}
                                        {/*</th>*/}
                                    </tr>
                                </thead>
                                <tbody>
                                    {(data && data.length > 0) ? data?.map((item, index) => (
                                        <tr key={index} className="py-2 border-t border-dashed border-slate-300">
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.id || ''}</div>
                                                </div>
                                            </td>
                                            <td className="px-2 py-2">
                                                <div className='w-full min-w-48 flex items-center gap-2 whitespace-nowrap'>
                                                {/*<img className="w-10 h-10 rounded-md" src={Avatar} alt="Jese image" />*/}
                                                <div className="text-left">
                                                    <div className="text-base font-semibold">{item?.product_id || ''}</div>
                                                    {/* <div className="font-normal text-gray-500 line-clamp-1">{item?.description}</div> */}
                                                </div>  
                                                </div>
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.interest_rate)}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.insurance_rate)}</div>
                                                </div> 
                                            </td>
                                            <td className="px-1">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.management_rate)}</div>
                                                </div> 
                                            </td>
                                            <td className="px-1">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.vat_rate)}</div>
                                                </div>
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.max_amount)}</div>
                                                    <div className="font-normal text-gray-500">{formatNumber(item?.min_amount)}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.tenor}</div>
                                                </div> 
                                            </td>
                                            {/*<td className="px-2 text-right">*/}
                                            {/*    <div className='flex items-center justify-end gap-3 md:gap-4'>*/}
                                            {/*        <div className='p-2 flex justify-center items-center text-slate-500 bg-white-body dark:text-white-body dark:bg-black-body rounded-md'>*/}
                                            {/*            <Icons name='eye' />*/}
                                            {/*        </div>*/}
                                            {/*    </div>*/}
                                            {/*</td>*/}
                                        </tr>
                                    ))
                                    :
                                    <tr className="py-2 border-t border-dashed border-slate-300">
                                        <td className="px-3 py-2" colSpan={7}>
                                            <div className="flex justify-center items-center">
                                                No Record Found
                                            </div>
                                        </td>
                                    </tr>
                                    }
                                </tbody>
                            </table>
                        </>
                    )}
                    </TablePaginatedWrapper>
                }
            </div>
        </div>
    )
}