import { useEffect, useState } from "react";
import Upload from 'rc-upload';
 import * as XLSX from 'xlsx';
import { checkedImage, convertBase64 } from "@/helpers/convertBase64";
export const UploadForm = ({ submitCount, errors, touched, name, title, value, remove, index, values, insert }: { submitCount: number; errors: any, touched: any, name: string; title: string; value?: any, remove: any, index: number, values: any, insert: any }) => {
    const [upLoading, setUpLoading] = useState(false);
    const [image, setImage] = useState(value);
    let pathArr = name.split('.');
    let errorValue = pathArr.reduce((prev, curr) => prev && prev[curr], errors);
    // let touchedValue = pathArr.reduce((prev, curr) => prev && prev[curr], touched);
    useEffect(() => {
        if (value) {
            if (value instanceof File) {
                convertBase64(value).then((res) => {
                    setImage(res);
                });
            } else {
                setImage(value);
            }

        }
    }, [value]);
    return (
        <> <div className={`mb-3 flex `}>
            <label htmlFor={title}>{title}</label>
            <Upload
                // multiple
                className={`h-[170px!important] w-[100%!important] rounded-md border border-dashed hover:border-primary ${submitCount ? (errorValue  ? '!bg-red-500/10 border-primary' : '') : ''}`}
                type="drag"
                beforeUpload={async (file: any) => {
                    const { type, size }: any = checkedImage(file);
                    if (type & size) {
                        const base64: any = await convertBase64(file);
                        setUpLoading((upLoading) => !upLoading);
                        setTimeout(() => {
                            insert(index, { id: values.voyageImgs[index]?.id, img: file, type: "new" })
                            setImage(base64);
                            setUpLoading((upLoading) => !upLoading);
                        }, 500);
                    }
                    return false;
                }}
                style={{ display: 'inline-block' }}
            >
                <div className=" mx-2 flex h-[170px!important] items-center justify-center  gap-4 p-1 ">
                    {!upLoading ? (
                        image ? (
                            <img src={image} alt="profile" className="h-full w-full object-contain" onError={(e) => {
                                setImage(undefined)
                            }} />
                        ) : (
                            <div className="flex w-1/2 flex-col items-center justify-center gap-1 text-center">
                                <div className="">
                                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path
                                            fillRule="evenodd"
                                            clipRule="evenodd"
                                            d="M11.4697 15.4697C11.7626 15.1768 12.2374 15.1768 12.5303 15.4697L14.5303 17.4697C14.8232 17.7626 14.8232 18.2374 14.5303 18.5303C14.2374 18.8232 13.7626 18.8232 13.4697 18.5303L12.75 17.8107V22C12.75 22.4142 12.4142 22.75 12 22.75C11.5858 22.75 11.25 22.4142 11.25 22V17.8107L10.5303 18.5303C10.2374 18.8232 9.76256 18.8232 9.46967 18.5303C9.17678 18.2374 9.17678 17.7626 9.46967 17.4697L11.4697 15.4697Z"
                                            stroke="currentColor"
                                        />
                                        <path
                                            d="M12.4762 3.75C9.7261 3.75 7.5119 5.95083 7.5119 8.64706C7.5119 9.10922 7.5766 9.55551 7.69729 9.97813C8.19449 10.1216 8.65991 10.3389 9.08045 10.6171C9.42592 10.8456 9.52072 11.3109 9.29219 11.6564C9.06366 12.0019 8.59835 12.0967 8.25288 11.8681C7.87207 11.6162 7.43917 11.4355 6.9733 11.3451C6.75147 11.3021 6.52165 11.2794 6.28571 11.2794C4.3246 11.2794 2.75 12.8482 2.75 14.7647C2.75 16.6812 4.3246 18.25 6.28571 18.25C6.69993 18.25 7.03571 18.5858 7.03571 19C7.03571 19.4142 6.69993 19.75 6.28571 19.75C3.51296 19.75 1.25 17.5264 1.25 14.7647C1.25 12.0605 3.41987 9.87207 6.11351 9.78228C6.04673 9.41355 6.0119 9.03413 6.0119 8.64706C6.0119 5.10572 8.91446 2.25 12.4762 2.25C15.6343 2.25 18.2724 4.49369 18.8314 7.47127C21.1313 8.44751 22.75 10.7093 22.75 13.3529C22.75 16.4269 20.5623 18.9843 17.6568 19.6057C17.2518 19.6923 16.8532 19.4341 16.7666 19.0291C16.68 18.624 16.9381 18.2254 17.3432 18.1388C19.5829 17.6598 21.25 15.693 21.25 13.3529C21.25 11.2162 19.8607 9.39087 17.9124 8.72463C17.4038 8.55072 16.8568 8.45588 16.2857 8.45588C15.7031 8.45588 15.1455 8.55458 14.6283 8.73526C14.2372 8.87185 13.8095 8.66557 13.6729 8.27453C13.5363 7.88348 13.7426 7.45575 14.1336 7.31916C14.8079 7.08364 15.5326 6.95588 16.2857 6.95588C16.5812 6.95588 16.8723 6.97555 17.1577 7.01367C16.477 5.11631 14.6422 3.75 12.4762 3.75Z"
                                            stroke="currentColor"
                                        />
                                    </svg>
                                </div>
                                <p className="text-gray-400">Click or drag file to this area to upload</p>
                            </div>
                        )
                    ) : (
                        <div className="flex h-full w-full items-center justify-center">
                            <span className="mx-1 inline-block h-5 w-5 animate-spin rounded-full border-2 border-white border-l-transparent align-middle ltr:mr-4 rtl:ml-4"></span>
                        </div>
                    )}
                </div>

            {submitCount ? errorValue  && <div className="w-full text-danger text-sm p-1 mt-[2px]">{errorValue}</div> : ''}

            </Upload>
            <button type="button" className="p-1 mx-1 border rounded-md text-dark-dark-light dark:text-dark border-dark-dark-light dark:border-dark dark:hover:border-danger hover:border-danger hover:text-danger dark:hover:text-danger" onClick={() => remove(index)

            }>
                <svg
                    xmlns="http://www.w3.org/2000/svg"
                    width="20"
                    height="20"
                    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>
        </>

    );
};



