import React from 'react'
import { useQuery } from "@tanstack/react-query";
import {Link} from 'react-router-dom'

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

import Avatar from '../../assets/user_avatar.jpg'
import queryKeys from '../../services/queryKeys'
import { loanOffers } from '../../services/siteServices'

import getTimeFromDateString from '../../helpers/GetTimeFromDateString';
import getDateFromDateString from '../../helpers/GetDateFromDateString';

export default function ApplicationsLoanCom() {

    const {data, isFetching, isError, error} = useQuery({
        queryKey: queryKeys.apply_loan,
        queryFn: () => loanOffers()
    })

    const loanOffersUsersList = data?.data // LOAN OFFERS LIST

    return (
        <div className='w-full flex flex-col gap-8'>
            <BreadcrumbCom title='Loan offers' paths={['Dashboard', 'Loan 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>
                :
                    <TableWrapper data={loanOffersUsersList} itemsPerPage={15}>
                    {({ 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-2 py-2">
                                            CID
                                        </th>
                                        <th scope="col" className="px-2">
                                            Loan
                                        </th>
                                        <th scope="col" className="px-2">
                                            Amount
                                        </th>
                                        <th scope="col" className="px-2">
                                            Description
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Days Duration
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Active
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Score
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Lorder
                                        </th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {(data && data.length > 0) ? data?.map((item, index) => (
                                        <tr className="py-2 border-t border-dashed border-slate-300">
                                            <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?.cid || ''}</div>
                                                </div>  
                                                </div>
                                            </td>
                                            <td className="px-2">
                                                <div className="text-left">
                                                    <div className="font-normal text-gray-500">{item?.loan}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-left">
                                                    <div className="font-normal text-gray-500">{item?.amount}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-left">
                                                    <div className="font-normal text-gray-500">{item?.description}</div>
                                                </div> 
                                            </td>

                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.days_duration}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.active}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.score}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-right">
                                                    <div className="font-normal text-gray-500">{item?.lorder}</div>
                                                </div> 
                                            </td>
                                        </tr>
                                    ))
                                    :
                                    <tr className="py-2 border-t border-dashed border-slate-300">
                                        <td className="px-3 py-2" colSpan={4}>
                                            <div className="flex justify-center items-center">
                                                No Record Found
                                            </div>
                                        </td>
                                    </tr>
                                    }
                                </tbody>
                            </table>
                        </>
                    )}
                    </TableWrapper>
                }
            </div>
        </div>
    )
}