import { type FC } from 'react'
import PageTitle from '../../components/PageTitle'
import ModalCreateCancelReason from './components/ModalCreateCancelReason'
import { useGetCancelReasons, useDeleteCancelReason } from './hooks/useOrderData'
import { type CancelReasonType } from './types/Types'
import PageLoading from '../../components/PageLoading'
import Error from '../../components/Error'
import Td from '../../components/Td'
import TrashWithConfrim from '../../components/TrashWithConfrim'

const CancellationCategories: FC = () => {

    const { data: cancelReasonsData, isLoading, error, refetch } = useGetCancelReasons()
    const deleteCancelReasonMutation = useDeleteCancelReason();

    const handleDeleteCancelReason = async (reasonId: string) => {
        await deleteCancelReasonMutation.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 cancelReasons = cancelReasonsData?.results?.reasons || [];

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <ModalCreateCancelReason />
            </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>
                        {cancelReasons.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={4} className="text-center py-8 text-gray-500">
                                    هیچ دلیل کنسلی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            cancelReasons.map((reason: CancelReasonType) => (
                                <tr key={reason._id} className='tr'>
                                    <Td text={reason.title} />
                                    <Td text={reason.fineRule ? `${reason.fineRule.title} (${reason.fineRule.fine_percentage}%)` : 'بدون جریمه'} />
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <span className={`px-2 py-1 rounded-full text-xs ${reason.deleted ? 'bg-red-100 text-red-800' : 'bg-green-100 text-green-800'}`}>
                                                {reason.deleted ? 'حذف شده' : 'فعال'}
                                            </span>
                                        </div>
                                    </Td>
                                    <Td text="">
                                        <div className="flex items-center gap-2">
                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteCancelReason(reason._id)}
                                                isLoading={deleteCancelReasonMutation.isPending}
                                            />
                                        </div>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>
        </div>
    )
}

export default CancellationCategories