import styled from 'styled-components';
import {BagIcon} from "../Icons/BagIcon";
import {useState} from "react";
import {OrderPopup} from "./OrderPopup";
import {useAppSelector} from "../../store";

import {reachGoal} from "../../hooks/useYM";


interface CartFooterProps {
    isVisible: boolean;
    totalPrice: string;
}

export function CartFooter({ isVisible, totalPrice }: CartFooterProps) {
    const [showCompletePopup, setShowCompletePopup] = useState(false);
    const users = useAppSelector(state => state.family.users);

    return (
        <Footer $visible={isVisible}>
            <TotalWrap>
                <div>
                    <IconCircle>
                        <BagIcon />
                    </IconCircle>

                    <div>
                        <TotalLabel>Итого</TotalLabel>
                        <TotalPrice>{totalPrice}</TotalPrice>
                    </div>
                </div>
            </TotalWrap>

            <CheckoutButton type="button" onClick={() => {
                reachGoal('ac_hvatilo')
                setShowCompletePopup(true)
            }}>
                Хватило
            </CheckoutButton>
            {showCompletePopup && <>
                <OrderPopup setShowCompletePopup={setShowCompletePopup} />
            </>}
        </Footer>
    );
}

const Footer = styled.div<{ $visible: boolean }>`
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-top: 20px;
    border-top: 1px solid #f0f0f0;
    flex-shrink: 0;

    & > * {
        visibility: ${({ $visible }) => ($visible ? 'visible' : 'hidden')};
    }
`;

const TotalWrap = styled.div`
    display: flex;
    flex-direction: column;
    align-items: flex-start;

    & > div {
        display: flex;
        align-items: center;
    }
`;

const IconCircle = styled.div`
    width: 44px;
    height: 44px;
    border-radius: 50%;
    margin-right: 8px;
`;

const TotalLabel = styled.span`
    font-size: 16px;
    line-height: 19px;
    color: ${({ theme }) => theme.colors.darkGreen};
`;

const TotalPrice = styled.div`
    font-size: 20px;
    line-height: 24px;
    font-weight: 500;
    color: ${({ theme }) => theme.colors.darkGreen};
    margin-top: 4px;
`;

const CheckoutButton = styled.button`
    background-color: ${({ theme }) => theme.colors.green};
    height: 54px;
    color: #ffffff;
    border: none;
    padding: 14px 32px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 370;
    line-height: 20px;
    cursor: pointer;
    transition: background 0.2s;

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

    &:active {
        background-color: #4A8D19;
    }
`;