import { type FC } from 'react'
import PageTitle from '../../components/PageTitle'
import ModalCreateReturnReason from './components/ModalCreateReturnReason'
import { useGetReturnReasons, useDeleteReturnReason } from './hooks/useOrderData'
import { type ReturnReasonType } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import Td from '../../components/Td'
import TrashWithConfrim from '../../components/TrashWithConfrim'

const ReturnCategories: FC = () => {

    const { data: returnReasonsData, isLoading, error, refetch } = useGetReturnReasons()
    const deleteReturnReasonMutation = useDeleteReturnReason();

    const handleDeleteReturnReason = async (reasonId: string) => {
        await deleteReturnReasonMutation.mutateAsync(reasonId);
        refetch()
    };

    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 returnReasons = returnReasonsData?.results?.reasons || [];

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <ModalCreateReturnReason />
            </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={'عملیات'} />
                        </tr>
                    </thead>
                    <tbody>
                        {returnReasons.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={4} className="text-center py-8 text-gray-500">
                                    هیچ دلیل بازگشتی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            returnReasons.map((reason: ReturnReasonType) => (
                                <tr key={reason._id} className='tr'>
                                    <Td text={reason.title} />
                                    <Td text={reason.fineRule?.toString() || 'بدون جریمه'} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <span className="px-2 py-1 rounded-full text-xs bg-green-100 text-green-800">
                                                فعال
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteReturnReason(reason._id)}
                                                isLoading={deleteReturnReasonMutation.isPending}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>
        </div>
    )
}

export default ReturnCategories
