import React, { useEffect, useState } 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 { getRepaymentSchedule } from '../../services/siteServices'
import getDateFromDateString from '../../helpers/GetDateFromDateString';
import formatNumber from '../../helpers/formatNumber';
import localSiteIcons from "../../helpers/localSiteIcons";
export default function RepaymentScheduleDetails({transactionID}) {

    const [page, setPage] = useState(1)

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

    const repaymentSchedule = data?.data?.repayment_schedules // LOAN CHARGES LIST
    // const pagination = data?.data?.pagination

    useEffect(()=>{
        
    },[])

    return (
        <div className='box bg-white dark:bg-black-box text-black-body dark:text-white-body'>
            <p className='pb-4 font-bold text-base'>Repayment Schedule</p>
            {isFetching ? 
            <>
                <p className='text-slate-800'>Loading...</p>
            </>
            : isError ?
                <p className='text-red-500'>{error.message}</p>
            :
                <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">
                                ID/TrxID
                            </th>
                            {/* <th scope="col" className="px-2">
                                Loan
                            </th> */}
                            <th scope="col" className="px-2 text-right">
                                Repay Amount
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Install Amount
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Due Date
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Paid Date
                            </th>
                            <th scope="col" className="px-2 text-right">
                                Paid ?
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {(repaymentSchedule && repaymentSchedule.length > 0) ? repaymentSchedule?.map((item, index) => (
                            <tr key={index} className="py-2 border-t border-dashed border-slate-300">
                                <td className="px-2 py-2">
                                    <div className="text-left">
                                        <div className="text-base font-semibold">{item?.loan_id || ''}</div>
                                        <div className="text-base font-semibold">{item?.transaction_id || ''}</div>
                                    </div>
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="font-normal text-gray-500">{formatNumber(item?.total_repayment_amount)}</div>
                                    </div> 
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <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?.due_date)}</div>
                                    </div> 
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="font-normal text-gray-500">{item?.paid_at ? getDateFromDateString(item?.paid_at) : 'Not available'}</div>
                                    </div>
                                </td>
                                <td className="px-2">
                                    <div className="text-right">
                                        <div className="text-base font-semibold">{item?.paid || 'false'}</div>
                                    </div>
                                </td>
                            </tr>
                        ))
                        :
                        <tr className="py-2 border-t border-dashed border-slate-300">
                            <td className="px-3 py-2" colSpan={6}>
                                <div className="flex justify-center items-center">
                                    No Record Found
                                </div>
                            </td>
                        </tr>
                        }
                    </tbody>
                </table>
            }
        </div>
    )
}