import { useEffect, Fragment, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import 'mantine-datatable/styles.css';
import { Dialog, Transition } from '@headlessui/react';
import { setPageTitle } from '../../../store/themeConfigSlice';
import Link from 'next/link';
import { coloredToast } from '@/lib/toast';
import Tippy from '@tippyjs/react';
import 'tippy.js/dist/tippy.css';
import { logActivity } from '@/utils/activityLogger';
import { DataTable } from '@/components/Shared/DataTable';
// import { type } from 'os';
// import async from 'react-select/dist/declarations/src/async/index';

interface Attribute {
    id: string;
    name: string;
    type: string;
    values: [];
    // temp_select: string;
}
const Attribute = () => {
    const dispatch = useDispatch();
    useEffect(() => {
        dispatch(setPageTitle('Attribute'));
    }, []);

    const [addAttributeModal, setAddAttributeModal] = useState<any>(false);
    const [addAttributeValueModal, setAddAttributeValueModal] = useState<any>(false);

    const defaultAttribute = {
        id: '',
        name: '',
        type: 'select',
        values: [],
        // temp_select: '',
    };

    const [ProductAttribute, setProductAttribute] = useState<any>(defaultAttribute);
    const [isLoading, setIsLoading] = useState(true); // Add isLoading state
    const [isMounted, setIsMounted] = useState(false);

    const [page, setPage] = useState(1);
    const PAGE_SIZES = [5, 10, 20, 30, 50, 100];
    const [pageSize, setPageSize] = useState(PAGE_SIZES[0]);
    const [records, setRecords] = useState<Attribute[]>([]);
    const [totalRecords, setTotalRecords] = useState(0);
    const [search, setSearch] = useState('');
    const [isUpdate, setIsUpdate] = useState(false);
    // const [product, setProduct] = useState<any>([]);

    // Fetch data from API
    const fetchData = async () => {
        setIsLoading(true); // Set isLoading to true before fetching data
        try {
            const api_url = process.env.API_URL;
            const response = await fetch(`${api_url}api/product-attributes/list?page=${page}&per_page=${pageSize}&q=${search}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include', // Include this option
            });
            const data = await response.json();

            setPage(data.currentPage);
            setTotalRecords(data.totalData);
            setRecords(data.data);

            setIsLoading(false); // Set isLoading to false after data is fetched
        } catch (error) {
            console.error('Error fetching data:', error);
            setIsLoading(false); // Set isLoading to false even if there's an error
        }
    };
    useEffect(() => {
        fetchData();
    }, [page, pageSize]);

    const handleKeypress = (event: React.KeyboardEvent<HTMLInputElement>) => {
        if (event.keyCode === 13) {
            if (page !== 1) {
                setPage(1);
            } else {
                fetchData();
            }
        }
    };

    const editAttribute = (attribute: any = null, type: any = null, id: any = null, values: any = null) => {
        if (id) {
            setProductAttribute((prev: any) => {
                return {
                    ...prev,
                    id: id,
                    name: attribute,
                    type: type,
                    values: values,
                    // temp_select: type,
                };
            });
            setIsUpdate(true);
        } else {
            setIsUpdate(false);
            setProductAttribute(defaultAttribute);
        }
        setAddAttributeModal(true);
    };

    const changeValueLabel = (e: any) => {
        setProductAttribute((prev: any) => {
            return {
                ...prev,
                name: e.target.value,
            };
        });
    };

    const changeValues = (e: any, index: any = null, type: any = null, fieldName: any = null) => {
        const newValue = e.target.value;
        if (type === 'label' || type === 'select') {
            setProductAttribute((prev: any) => {
                const updatedValues = [...prev.values];
                updatedValues[index] = newValue;

                return {
                    ...prev,
                    values: updatedValues,
                };
            });
        } else {
            setProductAttribute((prev: any) => {
                const updatedValues = [...prev.values];
                updatedValues[index] = {
                    ...updatedValues[index],
                    [fieldName]: newValue,
                };

                return {
                    ...prev,
                    values: updatedValues,
                };
            });
        }
    };

    const changeType = (e: any) => {
        const { id, value } = e.target;
        setProductAttribute((prev: any) => {
            return {
                ...prev,
                type: value,
                // temp_select: value,
            };
        });
    };

    const saveProductAttribute = async () => {
        const attribute_name = ProductAttribute.name;
        const action = isUpdate ? 'Update' : 'add';
        try {
            const api_url = process.env.API_URL;
            const id = ProductAttribute.id;
            const method = action === 'Update' ? `/update/${id}` : '/add';
            const api_method = action === 'Update' ? 'PUT' : 'POST';
            const fetchUrl = `${api_url}api/product-attributes${method}`;

            // const getProduct = await fetch(`${api_url}api/products/list`);

            let values = ProductAttribute.values.length > 0 ? ProductAttribute.values : [];

            if (!attribute_name) {
                coloredToast('warning', 'Attribute name not defiend');
                return;
            }

            // Cek apakah ada nilai yang kosong
            if (values.some((value: string) => value === '')) {
                coloredToast('warning', 'Please fill in all values');
                return;
            }

            if (ProductAttribute.type === 'color') {
                if (values.map((item: any) => item.label && item.hex).includes('')) {
                    coloredToast('warning', 'Please fill in all values');
                    return;
                }
            }

            if (ProductAttribute.type === 'image') {
                if (values.map((item: any) => item.label && item.img_url).includes('')) {
                    coloredToast('warning', 'Please fill in all values');
                    return;
                }
            }

            const data = {
                data: {
                    name: ProductAttribute.name,
                    type: ProductAttribute.type,
                    // type: ProductAttribute.temp_select,
                    values: values,
                },
            };

            const response = await fetch(fetchUrl, {
                method: `${api_method}`,
                headers: {
                    'Content-Type': 'application/json',
                },
                credentials: 'include', // Include this option
                body: JSON.stringify(data),
            });
            if (!response.ok) {
                const res = await response.json();
                throw new Error(res.message);
            }
            coloredToast('success', `Product attribute ${action === 'Update' ? 'Updated' : 'created'} successfully`);
            fetchData();
            await logActivity({
                location: 'Products',
                description: `Product attribute - ${attribute_name} - ${action === 'Update' ? 'Updated' : 'created'} successfully`,
            });
        } catch (error: any) {
            console.error(error);
            await logActivity({
                location: 'Products',
                description: `Product attribute - ${attribute_name} - gagal di ${action === 'Update' ? 'Update' : 'Buat'}`,
            });
            coloredToast('warning', `${error.message || 'something went wrong'}`);
        }
        setAddAttributeModal(false);
        setAddAttributeValueModal(false);
    };

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

    const configure = (name: any, type: any, value: any = null, id: any) => {
        if (value) {
            setProductAttribute((prev: any) => {
                return {
                    ...prev,
                    id: id,
                    name: name,
                    type: type,
                    values: value,
                    // temp_select: type,
                };
            });

            setIsUpdate(true);
        } else {
            setIsUpdate(false);
            setProductAttribute(defaultAttribute);
        }

        setAddAttributeValueModal(true);
    };

    const handleAddRow = (type: any) => {
        const newImage = {
            label: '',
            img_url: '',
        };

        const newColor = {
            label: '',
            hex: '',
        };

        setProductAttribute((prev: any) => {
            if (type === 'image') {
                return {
                    ...prev,
                    values: [...prev.values, newImage],
                };
            } else if (type === 'color') {
                return {
                    ...prev,
                    values: [...prev.values, newColor],
                };
            } else {
                return {
                    ...prev,
                    values: [...prev.values, ''],
                };
            }
        });
    };

    const handleDeleteRow = (index: number) => {
        // Buat salinan array menggunakan slice untuk memastikan Anda tidak mengubah array asli
        const updatedValues = [...ProductAttribute.values];
        updatedValues.splice(index, 1);

        setProductAttribute((prev: any) => {
            return {
                ...prev,
                values: updatedValues,
            };
        });
    };

    return (
        <>
                {isMounted && (
                    <>
                        <div className="mb-3">
                            <ul className="flex space-x-2 rtl:space-x-reverse">
                                <li>
                                    <Link href="/products" className="text-primary hover:underline">
                                        Product
                                    </Link>
                                </li>
                                <li className="before:content-['/'] ltr:before:mr-2 rtl:before:ml-2">
                                    <span>attribute</span>
                                </li>
                            </ul>
                        </div>
                        <div className="panel border-white-light px-0 dark:border-[#1b2e4b]">
                            <div className="invoice-table">
                                <div className="mb-4.5 flex flex-col gap-5 px-5 md:flex-row md:items-center">
                                    <div className="flex items-center gap-2">
                                        <button onClick={() => editAttribute()} className="btn btn-outline-primary gap-2">
                                            <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 Attribute
                                        </button>
                                    </div>
                                    <div className="flex ltr:ml-auto rtl:mr-auto">
                                        <input
                                            type="text"
                                            className="form-input w-auto"
                                            placeholder="Search Attribute Name"
                                            value={search}
                                            onChange={(e) => setSearch(e.target.value)}
                                            onKeyDown={handleKeypress}
                                        />
                                    </div>
                                </div>
                                <div className="datatables pagination-padding">
                                    {isLoading ? (
                                        // Show loading indicator here
                                        <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: 'name',
                                                    render: ({ name }) => <>{name}</>,
                                                },

                                                {
                                                    accessor: 'type',
                                                    render: ({ type }) => <>{type}</>,
                                                },
                                                {
                                                    accessor: 'terms',
                                                    cellsClassName: 'w-72 whitespace-normal',
                                                    render: ({ type, values }) => (
                                                        <>
                                                            {type === 'color' &&
                                                                values.map((item: any, index: number) => (
                                                                    <div className="mb-1" key={index}>
                                                                        {item.label} - {item.hex}
                                                                    </div>
                                                                ))}
                                                            {type === 'image' &&
                                                                values.map((item: any, index: number) => (
                                                                    <div className="mb-1" key={index}>
                                                                        {item.label} - {item.img_url}
                                                                    </div>
                                                                ))}
                                                            {(type === 'label' || type === 'select') && <>{values.join(', ')}</>}
                                                        </>
                                                    ),
                                                },
                                                {
                                                    accessor: 'action',
                                                    title: 'Actions',
                                                    textAlign: 'center',
                                                    render: ({ name, type, values, id }) => (
                                                        <div className="mx-auto flex w-max items-center gap-4">
                                                            <Tippy content="Edit" placement="top">
                                                                <button onClick={() => editAttribute(name, type, id, values)}>
                                                                    <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>
                                                                </button>
                                                            </Tippy>
                                                            <Tippy content="Configure Term" placement="top">
                                                                <button onClick={() => configure(name, type, values, id)}>
                                                                    <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                                                        <path
                                                                            opacity="0.5"
                                                                            d="M2.5 6.5C2.5 4.61438 2.5 3.67157 3.08579 3.08579C3.67157 2.5 4.61438 2.5 6.5 2.5C8.38562 2.5 9.32843 2.5 9.91421 3.08579C10.5 3.67157 10.5 4.61438 10.5 6.5V17.5C10.5 19.3856 10.5 20.3284 9.91421 20.9142C9.32843 21.5 8.38562 21.5 6.5 21.5C4.61438 21.5 3.67157 21.5 3.08579 20.9142C2.5 20.3284 2.5 19.3856 2.5 17.5V6.5Z"
                                                                            stroke="currentColor"
                                                                            strokeWidth="1.5"
                                                                        />
                                                                        <path
                                                                            d="M13.5 15.5C13.5 13.6144 13.5 12.6716 14.0858 12.0858C14.6716 11.5 15.6144 11.5 17.5 11.5C19.3856 11.5 20.3284 11.5 20.9142 12.0858C21.5 12.6716 21.5 13.6144 21.5 15.5V17.5C21.5 19.3856 21.5 20.3284 20.9142 20.9142C20.3284 21.5 19.3856 21.5 17.5 21.5C15.6144 21.5 14.6716 21.5 14.0858 20.9142C13.5 20.3284 13.5 19.3856 13.5 17.5V15.5Z"
                                                                            stroke="currentColor"
                                                                            strokeWidth="1.5"
                                                                        />
                                                                        <path
                                                                            d="M13.5 5.5C13.5 4.56812 13.5 4.10218 13.6522 3.73463C13.8552 3.24458 14.2446 2.85523 14.7346 2.65224C15.1022 2.5 15.5681 2.5 16.5 2.5H18.5C19.4319 2.5 19.8978 2.5 20.2654 2.65224C20.7554 2.85523 21.1448 3.24458 21.3478 3.73463C21.5 4.10218 21.5 4.56812 21.5 5.5C21.5 6.43188 21.5 6.89782 21.3478 7.26537C21.1448 7.75542 20.7554 8.14477 20.2654 8.34776C19.8978 8.5 19.4319 8.5 18.5 8.5H16.5C15.5681 8.5 15.1022 8.5 14.7346 8.34776C14.2446 8.14477 13.8552 7.75542 13.6522 7.26537C13.5 6.89782 13.5 6.43188 13.5 5.5Z"
                                                                            stroke="currentColor"
                                                                            strokeWidth="1.5"
                                                                        />
                                                                    </svg>
                                                                </button>
                                                            </Tippy>
                                                        </div>
                                                    ),
                                                },
                                            ]}
                                            highlightOnHover
                                            totalRecords={totalRecords}
                                            recordsPerPage={pageSize}
                                            page={page}
                                            onPageChange={(p) => setPage(p)}
                                            recordsPerPageOptions={PAGE_SIZES}
                                            onRecordsPerPageChange={setPageSize}
                                            paginationText={({ from, to, totalRecords }) => `Showing  ${from} to ${to} of ${totalRecords} attribute`}
                                        />
                                    )}
                                </div>
                            </div>
                        </div>

                        <Transition appear show={addAttributeModal} as={Fragment}>
                            <Dialog as="div" open={addAttributeModal} onClose={() => setAddAttributeModal(false)} className="relative z-50">
                                <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]/60" />
                                </Transition.Child>
                                <div className="fixed inset-0 overflow-y-auto">
                                    <div className="flex min-h-full items-center justify-center px-4 py-8">
                                        <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-full max-w-lg overflow-hidden rounded-lg border-0 p-0 text-black dark:text-white-dark">
                                                <button
                                                    type="button"
                                                    onClick={() => setAddAttributeModal(false)}
                                                    className="absolute top-4 text-gray-400 outline-none hover:text-gray-800 ltr:right-4 rtl:left-4 dark:hover:text-gray-600"
                                                >
                                                    <svg
                                                        xmlns="http://www.w3.org/2000/svg"
                                                        width="24"
                                                        height="24"
                                                        viewBox="0 0 24 24"
                                                        fill="none"
                                                        stroke="currentColor"
                                                        strokeWidth="1.5"
                                                        strokeLinecap="round"
                                                        strokeLinejoin="round"
                                                    >
                                                        <line x1="18" y1="6" x2="6" y2="18"></line>
                                                        <line x1="6" y1="6" x2="18" y2="18"></line>
                                                    </svg>
                                                </button>
                                                <div className="bg-[#fbfbfb] py-3 text-lg font-medium ltr:pl-5 ltr:pr-[50px] rtl:pl-[50px] rtl:pr-5 dark:bg-[#121c2c]">
                                                    {isUpdate ? 'Edit Attribute' : 'Add Attribute'}
                                                </div>
                                                <div className="p-5">
                                                    <form>
                                                        <div className="mb-5">
                                                            <label htmlFor="name">Attribute Name</label>
                                                            <input
                                                                id="name"
                                                                type="text"
                                                                placeholder="Enter Attribute Name"
                                                                className="form-input"
                                                                value={ProductAttribute.name}
                                                                onChange={changeValueLabel}
                                                                autoComplete="off"
                                                            />
                                                        </div>
                                                        <div className="mb-5">
                                                            <label htmlFor="name">Type</label>
                                                            <select
                                                                className="form-select mr-2 w-full pr-9 text-white-dark"
                                                                id="type"
                                                                onChange={changeType}
                                                                value={ProductAttribute.type}
                                                                disabled={ProductAttribute.type === 'image' || ProductAttribute.type === 'color'}
                                                                // value={ProductAttribute.temp_select}
                                                                // disabled={ProductAttribute.type === 'image' && ProductAttribute.type !== '' && ProductAttribute.id !== ''}
                                                            >
                                                                <option value="select">Select</option>
                                                                <option value="label">Label</option>
                                                                <option value="image" hidden={isUpdate}>
                                                                    {/* <option value="" hidden={ProductAttribute.type !== '' && ProductAttribute.id !== ''} disabled defaultValue={'Select'}>
                                                                    Select Type
                                                                </option>
                                                                <option value="image" hidden={ProductAttribute.type === 'select' || ProductAttribute.type === 'label'}> */}
                                                                    Image
                                                                </option>
                                                                {/* <option value="select">Select</option>
                                                                <option value="label">Label</option> */}

                                                                <option value="color" hidden>
                                                                    Color
                                                                </option>
                                                            </select>
                                                        </div>
                                                        <div className="mt-8 flex items-center justify-end">
                                                            <button type="button" className="btn btn-outline-danger" onClick={() => setAddAttributeModal(false)}>
                                                                Cancel
                                                            </button>
                                                            <button type="button" className="btn btn-primary ltr:ml-4 rtl:mr-4" onClick={saveProductAttribute}>
                                                                {isUpdate ? 'Update' : 'Add'}
                                                            </button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </Dialog.Panel>
                                        </Transition.Child>
                                    </div>
                                </div>
                            </Dialog>
                        </Transition>

                        <Transition appear show={addAttributeValueModal} as={Fragment}>
                            <Dialog as="div" open={addAttributeValueModal} onClose={() => setAddAttributeValueModal(false)} className="relative z-50">
                                <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]/60" />
                                </Transition.Child>
                                <div className="fixed inset-0 overflow-y-auto">
                                    <div className="flex min-h-full items-center justify-center px-4 py-8">
                                        <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-full max-w-lg overflow-hidden rounded-lg border-0 p-0 text-black dark:text-white-dark">
                                                <button
                                                    type="button"
                                                    onClick={() => setAddAttributeValueModal(false)}
                                                    className="absolute top-4 text-gray-400 outline-none hover:text-gray-800 ltr:right-4 rtl:left-4 dark:hover:text-gray-600"
                                                >
                                                    <svg
                                                        xmlns="http://www.w3.org/2000/svg"
                                                        width="24"
                                                        height="24"
                                                        viewBox="0 0 24 24"
                                                        fill="none"
                                                        stroke="currentColor"
                                                        strokeWidth="1.5"
                                                        strokeLinecap="round"
                                                        strokeLinejoin="round"
                                                    >
                                                        <line x1="18" y1="6" x2="6" y2="18"></line>
                                                        <line x1="6" y1="6" x2="18" y2="18"></line>
                                                    </svg>
                                                </button>
                                                <div className="bg-[#fbfbfb] py-3 text-lg font-medium ltr:pl-5 ltr:pr-[50px] rtl:pl-[50px] rtl:pr-5 dark:bg-[#121c2c]">
                                                    {isUpdate ? 'Edit Attribute' : 'Add Attribute'}
                                                </div>
                                                <div className="p-5">
                                                    <form>
                                                        <div className="mb-5">
                                                            {ProductAttribute.type === 'image' && (
                                                                <>
                                                                    <table>
                                                                        <thead>
                                                                            <tr>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`label`}>Label</label>
                                                                                </td>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`img_url`}>Image Url</label>
                                                                                </td>
                                                                                <td></td>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            {ProductAttribute.values.length > 0 &&
                                                                                ProductAttribute.values.map((itemRecord: any, index: any) => (
                                                                                    <tr key={index}>
                                                                                        <td>
                                                                                            <input
                                                                                                type="text"
                                                                                                id={`label_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord.label}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'image', 'label')}
                                                                                            />
                                                                                        </td>
                                                                                        <td>
                                                                                            <input
                                                                                                type="url"
                                                                                                id={`img_url_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord.img_url}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'image', 'img_url')}
                                                                                            />
                                                                                        </td>
                                                                                        <td>
                                                                                            {ProductAttribute.values.length > 1 && (
                                                                                                <Tippy content="Delete" placement="right">
                                                                                                    <svg
                                                                                                        width="26"
                                                                                                        height="26"
                                                                                                        viewBox="0 0 24 24"
                                                                                                        fill="none"
                                                                                                        xmlns="http://www.w3.org/2000/svg"
                                                                                                        onClick={() => handleDeleteRow(index)}
                                                                                                    >
                                                                                                        <path
                                                                                                            opacity="0.5"
                                                                                                            d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                        />
                                                                                                        <path
                                                                                                            d="M14.5 9.50002L9.5 14.5M9.49998 9.5L14.5 14.5"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                            strokeLinecap="round"
                                                                                                        />
                                                                                                    </svg>
                                                                                                </Tippy>
                                                                                            )}
                                                                                        </td>
                                                                                    </tr>
                                                                                ))}
                                                                        </tbody>
                                                                    </table>
                                                                    <div className="mr-4 mt-3 flex justify-end">
                                                                        <Tippy content="Add Attribute" placement="right">
                                                                            <svg
                                                                                width="26"
                                                                                height="26"
                                                                                viewBox="0 0 24 24"
                                                                                fill="none"
                                                                                xmlns="http://www.w3.org/2000/svg"
                                                                                onClick={() => handleAddRow('image')}
                                                                            >
                                                                                <path
                                                                                    opacity="0.5"
                                                                                    d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                />
                                                                                <path
                                                                                    d="M15 12L12 12M12 12L9 12M12 12L12 9M12 12L12 15"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                    strokeLinecap="round"
                                                                                />
                                                                            </svg>
                                                                        </Tippy>
                                                                    </div>
                                                                </>
                                                            )}

                                                            {ProductAttribute.type === 'label' && (
                                                                <>
                                                                    <table>
                                                                        <thead>
                                                                            <tr>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`label`}>Label</label>
                                                                                </td>
                                                                                <td></td>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            {ProductAttribute.values.length > 0 &&
                                                                                ProductAttribute.values.map((itemRecord: any, index: any) => (
                                                                                    <tr key={index}>
                                                                                        <td>
                                                                                            <input
                                                                                                type="text"
                                                                                                id={`label_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'label')}
                                                                                            />
                                                                                        </td>
                                                                                        <td className="mx-auto my-auto flex w-max ">
                                                                                            {ProductAttribute.values.length > 1 && (
                                                                                                <Tippy content="Delete" placement="right">
                                                                                                    <svg
                                                                                                        width="26"
                                                                                                        height="26"
                                                                                                        viewBox="0 0 24 24"
                                                                                                        fill="none"
                                                                                                        xmlns="http://www.w3.org/2000/svg"
                                                                                                        onClick={() => handleDeleteRow(index)}
                                                                                                    >
                                                                                                        <path
                                                                                                            opacity="0.5"
                                                                                                            d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                        />
                                                                                                        <path
                                                                                                            d="M14.5 9.50002L9.5 14.5M9.49998 9.5L14.5 14.5"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                            strokeLinecap="round"
                                                                                                        />
                                                                                                    </svg>
                                                                                                </Tippy>
                                                                                            )}
                                                                                        </td>
                                                                                    </tr>
                                                                                ))}
                                                                        </tbody>
                                                                    </table>
                                                                    <div className="mr-8 mt-3 flex justify-end">
                                                                        <Tippy content="Add Attribute" placement="right">
                                                                            <svg
                                                                                width="26"
                                                                                height="26"
                                                                                viewBox="0 0 24 24"
                                                                                fill="none"
                                                                                xmlns="http://www.w3.org/2000/svg"
                                                                                onClick={() => handleAddRow('label')}
                                                                            >
                                                                                <path
                                                                                    opacity="0.5"
                                                                                    d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                />
                                                                                <path
                                                                                    d="M15 12L12 12M12 12L9 12M12 12L12 9M12 12L12 15"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                    strokeLinecap="round"
                                                                                />
                                                                            </svg>
                                                                        </Tippy>
                                                                    </div>
                                                                </>
                                                            )}

                                                            {ProductAttribute.type === 'select' && (
                                                                <>
                                                                    <table>
                                                                        <thead>
                                                                            <tr>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`label`}>Label</label>
                                                                                </td>
                                                                                <td></td>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            {ProductAttribute.values.length > 0 &&
                                                                                ProductAttribute.values.map((itemRecord: any, index: any) => (
                                                                                    <tr key={index}>
                                                                                        <td>
                                                                                            <input
                                                                                                type="text"
                                                                                                id={`select_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'select')}
                                                                                            />
                                                                                        </td>
                                                                                        <td className="mx-auto my-auto flex w-max ">
                                                                                            {ProductAttribute.values.length > 1 && (
                                                                                                <Tippy content="Delete" placement="right">
                                                                                                    <svg
                                                                                                        width="26"
                                                                                                        height="26"
                                                                                                        viewBox="0 0 24 24"
                                                                                                        fill="none"
                                                                                                        xmlns="http://www.w3.org/2000/svg"
                                                                                                        onClick={() => handleDeleteRow(index)}
                                                                                                    >
                                                                                                        <path
                                                                                                            opacity="0.5"
                                                                                                            d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                        />
                                                                                                        <path
                                                                                                            d="M14.5 9.50002L9.5 14.5M9.49998 9.5L14.5 14.5"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                            strokeLinecap="round"
                                                                                                        />
                                                                                                    </svg>
                                                                                                </Tippy>
                                                                                            )}
                                                                                        </td>
                                                                                    </tr>
                                                                                ))}
                                                                        </tbody>
                                                                    </table>
                                                                    <div className="mr-8 mt-3 flex justify-end">
                                                                        <Tippy content="Add Attribute" placement="right">
                                                                            <svg
                                                                                width="26"
                                                                                height="26"
                                                                                viewBox="0 0 24 24"
                                                                                fill="none"
                                                                                xmlns="http://www.w3.org/2000/svg"
                                                                                onClick={() => handleAddRow('label')}
                                                                            >
                                                                                <path
                                                                                    opacity="0.5"
                                                                                    d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                />
                                                                                <path
                                                                                    d="M15 12L12 12M12 12L9 12M12 12L12 9M12 12L12 15"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                    strokeLinecap="round"
                                                                                />
                                                                            </svg>
                                                                        </Tippy>
                                                                    </div>
                                                                </>
                                                            )}

                                                            {ProductAttribute.type === 'color' && (
                                                                <>
                                                                    <table>
                                                                        <thead>
                                                                            <tr>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`color_label`}>Label</label>
                                                                                </td>
                                                                                <td className="text-center">
                                                                                    <label htmlFor={`hex`}>Hexa</label>
                                                                                </td>
                                                                                <td></td>
                                                                            </tr>
                                                                        </thead>
                                                                        <tbody>
                                                                            {ProductAttribute.values.length > 0 &&
                                                                                ProductAttribute.values.map((itemRecord: any, index: any) => (
                                                                                    <tr key={index}>
                                                                                        <td>
                                                                                            <input
                                                                                                type="text"
                                                                                                id={`label_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord.label}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'color', 'label')}
                                                                                            />
                                                                                        </td>
                                                                                        <td className="flex gap-3">
                                                                                            <input
                                                                                                type="text"
                                                                                                id={`img_url_${index}`}
                                                                                                className="form-input"
                                                                                                value={itemRecord.hex}
                                                                                                autoComplete="off"
                                                                                                onChange={(e) => changeValues(e, index, 'color', 'hex')}
                                                                                            />
                                                                                            <input
                                                                                                type="color"
                                                                                                id={`img_url_${index}`}
                                                                                                className="hex "
                                                                                                value={itemRecord.hex}
                                                                                                onChange={(e) => changeValues(e, index, 'color', 'hex')}
                                                                                            />
                                                                                        </td>
                                                                                        <td>
                                                                                            {ProductAttribute.values.length > 1 && (
                                                                                                <Tippy content="Delete" placement="right">
                                                                                                    <svg
                                                                                                        width="26"
                                                                                                        height="26"
                                                                                                        viewBox="0 0 24 24"
                                                                                                        fill="none"
                                                                                                        xmlns="http://www.w3.org/2000/svg"
                                                                                                        onClick={() => handleDeleteRow(index)}
                                                                                                    >
                                                                                                        <path
                                                                                                            opacity="0.5"
                                                                                                            d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                        />
                                                                                                        <path
                                                                                                            d="M14.5 9.50002L9.5 14.5M9.49998 9.5L14.5 14.5"
                                                                                                            stroke="currentColor"
                                                                                                            strokeWidth="1.5"
                                                                                                            strokeLinecap="round"
                                                                                                        />
                                                                                                    </svg>
                                                                                                </Tippy>
                                                                                            )}
                                                                                        </td>
                                                                                    </tr>
                                                                                ))}
                                                                        </tbody>
                                                                    </table>
                                                                    <div className="mr-4 mt-3 flex justify-end">
                                                                        <Tippy content="Add Attribute" placement="right">
                                                                            <svg
                                                                                width="26"
                                                                                height="26"
                                                                                viewBox="0 0 24 24"
                                                                                fill="none"
                                                                                xmlns="http://www.w3.org/2000/svg"
                                                                                onClick={() => {
                                                                                    handleAddRow('color');
                                                                                }}
                                                                            >
                                                                                <path
                                                                                    opacity="0.5"
                                                                                    d="M2 12C2 7.28595 2 4.92893 3.46447 3.46447C4.92893 2 7.28595 2 12 2C16.714 2 19.0711 2 20.5355 3.46447C22 4.92893 22 7.28595 22 12C22 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 12Z"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                />
                                                                                <path
                                                                                    d="M15 12L12 12M12 12L9 12M12 12L12 9M12 12L12 15"
                                                                                    stroke="currentColor"
                                                                                    strokeWidth="1.5"
                                                                                    strokeLinecap="round"
                                                                                />
                                                                            </svg>
                                                                        </Tippy>
                                                                    </div>
                                                                </>
                                                            )}
                                                        </div>

                                                        <div className="mt-8 flex items-center justify-end">
                                                            <button type="button" className="btn btn-outline-danger" onClick={() => setAddAttributeValueModal(false)}>
                                                                Cancel
                                                            </button>
                                                            <button type="button" className="btn btn-primary ltr:ml-4 rtl:mr-4" onClick={saveProductAttribute}>
                                                                {isUpdate ? 'Update' : 'Add'}
                                                            </button>
                                                        </div>
                                                    </form>
                                                </div>
                                            </Dialog.Panel>
                                        </Transition.Child>
                                    </div>
                                </div>
                            </Dialog>
                        </Transition>
                    </>
                )}
        </>
    );
};
export default Attribute;
