import { type FC } from 'react'
import Table from '@/components/Table'
import Filters from '@/components/Filters'
import { Add } from 'iconsax-react'
import Button from '@/components/Button'
import { useGetRestaurantPaymentMethods, useDeleteRestaurantPaymentMethod } from './hooks/usePaymentMethodData'
import { usePaymentMethodFilters } from './hooks/usePaymentMethodFilters'
import { getPaymentMethodTableColumns } from './components/PaymentMethodTableColumns'
import { usePaymentMethodFiltersFields } from './components/PaymentMethodFiltersFields'
import { Link } from 'react-router-dom'
import { Pages } from '@/config/Pages'

const PaymentMethodsList: FC = () => {
    const {
        filters,
        currentPage,
        apiParams,
        handleFiltersChange,
        handlePageChange,
        limit,
    } = usePaymentMethodFilters()

    const { data: restaurantPaymentMethodsData, isLoading } = useGetRestaurantPaymentMethods(apiParams)
    const { mutate: deletePaymentMethod, isPending: isDeleting } = useDeleteRestaurantPaymentMethod()

    const paymentMethods = restaurantPaymentMethodsData?.data || []
    const columns = getPaymentMethodTableColumns({
        onDelete: (id: string) => deletePaymentMethod(id),
        isDeleting
    })
    const filterFields = usePaymentMethodFiltersFields()

    const totalPages = Math.ceil(paymentMethods.length / limit) || 1

    return (
        <div className='mt-5'>
            <div className='flex justify-between items-center'>
                <h1 className='text-lg font-light'>لیست روش های پرداخت</h1>
                <Link to={Pages.payment_methods.add}>
                    <Button className='w-fit px-6'>
                        <div className='flex gap-2 items-center'>
                            <Add color='#fff' size={20} />
                            <span>افزودن روش پرداخت</span>
                        </div>
                    </Button>
                </Link>
            </div>

            <div className='mt-8'>
                <Filters
                    fields={filterFields}
                    onChange={handleFiltersChange}
                    initialValues={filters}
                    searchField="search"
                />
            </div>

            <Table
                columns={columns}
                data={paymentMethods}
                isloading={isLoading}
                pagination={{
                    currentPage,
                    totalPages,
                    onPageChange: handlePageChange,
                }}
            />
        </div>
    )
}

export default PaymentMethodsList