import styled, {css} from "styled-components";
import {TextInput} from "../components/TextInput";
import {useState} from "react";
import FeedbackTextarea from "../components/Textarea";
import {subscribeOnFail} from "../../api/promoApi";

export const NotEnoughPopup = ({onClose}: any) => {
    const [email, setEmail] = useState<string>("");
    const [error, setError] = useState<string>("");
    const [value, setValue] = useState('');
    const [sent, setSent] = useState(false);

    return <Overlay onClick={() => onClose()}>
        <Window onClick={event => event.stopPropagation()}>
            <CloseButton type="button" aria-label="Закрыть" onClick={() => onClose()}>
                &times;
            </CloseButton>
            <ModalContent>
                {sent ? <>
                    <h2>Сообщение отправлено</h2>
                    <V8/>
                    <p>
                        Спасибо, ваша помощь крайне важна для нас, всё записали!
                    </p>
                    <img src="/images/broc-fail.png" alt=""/>
                </> : <>
                    <h2>Чего-то не хватило?</h2>
                    <V8/>
                    <p>
                        Расскажите, что не так с текущей корзиной — мы постараемся учесть ваши пожелания. За отзыв
                        подарим <strong>3 бесплатные корзины</strong>
                    </p>
                    <V24/>
                    <TextInput placeholder={'Почта'} value={email} onChange={setEmail}/>
                    <V16/>
                    <FeedbackTextarea maxLength={400} value={value} onChange={(e: React.ChangeEvent<HTMLTextAreaElement>) => {
                        setValue(e.target.value);
                    }}></FeedbackTextarea>
                    <V16/>
                    <V24/>
                    <ActiveButton onClick={() => subscribeOnFail(email, value)
                        .then((d) => {
                            if (d.status === 400) {
                                setError(d.error_message)
                            } else {
                                setSent(true)
                            }
                        })}>Отправить</ActiveButton>
                    <Error>{error ? error : <>&nbsp;</>}</Error>
                </>}
            </ModalContent>
        </Window>
    </Overlay>
};

const V8 = styled.div`height: 8px;`;
const V16 = styled.div`height: 16px;`;
const V24 = styled.div`height: 24px;`;

const Overlay = styled.div`
    height: 100svh;
    position: fixed;
    display: flex;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.4);
    justify-content: center;
    align-items: center;
    z-index: 4;
`;
const Window = styled.div`
    background-color: #ffffff;
    border-radius: 12px;
    width: 515px;
    max-width: 100%;
    padding: 20px 24px;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 9;
    max-height: 90vh;
    overflow: auto;
`;

const CloseButton = styled.button`
    position: absolute;
    top: 20px;
    right: 20px;
    background: none;
    border: none;
    width: 24px;
    height: 24px;
    font-weight: 300;
    line-height: 12px;
    color: #CEDFD0;
    font-size: 24px;
    cursor: pointer;
    padding: 5px;
    transition: 0.2s;

    &:hover {
        color: #ACBAAE;
    }

    ${({theme}) => theme.media.mobile(css`
        top: 40px;
    `)};
`;

const ModalContent = styled.div`
    padding: 20px;
    color: #0C3B2E;
    box-sizing: content-box;

    h2 {
        font-size: 20px;
        text-align: center;
    }

    p {
        font-size: 16px;
        text-align: center;
    }
    img {
        max-width: 100%;
    }
`;
const ButtonBlock = styled.div`
    margin-top: 24px;
    display: flex;
    gap: 16px;
    flex-direction: column;
`;
const Button = styled.div`
    height: 54px;
    border-radius: 12px;
    text-align: center;
    background: #fff;
    color: #5EA12D;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    line-height: 16px;
    cursor: pointer;

    &:hover {
        background-color: #F7FBF6;
    }

    &:active,
    &:focus {
        background-color: #EEF8EB;
        outline: none;
    }

`;
const ActiveButton = styled(Button)`
    background-color: #5EA12D;
    color: #fff;
    font-size: 16px;
    line-height: 20px;

    &:hover,
    &:focus {
        background-color: ${({theme}) => theme.colors.greenHover};
    }

    &:active {
        background-color: #4A8D19;
    }
`;
const Error = styled.div`
    text-align: center;
    height: 16px;
    padding-top: 8px;
`