<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Магазин донатов</title>
    <style>
        .switch-button-container {
            display: flex;
            justify-content: center;
            gap: 10px;
            margin-bottom: 20px;
        }

        .switch-button {
            padding: 10px 240px;
            background-color: #03a9f4;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .buy-button {
            padding: 5px 10px;
            margin-top: 20px;
            background-color: #1e88e5;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .switch-button:hover {
            background-color: #1e88e5;
        }

        .switch-button.active {
            background-color: #1e88e5;
        }

        .hidden {
            display: none;
        }
.container {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    justify-content: center;
}

.card {
    border: 1px solid #ddd;
    border-radius: 10px;
    width: 200px;
    text-align: center;
    padding: 15px;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s, box-shadow 0.2s;
    cursor: pointer;
}

        .card:hover {
            transform: scale(1.05);
            box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2);
        }

        .card img {
            width: 100%;
            height: auto;
            border-radius: 10px;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            justify-content: center;
            align-items: center;
            backdrop-filter: blur(5px);
        }
        .modal-content {
            background-color: white;
            border-radius: 10px;
            width: 300px;
            padding: 20px;
            text-align: center;
        }
        .modal-content input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .modal-content button {
            padding: 10px 15px;
            border: none;
            border-radius: 5px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
        }
        .close-modal {
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 20px;
            color: #888;
            background: transparent;
            border: none;
            cursor: pointer;
        }
        .close-modal:hover {
            color: black;
        }
    </style>
</head>
<body>
    <div class="switch-button-container">
        <h1 id="shop-1-button" class="switch-button">АНАРХИЯ</h1>
    </div>
    <div id="shop-1" class="container"></div>
    <div class="switch-button-container">
        <h1 id="shop-2-button" class="switch-button">FFA</h1>
    </div>
    <div id="shop-2" class="container"></div>
    <div id="nicknameModal" class="modal">
        <div class="modal-content">
            <button class="close-modal" onclick="closeModal()">×</button>
            <h2 id="modalItemTitle"></h2>
            <p>Введите ваш ник:</p>
            <input type="text" id="nicknameInput" placeholder="Ваш ник">
            <button id="confirmPurchaseButton">Подтвердить</button>
        </div>
    </div>

<script>
    const shops = [
        { id: 1, container: 'shop-1', apiId: '199416' },
        { id: 2, container: 'shop-2', apiId: '227205' },
    ];

    let selectedItem = null;

    async function loadItems(shopApiId, containerId) {
        try {
            const response = await fetch(`https://api.trademc.org/shop.getItems?shop=${shopApiId}&v=3`);
            const data = await response.json();

            if (data.response && data.response.categories) {
                const container = document.getElementById(containerId);
                container.innerHTML = ''; 

                data.response.categories.forEach(category => {
                    category.items.forEach(item => {
                        const card = document.createElement('div');
                        card.className = 'card';
                        card.innerHTML = `
                            <img src="${item.image}" alt="${item.name}">
                            <h3>${item.name}</h3>
                            <p>Цена: ${item.cost} руб.</p>
                            <div class="switch-button-container">
                            <h3 class="buy-button">КУПИТЬ</h3>
                            </div>
                        `;
                        card.onclick = () => openModal(item);
                        container.appendChild(card);
                    });
                });
            } else if (data.error) {
                console.error('Ошибка API:', data.error.message);
            }
        } catch (error) {
            console.error('Ошибка загрузки данных:', error);
        }
    }

    function switchShop(shopNumber) {
        shops.forEach(shop => {
            document.getElementById(shop.container).classList.add('hidden');
        });
        const selectedShop = shops.find(shop => shop.id === shopNumber);
        if (selectedShop) {
            document.getElementById(selectedShop.container).classList.remove('hidden');
            loadItems(selectedShop.apiId, selectedShop.container);
        }
        document.querySelectorAll('.switch-button').forEach(button => button.classList.remove('active'));
        document.getElementById(`shop-${shopNumber}-button`).classList.add('active');
    }

    function openModal(item) {
        selectedItem = item;
        document.getElementById('modalItemTitle').textContent = `${item.name} - ${item.cost} RUB`;
        document.getElementById('nicknameModal').style.display = 'flex';
    }

    function closeModal() {
        document.getElementById('nicknameModal').style.display = 'none';
    }

    async function initiatePurchase() {
        const nickname = document.getElementById('nicknameInput').value.trim();

        if (!nickname) {
            alert('Пожалуйста, введите ник!');
            return;
        }

        closeModal();
        try {
            const response = await fetch(`https://api.trademc.org/shop.buyItems?items=${selectedItem.id}:1&buyer=${encodeURIComponent(nickname)}&v=3`);
            const data = await response.json();

            if (data.response) {
                const cartId = data.response.cart_id;
                const successUrl = 'https://treexmine.ru/success';
                const pendingUrl = 'https://treexmine.ru/pending';
                const failUrl = 'https://treexmine.ru/fail';

                const paymentUrl = `https://pay.trademc.org?cart_id=${cartId}&success_url=${encodeURIComponent(successUrl)}&pending_url=${encodeURIComponent(pendingUrl)}&fail_url=${encodeURIComponent(failUrl)}`;
                window.location.href = paymentUrl;
            } else if (data.error) {
                alert(`Ошибка при покупке: ${data.error.message}`);
            }
        } catch (error) {
            console.error('Ошибка покупки:', error);
        }
    }

    document.getElementById('confirmPurchaseButton').onclick = initiatePurchase;
    switchShop(1);
    switchShop(2);
</script>

</body>
</html>