export const UploadSingleForm = ({ setFieldValue, name, title, value, submitCount, errors, touched }: { setFieldValue: any; name: string; title: string; value?: any, submitCount: number; errors: any; touched: any; }) => {
    const [upLoading, setUpLoading] = useState(false);
    const [image, setImage] = useState(value);
    useEffect(() => {
        if (value) {
            if (value instanceof File) {
                convertBase64(value).then((res) => {
                    setImage(res);
                });
            } else {
                setImage(value);
            }

        }
    }, [value]);
    return (
        <div className={`mb-3  `}>
            <label htmlFor={title}>{title}</label>
            <Upload
                className={`h-[170px!important] w-[100%!important] rounded-md border border-dashed hover:border-primary ${submitCount ? (errors[name] && touched[name] ? '!bg-red-500/10 border-primary' : '') : ''}`}
                type="drag"
                beforeUpload={async (file: any) => {
                    const { type, size }: any = checkedImage(file);
                    if (type & size) {
                        const base64: any = await convertBase64(file);
                        setUpLoading((upLoading) => !upLoading);
                        setTimeout(() => {
                            setFieldValue(name, file);
                            setImage(base64);
                            setUpLoading((upLoading) => !upLoading);
                        }, 500);
                    }
                    return false;
                }}
                style={{ display: 'inline-block' }}
            >
                <div className={`mx-2 flex h-[170px!important] items-center justify-center  gap-4 p-1 `}>
                    {!upLoading ? (
                        image ? (
                            <img src={image} alt="profile" className="h-full w-full object-contain" onError={(e) => {
                                setImage(undefined)
                            }} />
                        ) : (
                            <div className="flex w-1/2 flex-col items-center justify-center gap-1 text-center">
                                <div className="">
                                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path
                                            fillRule="evenodd"
                                            clipRule="evenodd"
                                            d="M11.4697 15.4697C11.7626 15.1768 12.2374 15.1768 12.5303 15.4697L14.5303 17.4697C14.8232 17.7626 14.8232 18.2374 14.5303 18.5303C14.2374 18.8232 13.7626 18.8232 13.4697 18.5303L12.75 17.8107V22C12.75 22.4142 12.4142 22.75 12 22.75C11.5858 22.75 11.25 22.4142 11.25 22V17.8107L10.5303 18.5303C10.2374 18.8232 9.76256 18.8232 9.46967 18.5303C9.17678 18.2374 9.17678 17.7626 9.46967 17.4697L11.4697 15.4697Z"
                                            stroke="currentColor"
                                        />
                                        <path
                                            d="M12.4762 3.75C9.7261 3.75 7.5119 5.95083 7.5119 8.64706C7.5119 9.10922 7.5766 9.55551 7.69729 9.97813C8.19449 10.1216 8.65991 10.3389 9.08045 10.6171C9.42592 10.8456 9.52072 11.3109 9.29219 11.6564C9.06366 12.0019 8.59835 12.0967 8.25288 11.8681C7.87207 11.6162 7.43917 11.4355 6.9733 11.3451C6.75147 11.3021 6.52165 11.2794 6.28571 11.2794C4.3246 11.2794 2.75 12.8482 2.75 14.7647C2.75 16.6812 4.3246 18.25 6.28571 18.25C6.69993 18.25 7.03571 18.5858 7.03571 19C7.03571 19.4142 6.69993 19.75 6.28571 19.75C3.51296 19.75 1.25 17.5264 1.25 14.7647C1.25 12.0605 3.41987 9.87207 6.11351 9.78228C6.04673 9.41355 6.0119 9.03413 6.0119 8.64706C6.0119 5.10572 8.91446 2.25 12.4762 2.25C15.6343 2.25 18.2724 4.49369 18.8314 7.47127C21.1313 8.44751 22.75 10.7093 22.75 13.3529C22.75 16.4269 20.5623 18.9843 17.6568 19.6057C17.2518 19.6923 16.8532 19.4341 16.7666 19.0291C16.68 18.624 16.9381 18.2254 17.3432 18.1388C19.5829 17.6598 21.25 15.693 21.25 13.3529C21.25 11.2162 19.8607 9.39087 17.9124 8.72463C17.4038 8.55072 16.8568 8.45588 16.2857 8.45588C15.7031 8.45588 15.1455 8.55458 14.6283 8.73526C14.2372 8.87185 13.8095 8.66557 13.6729 8.27453C13.5363 7.88348 13.7426 7.45575 14.1336 7.31916C14.8079 7.08364 15.5326 6.95588 16.2857 6.95588C16.5812 6.95588 16.8723 6.97555 17.1577 7.01367C16.477 5.11631 14.6422 3.75 12.4762 3.75Z"
                                            stroke="currentColor"
                                        />
                                    </svg>
                                </div>
                                <p className="text-gray-400">Click or drag file to this area to upload</p>
                            </div>
                        )
                    ) : (
                        <div className="flex h-full w-full items-center justify-center">
                            <span className="mx-1 inline-block h-5 w-5 animate-spin rounded-full border-2 border-white border-l-transparent align-middle ltr:mr-4 rtl:ml-4"></span>
                        </div>
                    )}
                </div>
            </Upload>
            {submitCount ? errors[name] && touched[name] && <div className="w-full text-danger text-sm p-1 mt-[2px]">{errors[name]}</div> : ''}

        </div>
    );
};

