import { FC, useState } from 'react'
import { useTranslation } from 'react-i18next'
import DefaulModal from '../../../../components/DefaulModal'
import { useDeleteRestaurantAdmin, useGetRestaurantAdmins } from '../../hooks/useIconData'
import PageLoading from '../../../../components/PageLoading'
import Td from '../../../../components/Td'
import { RestaurantAdminType } from '../../types/Types'
import moment from 'moment-jalaali'
import TrashWithConfrim from '../../../../components/TrashWithConfrim'
import { toast } from '../../../../components/Toast';
import { ErrorType } from '../../../../helpers/types'
import Button from '../../../../components/Button'
import AddRestaurantAdminForm from './AddRestaurantAdminForm'

interface RestaurantAdminsModalProps {
    open: boolean
    close: () => void
    restaurantId: string
}

const RestaurantAdminsModal: FC<RestaurantAdminsModalProps> = ({ open, close, restaurantId }) => {
    const { t } = useTranslation('global')
    const { data: adminsData, isLoading, refetch } = useGetRestaurantAdmins(restaurantId)
    const [showAddForm, setShowAddForm] = useState(false)

    const adminsList = adminsData?.data || []

    const { mutate: deleteAdmin, isPending } = useDeleteRestaurantAdmin()

    const handleDeleteAdmin = (restaurantId: string, adminId: string) => {
        deleteAdmin({ restaurantId, adminId }, {
            onSuccess: () => {
                refetch()
                toast(t('success'), 'success')
            },
            onError: (error: ErrorType) => {
                toast(error.response?.data?.error?.message[0], 'error')
            }
        })
    }

    const handleAddAdmin = () => {
        setShowAddForm(true)
    }

    const handleFormSuccess = () => {
        refetch()
        setShowAddForm(false)
    }

    const handleFormClose = () => {
        setShowAddForm(false)
    }
    return (
        <DefaulModal
            open={open}
            close={close}
            title_header={t('restaurant.admins')}
            isHeader
        >
            <div className='mt-6'>
                {showAddForm ? (
                    <AddRestaurantAdminForm
                        restaurantId={restaurantId}
                        onSuccess={handleFormSuccess}
                        onClose={handleFormClose}
                    />
                ) : (
                    <>
                        <div className='flex justify-end'>
                            <Button
                                onClick={handleAddAdmin}
                                className='w-fit px-5'
                            >
                                {t('restaurant.add_admin')}
                            </Button>
                        </div>
                        {
                            isLoading ?
                                <PageLoading />
                                :
                                <div className='relative overflow-x-auto rounded-3xl w-full mt-6'>
                                    <table className='w-full text-sm'>
                                        <thead className='thead'>
                                            <tr>
                                                <Td text={t('user.name')} />
                                                <Td text={t('restaurant.phone')} />
                                                <Td text={t('restaurant.role')} />
                                                <Td text={t('restaurant.created_at')} />
                                                <Td text={''} />
                                            </tr>
                                        </thead>
                                        <tbody>
                                            {
                                                adminsList.map((admin: RestaurantAdminType) => {
                                                    return (
                                                        <tr key={admin.id} className='tr'>
                                                            <Td text={`${admin.firstName} ${admin.lastName}`} />
                                                            <Td text={admin.phone || '-'} />
                                                            <Td text={admin.roles[0].role?.name || '-'} />
                                                            <Td text={''}>
                                                                <div className='dltr text-right'>
                                                                    {moment(admin.createdAt).format('jYYYY-jMM-jDD HH:mm')}
                                                                </div>
                                                            </Td>
                                                            <Td text=''>
                                                                <div className='flex gap-2'>
                                                                    <TrashWithConfrim
                                                                        onDelete={() => handleDeleteAdmin(restaurantId, admin.id)}
                                                                        isLoading={isPending}
                                                                    />
                                                                </div>
                                                            </Td>
                                                        </tr>
                                                    )
                                                })
                                            }
                                        </tbody>
                                    </table>
                                </div>
                        }
                    </>
                )}
            </div>
        </DefaulModal>
    )
}

export default RestaurantAdminsModal

