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 { approvedLoan } from '../../services/siteServices'
import getDateFromDateString from '../../helpers/GetDateFromDateString';
import getTimeFromDateString from '../../helpers/GetTimeFromDateString';

export default function ApprovedLoanCom() {

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

    const approvedUsers = data?.data?.result_data?.data // APPLY LOAN LIST

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

            <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={approvedUsers} 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">
                                            Name
                                        </th>
                                        <th scope="col" className="px-2">
                                            Loan
                                        </th>
                                        <th scope="col" className="px-2">
                                            Added
                                        </th>
                                        <th scope="col" className="px-2 text-right">
                                            Action
                                        </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?.name || ''}</div>
                                                    <div className="font-normal text-gray-500">{item?.bvn}</div>
                                                </div>  
                                                </div>
                                            </td>
                                            <td className="px-2">
                                                <div className="text-left">
                                                    <div className="text-base font-semibold">{item?.loan}</div>
                                                    <div className="font-normal text-gray-500">{item?.description}</div>
                                                </div> 
                                            </td>
                                            <td className="px-2">
                                                <div className="text-left">
                                                    <div className="font-normal text-gray-500">{getDateFromDateString(item?.added)} {getTimeFromDateString(item?.added)}</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='edit' />
                                                </div>
                                                <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 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='trash' />
                                                </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>
    )
}