import { useEffect, useState, Fragment } from 'react';
import 'mantine-datatable/styles.css';
import { Dialog, Transition } from '@headlessui/react';
import AsyncSelect from 'react-select/async';
import { OptionsOrGroups } from 'react-select';
import { coloredToast } from '@/lib/toast';
import { logActivity } from '@/utils/activityLogger';
import NumberFormatter from '@/lib/ribuan';
import { DataTable } from '@/components/Shared/DataTable';

interface AffilatorCommission {
    affiliator: {
        personal_detail: {
            name: string;
            email: string;
        };
        _id: string;
        affiliate_code: string;
    };
    max_percentage: number;
    max_commission: number;
    is_active: boolean;
    _id: string;
}

const AffiliatorCommissions = () => {
    const [isMounted, setIsMounted] = useState(false);

    useEffect(() => {
        setIsMounted(true);
    }, []);

    const [records, setRecords] = useState<AffilatorCommission[]>([]);
    const [isLoading, setIsLoading] = useState(true);

    const [page, setPage] = useState(1);
    const PAGE_SIZES = [5, 10, 20, 30, 50, 100];
    const [pageSize, setPageSize] = useState(PAGE_SIZES[0]);
    const [totalPages, setTotalPages] = useState(0);
    const [search, setSearch] = useState('');

    const fetchData = async () => {
        setIsLoading(true);
        try {
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/setting/affiliate/affiliator-commission?page=${page}&per_page=${pageSize}&q=${search}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include',
            });
            const data = await response.json();
            setPage(data.currentPage);
            setTotalPages(data.totalPages);
            setRecords(data.data);

            setIsLoading(false);
        } catch (error) {
            console.error('Error fetching data:', error);
            setIsLoading(false);
        }
    };

    useEffect(() => {
        fetchData();
    }, [page, pageSize]);

    const [isModalOpen, setIsModalOpen] = useState(false);
    const [isModalOpenEdit, setIsModalOpenEdit] = useState(false);

    interface SelectedAffiliatorState {
        affiliate_id: string;
        email: string;
        max_percentage: number;
        max_commission: number;
    }

    const [selectedAffiliator, setSelectedAffiliator] = useState<SelectedAffiliatorState>({
        affiliate_id: '',
        email: '',
        max_commission: 0,
        max_percentage: 0,
    });

    const fetchEmailAffiliator = async (inputValue: string) => {
        try {
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/affiliate/affiliator?email=${inputValue}&status=approved`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include',
            });
            const { data } = await response.json();
            return data.map((item: any) => ({ value: item.id, label: item.personal_detail.email }));
        } catch (error) {
            console.error('Error fetching data:', error);
            return [];
        }
    };

    let debounceTimeout: any;
    const loadOptions = async (inputValue: string): Promise<OptionsOrGroups<any, any>> => {
        return new Promise<OptionsOrGroups<any, any>>((resolve) => {
            clearTimeout(debounceTimeout);

            if (inputValue.length >= 3) {
                debounceTimeout = setTimeout(async () => {
                    const options = await fetchEmailAffiliator(inputValue);
                    resolve(options);
                }, 1000);
            } else {
                resolve([]);
            }
        });
    };

    const describeChanges = (oldData: AffilatorCommission, newData: Partial<AffilatorCommission>) => {
        const changes: string[] = [];

        if (newData.max_commission !== undefined && oldData.max_commission !== newData.max_commission) {
            changes.push(`Max Commission: ${oldData.max_commission} -> ${newData.max_commission}`);
        }

        if (newData.max_percentage !== undefined && oldData.max_percentage !== newData.max_percentage) {
            changes.push(`Max Percentage: ${oldData.max_percentage}% -> ${newData.max_percentage}%`);
        }

        if (newData.is_active !== undefined && oldData.is_active !== newData.is_active) {
            changes.push(`Is Active: ${oldData.is_active} -> ${newData.is_active}`);
        }

        return changes.join(' | ') || 'No changes made';
    };

    const handleAddCommission = async (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        const newCommission = {
            affiliate_id: selectedAffiliator.affiliate_id,
            max_percentage: selectedAffiliator.max_percentage,
            max_commission: selectedAffiliator.max_commission,
            is_active: true,
        };

        try {
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/setting/affiliate/affiliator-commission/add`, {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include',
                body: JSON.stringify(newCommission),
            });
            const data = await response.json();
            if (!response.ok) {
                throw new Error(data.message || 'Error adding commission');
            }
            setSelectedAffiliator({
                affiliate_id: '',
                email: '',
                max_percentage: 0,
                max_commission: 0,
            });
            setIsModalOpen(false);
            coloredToast('success', 'Commission added successfully');
            await logActivity({
                location: 'Affiliate Setting',
                description: `Added Affiliator: ${newCommission.affiliate_id} | Max Commission: ${newCommission.max_commission} | Max Percentage: ${newCommission.max_percentage}% | Is Active: true`,
            });
            fetchData();
        } catch (error) {
            console.error('Error adding commission:', error);
            coloredToast('danger', error instanceof Error ? error.message : 'Error adding commission');
        }
    };

    const updateAffiliatorCommission = async (commissionData: { affiliate_id: string; max_commission?: number; max_percentage?: number; is_active?: boolean }) => {
        try {
            const old = records.find((r) => r._id === commissionData.affiliate_id);
            if (!old) return;
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/setting/affiliate/affiliator-commission/${commissionData.affiliate_id}/update`, {
                method: 'PATCH',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include',
                body: JSON.stringify(commissionData),
            });
            const data = await response.json();
            if (!response.ok) {
                throw new Error(response.statusText);
            }
            const changeDescription = describeChanges(old, commissionData);
            await logActivity({
                location: 'Affiliate Setting',
                description: `Edited Affiliator (${old.affiliator.personal_detail.email}): ${changeDescription}`,
            });
            setIsModalOpenEdit(false);
            fetchData();
            coloredToast('success', 'Commission updated successfully');
        } catch (error) {
            console.error('Error updating commission:', error);
            coloredToast('danger', 'Error updating commission');
        }
    };

    const deleteAffiliatorCommission = async (affiliate_id: string) => {
        try {
            const target = records.find((r) => r._id === affiliate_id);
            const email = target?.affiliator?.personal_detail?.email || affiliate_id;
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/setting/affiliate/affiliator-commission/${affiliate_id}/delete`, {
                method: 'DELETE',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include',
            });
            const data = await response.json();
            if (!response.ok) {
                throw new Error(response.statusText);
            }
            await logActivity({
                location: 'Affiliate Setting',
                description: `Deleted Affiliator Commission: ${email}`,
            });
            fetchData();
            coloredToast('success', 'Commission deleted successfully');
        } catch (error) {
            console.error('Error deleting commission:', error);
            coloredToast('danger', 'Error deleting commission');
        }
    };

    const toggleIsActive = async (affiliate_id: string, is_active: boolean) => {
        updateAffiliatorCommission({ affiliate_id, is_active: !is_active });
    };

    const [selectedAffiliatorEdit, setSelectedAffiliatorEdit] = useState<SelectedAffiliatorState>({
        affiliate_id: '',
        email: '',
        max_percentage: 0,
        max_commission: 0,
    });
    const handleEditCommission = async (event: React.FormEvent<HTMLFormElement>) => {
        event.preventDefault();
        updateAffiliatorCommission({ affiliate_id: selectedAffiliatorEdit.affiliate_id, max_commission: selectedAffiliatorEdit.max_commission, max_percentage: selectedAffiliatorEdit.max_percentage });
    };
    const handleKeypress = (event: React.KeyboardEvent<HTMLInputElement>) => {
        if (event.keyCode === 13) {
            if (page !== 1) {
                setPage(1);
            } else {
                fetchData();
            }
        }
    };
    return (
        <>
            {isMounted && (
                <>
                    <div className="panel mx-1 border-white-light px-0 dark:border-[#1b2e4b]">
                        <div className="user-table">
                            <div className="mb-4.5 flex flex-col gap-5 px-5 md:flex-row md:items-center">
                                <div className="flex cursor-pointer items-center gap-2">
                                    <div className="btn btn-outline-primary gap-2" onClick={() => setIsModalOpen(true)}>
                                        <svg className="h-5 w-5" viewBox="0 0 24 24" stroke="currentColor" strokeWidth="1.5" fill="none" strokeLinecap="round" strokeLinejoin="round">
                                            <line x1="12" y1="5" x2="12" y2="19"></line>
                                            <line x1="5" y1="12" x2="19" y2="12"></line>
                                        </svg>
                                        Add New Commission
                                    </div>
                                </div>
                                <div className="flex ltr:ml-auto rtl:mr-auto">
                                    <input
                                        type="text"
                                        className="form-input w-auto"
                                        placeholder="Search Affiliator"
                                        value={search}
                                        onChange={(e) => setSearch(e.target.value)}
                                        onKeyDown={handleKeypress}
                                    />
                                </div>
                            </div>

                            {/* Modal Add Affiliate Setting  */}

                            <Transition appear show={isModalOpen} as={Fragment}>
                                <Dialog as="div" className="relative z-10" onClose={() => setIsModalOpen(false)}>
                                    <Transition.Child
                                        as={Fragment}
                                        enter="ease-out duration-300"
                                        enterFrom="opacity-0"
                                        enterTo="opacity-100"
                                        leave="ease-in duration-200"
                                        leaveFrom="opacity-100"
                                        leaveTo="opacity-0"
                                    >
                                        <div className="fixed inset-0 bg-black bg-opacity-25" />
                                    </Transition.Child>

                                    <div className="fixed inset-0 overflow-y-auto">
                                        <div className="flex min-h-full items-center justify-center p-4 text-center">
                                            <Transition.Child
                                                as={Fragment}
                                                enter="ease-out duration-300"
                                                enterFrom="opacity-0 scale-95"
                                                enterTo="opacity-100 scale-100"
                                                leave="ease-in duration-200"
                                                leaveFrom="opacity-100 scale-100"
                                                leaveTo="opacity-0 scale-95"
                                            >
                                                <Dialog.Panel className="panel w-1/3  overflow-hidden rounded-lg  p-6 text-left text-black dark:text-white-dark ">
                                                    <Dialog.Title as="h3" className="mb-4 text-lg font-medium leading-6 text-black dark:text-white-dark">
                                                        Add New Affiliator Commission
                                                    </Dialog.Title>
                                                    <form onSubmit={handleAddCommission}>
                                                        <div className="custom-select mt-2">
                                                            <AsyncSelect
                                                                loadOptions={loadOptions}
                                                                value={selectedAffiliator.email ? { label: selectedAffiliator.email, value: selectedAffiliator.affiliate_id } : null}
                                                                onChange={(selectedItem: any) => {
                                                                    if (selectedItem) {
                                                                        setSelectedAffiliator((prevState: SelectedAffiliatorState) => ({
                                                                            ...prevState,
                                                                            email: selectedItem.label,
                                                                            affiliate_id: selectedItem.value,
                                                                        }));
                                                                        console.log(selectedAffiliator);
                                                                    }
                                                                }}
                                                                placeholder="Search by email..."
                                                            />
                                                        </div>
                                                        <div className="mt-4">
                                                            <label htmlFor="max_percentage" className="text-sm text-gray-500">
                                                                Maximum Percentage
                                                            </label>
                                                            <input
                                                                type="number"
                                                                id="max_percentage"
                                                                name="max_percentage"
                                                                className="form-input"
                                                                required
                                                                min="0"
                                                                max="100"
                                                                value={selectedAffiliator.max_percentage}
                                                                onChange={(e) => {
                                                                    const value = Math.min(100, Math.max(0, parseFloat(e.target.value)));
                                                                    setSelectedAffiliator((prevState: SelectedAffiliatorState) => ({ ...prevState, max_percentage: value }));
                                                                }}
                                                            />
                                                        </div>
                                                        <div className="mt-4">
                                                            <label htmlFor="max_commission" className="text-sm text-gray-500">
                                                                Maximum Commission
                                                            </label>
                                                            <NumberFormatter
                                                                id="max_commission"
                                                                name="max_commission"
                                                                value={selectedAffiliator.max_commission}
                                                                onChange={(value: number) => setSelectedAffiliator((prevState: SelectedAffiliatorState) => ({ ...prevState, max_commission: value }))}
                                                                className="form-input"
                                                            />
                                                        </div>
                                                        <div className="mt-4">
                                                            <button type="submit" className="btn btn-outline-primary gap-2">
                                                                Submit Commission
                                                            </button>
                                                        </div>
                                                    </form>
                                                </Dialog.Panel>
                                            </Transition.Child>
                                        </div>
                                    </div>
                                </Dialog>
                            </Transition>

                            {/* Modal Edit Affiliate Setting  */}

                            <Transition appear show={isModalOpenEdit} as={Fragment}>
                                <Dialog as="div" className="relative z-10" onClose={() => setIsModalOpenEdit(false)}>
                                    <Transition.Child
                                        as={Fragment}
                                        enter="ease-out duration-300"
                                        enterFrom="opacity-0"
                                        enterTo="opacity-100"
                                        leave="ease-in duration-200"
                                        leaveFrom="opacity-100"
                                        leaveTo="opacity-0"
                                    >
                                        <div className="fixed inset-0 bg-black bg-opacity-25" />
                                    </Transition.Child>

                                    <div className="fixed inset-0 overflow-y-auto">
                                        <div className="flex min-h-full items-center justify-center p-4 text-center">
                                            <Transition.Child
                                                as={Fragment}
                                                enter="ease-out duration-300"
                                                enterFrom="opacity-0 scale-95"
                                                enterTo="opacity-100 scale-100"
                                                leave="ease-in duration-200"
                                                leaveFrom="opacity-100 scale-100"
                                                leaveTo="opacity-0 scale-95"
                                            >
                                                <Dialog.Panel className="panel w-1/3  overflow-hidden rounded-lg  p-6 text-left text-black dark:text-white-dark ">
                                                    <Dialog.Title as="h3" className="mb-4 text-lg font-medium leading-6 text-black dark:text-white-dark">
                                                        Edit Affiliator Commission
                                                    </Dialog.Title>
                                                    <form onSubmit={handleEditCommission}>
                                                        <div className="custom-select mt-2">
                                                            <input type="text" value={selectedAffiliatorEdit.email || ''} readOnly className="form-input" placeholder="Email" />
                                                        </div>
                                                        <div className="mt-4">
                                                            <label htmlFor="max_percentage" className="text-sm text-gray-500">
                                                                Maximum Percentage
                                                            </label>
                                                            <input
                                                                type="number"
                                                                id="max_percentage"
                                                                name="max_percentage"
                                                                className="form-input"
                                                                required
                                                                min="0"
                                                                max="100"
                                                                value={selectedAffiliatorEdit.max_percentage}
                                                                onChange={(e) => {
                                                                    const value = Math.min(100, Math.max(0, parseFloat(e.target.value)));
                                                                    setSelectedAffiliatorEdit((prevState: SelectedAffiliatorState) => ({ ...prevState, max_percentage: value }));
                                                                }}
                                                            />
                                                        </div>
                                                        <div className="mt-4">
                                                            <label htmlFor="max_commission" className="text-sm text-gray-500">
                                                                Maximum Commission
                                                            </label>
                                                            <NumberFormatter
                                                                id="max_commission"
                                                                name="max_commission"
                                                                value={selectedAffiliatorEdit.max_commission}
                                                                onChange={(value: number) =>
                                                                    setSelectedAffiliatorEdit((prevState: SelectedAffiliatorState) => ({ ...prevState, max_commission: value }))
                                                                }
                                                                className="form-input"
                                                            />
                                                        </div>
                                                        <div className="mt-4">
                                                            <button type="submit" className="btn btn-outline-primary gap-2">
                                                                Update Commission
                                                            </button>
                                                        </div>
                                                    </form>
                                                </Dialog.Panel>
                                            </Transition.Child>
                                        </div>
                                    </div>
                                </Dialog>
                            </Transition>
                            <div className="datatables pagination-padding">
                                {isLoading ? (
                                    <div className="flex justify-center py-4 text-center">
                                        <svg
                                            viewBox="0 0 24 24"
                                            width="24"
                                            height="24"
                                            stroke="currentColor"
                                            strokeWidth="1.5"
                                            fill="none"
                                            strokeLinecap="round"
                                            strokeLinejoin="round"
                                            className="h-5 w-5 animate-spin text-white-dark"
                                        >
                                            <line x1="12" y1="2" x2="12" y2="6"></line>
                                            <line x1="12" y1="18" x2="12" y2="22"></line>
                                            <line x1="4.93" y1="4.93" x2="7.76" y2="7.76"></line>
                                            <line x1="16.24" y1="16.24" x2="19.07" y2="19.07"></line>
                                            <line x1="2" y1="12" x2="6" y2="12"></line>
                                            <line x1="18" y1="12" x2="22" y2="12"></line>
                                            <line x1="4.93" y1="19.07" x2="7.76" y2="16.24"></line>
                                            <line x1="16.24" y1="7.76" x2="19.07" y2="4.93"></line>
                                        </svg>
                                        <span className="ms-3">Please wait, we&apos;re still working to load the data...</span>
                                    </div>
                                ) : (
                                    <DataTable
                                        className="table-hover whitespace-nowrap"
                                        records={records}
                                        minHeight={250}
                                        noRecordsText="No data found"
                                        columns={[
                                            {
                                                accessor: 'affiliator.personal_detail.email',
                                                title: 'Affiliator',
                                                render: ({ affiliator }) => (
                                                    <>
                                                        <div className="font-semibold hover:no-underline">{affiliator?.personal_detail?.name}</div>
                                                        <div className="text-sm text-gray-500">({affiliator?.personal_detail?.email})</div>
                                                        <div className="text-sm text-gray-500">{affiliator?.affiliate_code}</div>
                                                    </>
                                                ),
                                            },

                                            {
                                                accessor: 'is_active',
                                                title: 'Is Active',
                                                render: ({ is_active, _id }) => (
                                                    <label className="relative h-6 w-12">
                                                        <input
                                                            type="checkbox"
                                                            checked={is_active}
                                                            onChange={() => toggleIsActive(_id, is_active)}
                                                            className="custom_switch peer absolute z-10 h-full w-full cursor-pointer opacity-0"
                                                        />
                                                        <span className="block h-full rounded-full bg-[#ebedf2] before:absolute before:bottom-1 before:left-1 before:h-4 before:w-4 before:rounded-full before:bg-white before:transition-all before:duration-300 peer-checked:bg-primary peer-checked:before:left-7 dark:bg-dark dark:before:bg-white-dark dark:peer-checked:before:bg-white"></span>
                                                    </label>
                                                ),
                                            },
                                            {
                                                accessor: 'max_percentage',
                                                render: ({ max_percentage }) => <div className="font-semibold">{max_percentage}%</div>,
                                            },
                                            {
                                                accessor: 'max_commission',
                                                render: ({ max_commission }) => (
                                                    <div className="font-semibold">
                                                        {new Intl.NumberFormat('id-ID', { style: 'currency', currency: 'IDR', minimumFractionDigits: 0, maximumFractionDigits: 0 }).format(
                                                            max_commission
                                                        )}
                                                    </div>
                                                ),
                                            },

                                            {
                                                accessor: 'action',
                                                title: 'Actions',
                                                textAlign: 'center',
                                                render: ({ affiliator, _id, max_commission, max_percentage }) => (
                                                    <div className="mx-auto flex w-max  items-center gap-4">
                                                        <div
                                                            className="cursor-pointer"
                                                            onClick={() => {
                                                                setIsModalOpenEdit(true);
                                                                setSelectedAffiliatorEdit({
                                                                    affiliate_id: _id,
                                                                    email: affiliator.personal_detail.email,
                                                                    max_commission: max_commission,
                                                                    max_percentage: max_percentage,
                                                                });
                                                            }}
                                                        >
                                                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" className="h-4.5 w-4.5">
                                                                <path
                                                                    opacity="0.5"
                                                                    d="M22 10.5V12C22 16.714 22 19.0711 20.5355 20.5355C19.0711 22 16.714 22 12 22C7.28595 22 4.92893 22 3.46447 20.5355C2 19.0711 2 16.714 2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2H13.5"
                                                                    stroke="currentColor"
                                                                    strokeWidth="1.5"
                                                                    strokeLinecap="round"
                                                                ></path>
                                                                <path
                                                                    d="M17.3009 2.80624L16.652 3.45506L10.6872 9.41993C10.2832 9.82394 10.0812 10.0259 9.90743 10.2487C9.70249 10.5114 9.52679 10.7957 9.38344 11.0965C9.26191 11.3515 9.17157 11.6225 8.99089 12.1646L8.41242 13.9L8.03811 15.0229C7.9492 15.2897 8.01862 15.5837 8.21744 15.7826C8.41626 15.9814 8.71035 16.0508 8.97709 15.9619L10.1 15.5876L11.8354 15.0091C12.3775 14.8284 12.6485 14.7381 12.9035 14.6166C13.2043 14.4732 13.4886 14.2975 13.7513 14.0926C13.9741 13.9188 14.1761 13.7168 14.5801 13.3128L20.5449 7.34795L21.1938 6.69914C22.2687 5.62415 22.2687 3.88124 21.1938 2.80624C20.1188 1.73125 18.3759 1.73125 17.3009 2.80624Z"
                                                                    stroke="currentColor"
                                                                    strokeWidth="1.5"
                                                                ></path>
                                                                <path
                                                                    opacity="0.5"
                                                                    d="M16.6522 3.45508C16.6522 3.45508 16.7333 4.83381 17.9499 6.05034C19.1664 7.26687 20.5451 7.34797 20.5451 7.34797M10.1002 15.5876L8.4126 13.9"
                                                                    stroke="currentColor"
                                                                    strokeWidth="1.5"
                                                                ></path>
                                                            </svg>
                                                        </div>
                                                        <button
                                                            type="button"
                                                            className="flex hover:text-danger"
                                                            onClick={() => {
                                                                if (window.confirm('Are you sure you want to delete this commission?')) {
                                                                    deleteAffiliatorCommission(_id);
                                                                }
                                                            }}
                                                        >
                                                            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" className="h-5 w-5">
                                                                <path d="M20.5001 6H3.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"></path>
                                                                <path
                                                                    d="M18.8334 8.5L18.3735 15.3991C18.1965 18.054 18.108 19.3815 17.243 20.1907C16.378 21 15.0476 21 12.3868 21H11.6134C8.9526 21 7.6222 21 6.75719 20.1907C5.89218 19.3815 5.80368 18.054 5.62669 15.3991L5.16675 8.5"
                                                                    stroke="currentColor"
                                                                    strokeWidth="1.5"
                                                                    strokeLinecap="round"
                                                                ></path>
                                                                <path opacity="0.5" d="M9.5 11L10 16" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"></path>
                                                                <path opacity="0.5" d="M14.5 11L14 16" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"></path>
                                                                <path
                                                                    opacity="0.5"
                                                                    d="M6.5 6C6.55588 6 6.58382 6 6.60915 5.99936C7.43259 5.97849 8.15902 5.45491 8.43922 4.68032C8.44784 4.65649 8.45667 4.62999 8.47434 4.57697L8.57143 4.28571C8.65431 4.03708 8.69575 3.91276 8.75071 3.8072C8.97001 3.38607 9.37574 3.09364 9.84461 3.01877C9.96213 3 10.0932 3 10.3553 3H13.6447C13.9068 3 14.0379 3 14.1554 3.01877C14.6243 3.09364 15.03 3.38607 15.2493 3.8072C15.3043 3.91276 15.3457 4.03708 15.4286 4.28571L15.5257 4.57697C15.5433 4.62992 15.5522 4.65651 15.5608 4.68032C15.841 5.45491 16.5674 5.97849 17.3909 5.99936C17.4162 6 17.4441 6 17.5 6"
                                                                    stroke="currentColor"
                                                                    strokeWidth="1.5"
                                                                ></path>
                                                            </svg>
                                                        </button>
                                                    </div>
                                                ),
                                            },
                                        ]}
                                        highlightOnHover
                                        page={page}
                                        totalRecords={totalPages}
                                        recordsPerPage={pageSize}
                                        onPageChange={(p) => setPage(p)}
                                        recordsPerPageOptions={PAGE_SIZES}
                                        onRecordsPerPageChange={setPageSize}
                                        paginationText={({ from, to, totalRecords }) => `Showing  ${from} to ${to} of ${totalRecords} Affiliator`}
                                    />
                                )}
                            </div>
                        </div>
                    </div>
                </>
            )}
        </>
    );
};

export default AffiliatorCommissions;
