import { type FC, useState } from 'react'
import PageTitle from '../../components/PageTitle'
import { useGetContactUs } from './hooks/useContactUsData';
import { type ContactUsItem } from './types';
import PageLoading from '../../components/PageLoading';
import Error from '../../components/Error';
import PaginationUi from '../../components/PaginationUi';
import Td from '../../components/Td';
import { Eye } from 'iconsax-react';
import ContactUsMessageModal from '../../components/ContactUsMessageModal';

const ContactUsList: FC = () => {
    const [currentPage, setCurrentPage] = useState(1);
    const [selectedContactUs, setSelectedContactUs] = useState<ContactUsItem | null>(null);
    const [isModalOpen, setIsModalOpen] = useState(false);
    const { data: contactUsData, isLoading, error } = useGetContactUs(currentPage);

    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 contactUs = contactUsData?.results?.contactUs || [];
    const pager = contactUsData?.results?.pager;

    const truncateText = (text: string, maxLength: number = 100) => {
        return text.length > maxLength ? text.substring(0, maxLength) + '...' : text;
    };

    const handleViewMessage = (contactUs: ContactUsItem) => {
        setSelectedContactUs(contactUs);
        setIsModalOpen(true);
    };

    const handleCloseModal = () => {
        setIsModalOpen(false);
        setSelectedContactUs(null);
    };

    return (
        <div>
            <PageTitle />
            <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>
                        </tr>
                    </thead>
                    <tbody>
                        {contactUs.length === 0 ? (
                            <tr className='tr'>
                                <td colSpan={3} className="text-center py-8 text-gray-500">
                                    هیچ پیامی یافت نشد
                                </td>
                            </tr>
                        ) : (
                            contactUs.map((contact: ContactUsItem, index: number) => (
                                <tr key={index} className='tr'>
                                    <Td text={contact.fullName} />
                                    <Td text={contact.email_phone} />
                                    <Td text={truncateText(contact.message)} />
                                    <Td text=''>
                                        <button
                                            onClick={() => handleViewMessage(contact)}
                                            className="cursor-pointer hover:opacity-70 transition-opacity"
                                        >
                                            <Eye size={20} color='#8C90A3' />
                                        </button>
                                    </Td>
                                </tr>
                            ))
                        )}
                    </tbody>
                </table>
            </div>

            <PaginationUi
                pager={pager}
                onPageChange={(page) => {
                    setCurrentPage(page);
                }}
            />

            <ContactUsMessageModal
                open={isModalOpen}
                close={handleCloseModal}
                contactUs={selectedContactUs}
            />
        </div>
    )
}

export default ContactUsList