import { type FC, useState } from 'react'
import { useGetWithdrawalRequests } from './hooks/useSellerData'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import PaginationUi from '../../components/PaginationUi'
import Td from '../../components/Td'
import WithdrawalRequestTableRow from './components/WithdrawalRequestTableRow'
import WithdrawalRequestModal from './components/WithdrawalRequestModal'

interface WithdrawalRequestData {
    wallet: {
        _id: string;
        seller: {
            fullName: string;
            email?: string;
            phoneNumber: string;
        };
        balance: number;
    };
    withdrawals: unknown[];
    lastPaidAmount: number | null;
    lastPaidDate: string | null;
}

const WithdrawalRequest: FC = () => {

    const [currentPage, setCurrentPage] = useState(1);
    const [selectedRequest, setSelectedRequest] = useState<WithdrawalRequestData | null>(null);
    const [isModalOpen, setIsModalOpen] = useState(false);
    const { data: withdrawalData, isLoading, error } = useGetWithdrawalRequests(currentPage);

    if (isLoading) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <PageLoading />
            </div>
        );
    }

    if (error) {
        return (
            <div className="flex justify-center items-center min-h-[400px]">
                <Error errorText="خطا در بارگذاری درخواست‌های برداشت" />
            </div>
        );
    }

    const withdrawalRequests = withdrawalData?.results?.walletData || [];

    const handleViewDetails = (request: WithdrawalRequestData) => {
        setSelectedRequest(request);
        setIsModalOpen(true);
    };

    return (
        <div>
            <div className='relative overflow-x-auto rounded-3xl mt-5 w-full'>
                <table className='w-full text-sm'>
                    <thead className='thead'>
                        <tr>
                            <Td text={'نام و نام خانوادگی'} />
                            <Td text={'ایمیل'} />
                            <Td text={'شماره تلفن'} />
                            <Td text={'موجودی'} />
                            <Td text={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {withdrawalRequests.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={5} className="text-center py-8 text-gray-500">
                                    هیچ درخواست برداشتی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            withdrawalRequests.map((request: WithdrawalRequestData) => (
                                <WithdrawalRequestTableRow
                                    key={request.wallet._id}
                                    request={request}
                                    onViewDetails={handleViewDetails}
                                />
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <PaginationUi
                pager={{ page: currentPage, limit: 10, totalItems: withdrawalRequests.length, totalPages: 1, prevPage: null, nextPage: null }}
                onPageChange={(page) => {
                    setCurrentPage(page);
                }}
            />

            <WithdrawalRequestModal
                open={isModalOpen}
                close={() => setIsModalOpen(false)}
                request={selectedRequest}
            />
        </div>
    )
}

export default WithdrawalRequest