export const UploadSingleFileForm = ({ setFieldValue, name, title, value, submitCount, errors, touched }: { setFieldValue: any; name: string; title: string; value?: string, submitCount: number; errors: any; touched: any; }) => {
    const [upLoading, setUpLoading] = useState(false);
    const [image, setImage] = useState(value);
    return (
        <div className={`mb-3  `}>
            <label htmlFor={title}>{title}</label>
            <Upload
                accept=".csv"
                className={`h-[170px!important] w-[100%!important] rounded-md border border-dashed hover:border-primary ${submitCount ? (errors[name] && touched[name] ? '!bg-red-500/10 border-primary' : '') : ''}`}
                type="drag"
                beforeUpload={async (file: any) => {
                    if (file) {
                        setUpLoading((upLoading) => !upLoading);
                        setTimeout(async () => {
                            setFieldValue(name, file);
                            const getData = (file: any) => {
                                return new Promise((resolve, reject) => {
                                    const reader = new FileReader();
                                    reader.onload = (e) => {
                                        const data = new Uint8Array(e.target!.result as ArrayBuffer);
                                        const workbook = XLSX.read(data, { type: 'array' });

                                        // Process the workbook data 
                                        const nestedArray = workbook.SheetNames.map((name) => {
                                            const worksheet = workbook.Sheets[name];
                                            const jsonData: any[][] = XLSX.utils.sheet_to_json(worksheet, { header: 1 });
                                            return jsonData
                                        })
                                        const flatArray = nestedArray.flatMap((subArray, indexArray) => [...indexArray == 0 ? subArray : subArray.filter((it, index) => index != 0)]);

                                        resolve(flatArray);
                                    };
                                    reader.readAsArrayBuffer(file);
                                    reader.onerror = (error) => {
                                        reject(error);
                                    };
                                });
                            };
                            const data: any = await getData(file);
                            setFieldValue("column", data[0]);
                            setImage('/assets/images/csv.png');
                            setUpLoading((upLoading) => !upLoading);
                        }, 500);
                    }
                    return false;
                }}
                multiple={false}

                style={{ display: 'inline-block' }}
            >
                <div className={`mx-2 flex h-[170px!important] items-center justify-center  gap-4 p-1 `}>
                    {!upLoading ? (
                        image ? (
                            <img src={image} alt="profile" className="h-full w-full object-contain" onError={(e) => {
                                setImage(undefined)
                            }} />
                        ) : (
                            <div className="flex w-1/2 flex-col items-center justify-center gap-1 text-center">
                                <div className="">
                                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                                        <path
                                            fillRule="evenodd"
                                            clipRule="evenodd"
                                            d="M11.4697 15.4697C11.7626 15.1768 12.2374 15.1768 12.5303 15.4697L14.5303 17.4697C14.8232 17.7626 14.8232 18.2374 14.5303 18.5303C14.2374 18.8232 13.7626 18.8232 13.4697 18.5303L12.75 17.8107V22C12.75 22.4142 12.4142 22.75 12 22.75C11.5858 22.75 11.25 22.4142 11.25 22V17.8107L10.5303 18.5303C10.2374 18.8232 9.76256 18.8232 9.46967 18.5303C9.17678 18.2374 9.17678 17.7626 9.46967 17.4697L11.4697 15.4697Z"
                                            stroke="currentColor"
                                        />
                                        <path
                                            d="M12.4762 3.75C9.7261 3.75 7.5119 5.95083 7.5119 8.64706C7.5119 9.10922 7.5766 9.55551 7.69729 9.97813C8.19449 10.1216 8.65991 10.3389 9.08045 10.6171C9.42592 10.8456 9.52072 11.3109 9.29219 11.6564C9.06366 12.0019 8.59835 12.0967 8.25288 11.8681C7.87207 11.6162 7.43917 11.4355 6.9733 11.3451C6.75147 11.3021 6.52165 11.2794 6.28571 11.2794C4.3246 11.2794 2.75 12.8482 2.75 14.7647C2.75 16.6812 4.3246 18.25 6.28571 18.25C6.69993 18.25 7.03571 18.5858 7.03571 19C7.03571 19.4142 6.69993 19.75 6.28571 19.75C3.51296 19.75 1.25 17.5264 1.25 14.7647C1.25 12.0605 3.41987 9.87207 6.11351 9.78228C6.04673 9.41355 6.0119 9.03413 6.0119 8.64706C6.0119 5.10572 8.91446 2.25 12.4762 2.25C15.6343 2.25 18.2724 4.49369 18.8314 7.47127C21.1313 8.44751 22.75 10.7093 22.75 13.3529C22.75 16.4269 20.5623 18.9843 17.6568 19.6057C17.2518 19.6923 16.8532 19.4341 16.7666 19.0291C16.68 18.624 16.9381 18.2254 17.3432 18.1388C19.5829 17.6598 21.25 15.693 21.25 13.3529C21.25 11.2162 19.8607 9.39087 17.9124 8.72463C17.4038 8.55072 16.8568 8.45588 16.2857 8.45588C15.7031 8.45588 15.1455 8.55458 14.6283 8.73526C14.2372 8.87185 13.8095 8.66557 13.6729 8.27453C13.5363 7.88348 13.7426 7.45575 14.1336 7.31916C14.8079 7.08364 15.5326 6.95588 16.2857 6.95588C16.5812 6.95588 16.8723 6.97555 17.1577 7.01367C16.477 5.11631 14.6422 3.75 12.4762 3.75Z"
                                            stroke="currentColor"
                                        />
                                    </svg>
                                </div>
                                <p className="text-gray-400">Click or drag file to this area to upload</p>
                            </div>
                        )
                    ) : (
                        <div className="flex h-full w-full items-center justify-center">
                            <span className="mx-1 inline-block h-5 w-5 animate-spin rounded-full border-2 border-primary border-l-transparent align-middle ltr:mr-4 rtl:ml-4"></span>
                        </div>
                    )}
                </div>
            </Upload>
            {submitCount ? errors[name] && touched[name] && <div className="w-full text-danger text-sm p-1 mt-[2px]">{errors[name]}</div> : ''}

        </div>
    );
};