import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import Select from '../../components/Select'
import Input from '../../components/Input'
import Td from '../../components/Td'
import CreateCategory from './components/CreateCategory'
import PageLoading from '../../components/PageLoading'
import { ServiceCategoryType } from './types/ServiceTypes'
import { useDeleteCategory, useGetAllCategory } from './hooks/useServiceData'
import StatusCategory from './components/StatusCategory'
import Pagination from '../../components/Pagination'
import UpdateCategory from './components/UpdateCategory'
import { Trash } from 'iconsax-react'


const Category: FC = () => {

    const { t } = useTranslation('global')
    const [search, setSearch] = useState<string>('')
    const [page, setPage] = useState<number>(1)
    const [isActive, setIsActive] = useState<'active' | 'deactive' | ''>('')
    const getCategory = useGetAllCategory(page, search, isActive)
    const deleteCategory = useDeleteCategory()

    const handleDelete = (id: string) => {
        deleteCategory.mutate(id, {
            onSuccess: () => {
                getCategory.refetch()
            }
        })
    }

    return (
        <div className='mt-4'>
            <div>
                {t('service.cateory_services')}
            </div>

            <div className='flex gap-6 mt-9'>
                <div className='flex-1'>
                    <div className='flex justify-between items-center'>
                        <div className='w-32'>
                            <Select
                                items={[
                                    { label: t('all'), value: '' },
                                    { label: t('service.active'), value: 'active' },
                                    { label: t('service.deactive'), value: 'deactive' },
                                ]}
                                placeholder={t('status')}
                                onChange={(e) => setIsActive(e.target.value as 'active' | 'deactive' | '')}
                            />
                        </div>

                        <div className='w-40'>
                            <Input
                                variant='search'
                                placeholder={t('search')}
                                className='bg-white'
                                onChangeSearchFinal={(value) => setSearch(value)}
                            />
                        </div>
                    </div>
                    <div className='flex-1 mt-8 bg-white py-2 xl:px-10 px-5 rounded-3xl'>
                        {
                            getCategory.isPending ?
                                <div className='mt-4'>
                                    <PageLoading />
                                </div>
                                :
                                <div className='relative overflow-x-auto rounded-3xl w-full'>
                                    <table className='w-full text-sm '>
                                        <thead className='thead'>
                                            <tr>
                                                <Td text='' />
                                                <Td text={t('service.title')} />
                                                <Td text={t('priority')} />
                                                <Td text={t('service.parent_category')} />
                                                <Td text={t('service.count_sub_category')} />
                                                <Td text={t('service.count_service')} />
                                                <Td text={t('ticket.status')} />
                                                <Td text={''} />
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {
                                                getCategory.data?.data?.categories?.map((item: ServiceCategoryType) => {
                                                    return (
                                                        <tr key={item.id} className='tr'>
                                                            <Td text=''>
                                                                <img src={item.icon} className='size-10 min-w-10 rounded-lg' />
                                                            </Td>
                                                            <Td text={item.title} />
                                                            <Td text={item.order} />
                                                            <Td text={item.parent ? item.parent.title : '-'} />
                                                            <Td text={item.childrenCount + ''} />
                                                            <Td text={item.servicesCount + ''} />
                                                            <Td text={''}>
                                                                <StatusCategory
                                                                    defaultActive={item.isActive}
                                                                    id={item.id}
                                                                />
                                                            </Td>
                                                            <Td text={''}>
                                                                <div className='flex gap-2'>
                                                                    <UpdateCategory refetch={() => getCategory.refetch()} id={item.id} />
                                                                    <Trash
                                                                        size={20}
                                                                        color='#888888'
                                                                        onClick={() => handleDelete(item.id)}
                                                                    />
                                                                </div>
                                                            </Td>
                                                        </tr>
                                                    )
                                                })
                                            }
                                        </tbody>
                                    </table>
                                    <div className='flex justify-end pb-3'>
                                        <Pagination
                                            currentPage={page}
                                            totalPages={getCategory.data?.data?.pager?.totalPages}
                                            onPageChange={setPage}
                                        />
                                    </div>
                                </div>
                        }


                    </div>
                </div>

                <CreateCategory />
            </div>
        </div>
    )
}

export default Category