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

import Icons from '../Icons'

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

export default function LoanDetails({transactionID}) {

    const {data:allLoans, isFetching, isError, error} = useQuery({
        queryKey: queryKeys.loans,
        queryFn: () => getLoans({transaction_id: transactionID})
    })

    const loans = allLoans?.data?.loans // LOANS LIST
    // const loansCount = allLoans?.data?.count // LOANS LIST COUNT

    return (
        <>
        {isFetching ? 
            <div className='box bg-white dark:bg-black-box text-black-body dark:text-white-body'>
                <p className='pb-4 font-bold text-base'>Loans</p>
                <p className='text-slate-800'>Loading...</p>
            </div>
        : isError ?
            <div className='box bg-white dark:bg-black-box text-black-body dark:text-white-body'>
                <p className='pb-4 font-bold text-base'>Loans</p>
                <p className='text-red-500'>{error.message}</p>
            </div>
        : (loans && loans.length > 0) ?
            <div className='box bg-white dark:bg-black-box text-black-body dark:text-white-body'>
                <p className='pb-4 font-bold text-base'>Loans</p>
                <table className="table-auto 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 text-right">
                                Loan/Eligible Amount
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Product/Tenor
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Repay/Install Amount.
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Added/Due
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Action
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {loans?.map((item, index) => (
                            <tr key={index} 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" />
                                    <div className="text-left">
                                        <div className="text-base font-semibold">{item?.account_id || ''}</div>
                                        <div className="font-normal text-gray-500">{item?.id} : {item?.transaction_id}</div>
                                        <div className="font-semibold text-red-500">ORIGIN : {item?.original_transaction}</div>
                                        <div className="font-bold text-blue-500">OFFER : {item?.offer_id}</div>
                                    </div>  
                                    </div>
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        {/* <div className="text-base font-semibold">{formatNumber(item?.initial_loan_amount)}</div> */}
                                        <div className="font-normal text-gray-500">{formatNumber(item?.initial_loan_amount)}</div>
                                        <div className="font-semibold text-red-500">{formatNumber(item?.eligible_amount)}</div>

                                    </div> 
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="font-normal text-gray-500">{formatNumber(item?.product_id)}</div>
                                        <div className="font-normal text-gray-500">{item?.tenor} days</div>
                                    </div> 
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="font-normal text-gray-500">{formatNumber(item?.repayment_amount)}</div>
                                        <div className="font-normal text-gray-500">{formatNumber(item?.installment_amount)}</div>
                                    </div> 
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="font-normal text-gray-500">{getDateFromDateString(item?.created_at)}</div>
                                        <div className="font-semibold text-red-500">{getDateFromDateString(item?.due_date)}</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>
                        ))
                        }
                    </tbody>
                </table>
            </div>
        :
            null
        }
        </>
    )
}