import { type FC } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetTicketCategories } from './hooks/useTicketData'
import { type ITicketCategory } from './types/Types'
import { Pages } from '../../config/Pages'
import Button from '../../components/Button'
import Td from '../../components/Td'
import { useNavigate } from 'react-router-dom'
import PageLoading from '../../components/PageLoading'

const Category: FC = () => {
    const navigate = useNavigate()
    const { data, isLoading } = useGetTicketCategories()
    // const deleteCategoryMutation = useDeleteTicketCategory()

    // const handleDeleteCategory = async (categoryId: string) => {
    //     await deleteCategoryMutation.mutateAsync(categoryId)
    //     refetch()
    // }

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

    const categories = data?.results?.categories || []

    return (
        <div>
            <PageTitle />
            <div className='flex justify-end mt-5'>
                <Button
                    label='افزودن دسته‌بندی'
                    onClick={() => navigate(Pages.ticket.category.create)}
                    className='w-fit'
                />
            </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>
                        {categories.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={5} className="text-center py-8 text-gray-500">
                                    هیچ دسته‌بندی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            categories.map((category: ITicketCategory) => (
                                <tr key={category._id} className='tr'>
                                    <Td text={category.title_fa} />
                                    <Td text={category.description} />
                                    <Td text={category.createdAt} />
                                    <Td text={category.updatedAt} />
                                    {/* <Td text="">
                                        <div className="flex items-center gap-2">
                                            <Link to={`${Pages.ticket.category.update}${category._id}`}>
                                                <Edit color='#8C90A3' size={16} className="cursor-pointer hover:text-blue-500" />
                                            </Link>
                                            <TrashWithConfrim
                                                onDelete={() => handleDeleteCategory(category._id)}
                                                isLoading={deleteCategoryMutation.isPending}
                                            />
                                        </div>
                                    </Td> */}
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>
        </div>
    )
}

export default Category