{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">
    <title>Title</title>
    <style>
        /* Header */
        .main-header {
            background-color: white;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-top {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 20px;
            border-bottom: 1px solid #e5e5e5;
        }

        .logo img {
            height: 60px;
        }

        .header-search {
            flex-grow: 1;
            margin: 0 20px;
            position: relative;
        }

        .header-search input {
            width: 97%;
            padding: 12px 20px;
            border: 1px solid #ddd;
            border-radius: 30px;
            font-size: 16px;
            background-color: #f5f5f5;
        }

        .header-search button {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            background: none;
            border: none;
            color: #666;
            cursor: pointer;
            font-size: 18px;
        }

        .user-actions {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-actions a {
            color: #333;
            text-decoration: none;
            font-size: 14px;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .user-actions a:hover {
            color: #2e652e;
        }

        /* Auth Form Styles */
        .auth-form {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .auth-btn {
            padding: 8px 16px;
            border-radius: 20px;
            font-weight: 600;
            text-decoration: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
            transition: all 0.3s ease;
        }

        .auth-btn.logout {
            background-color: #2e652e;
            color: white;
            border: none;
            cursor: pointer;
        }

        .auth-btn.logout:hover {
            background-color: #1f3f1f;
            transform: translateY(-1px);
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }

        .auth-btn:not(.logout) {
            color: #333;
            background-color: #f5f5f5;
        }

        .auth-btn:not(.logout):hover {
            background-color: #e0e0e0;
        }

        /* User greeting styles */
        .user-greeting {
            font-weight: 600;
            color: #2e652e;
            margin-right: 10px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            max-width: 150px;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        /* List Selector Styles */
        .list-selector-container {
            position: relative;
            min-width: 180px;
        }

        .list-selector {
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            width: 100%;
            padding: 8px 35px 8px 15px;
            border: 1px solid #ddd;
            border-radius: 20px;
            background-color: #f5f5f5;
            font-size: 14px;
            color: #333;
            cursor: pointer;
            transition: all 0.3s ease;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%232e652e' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 12px;
        }

        .list-selector:focus {
            outline: none;
            border-color: #2e652e;
            box-shadow: 0 0 0 2px rgba(46, 101, 46, 0.2);
        }

        .list-selector:hover {
            background-color: #e8f5e9;
        }

        .list-selector option {
            padding: 8px;
            background-color: white;
        }

        /* Navigation */
        .main-nav {
            padding: 15px 30px;
            background-color: #2e652e;
        }

        .main-nav ul {
            display: flex;
            list-style: none;
            margin: 0;
            padding: 0;
        {#justify-content: space-between;#}
        }

        .main-nav li {
            margin-right: 20px;
        }

        .main-nav a {
            color: white;
            text-decoration: none;
            font-weight: 600;
            padding: 5px 0;
            position: relative;
        }

        .main-nav a:hover {
            color: #fdd835;
        }

        .main-nav a::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 0;
            height: 2px;
            background-color: #fdd835;
            transition: width 0.3s ease;
        }

        .main-nav a:hover::after {
            width: 100%;
        }

        /* Mobile Menu */
        #mobile-header {
            display: none;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            background: linear-gradient(135deg, #2e652e, #1f3f1f);
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
            z-index: 1001;
            position: relative;
        }

        #mobile-logo img {
            height: 50px;
        }

        .menu-toggle {
            font-size: 28px;
            color: white;
            cursor: pointer;
            transition: transform 0.3s ease;
        }

        .menu-toggle:hover {
            transform: scale(1.1);
        }

        .mobile-menu {
            position: fixed;
            top: 0;
            right: -100%;
            width: 75%;
            height: 100vh;
            background: linear-gradient(135deg, #2e652e, #1f3f1f);
            box-shadow: -2px 0 10px rgba(0, 0, 0, 0.3);
            transition: right 0.3s ease;
            padding: 20px;
            z-index: 1002;
            display: flex;
            flex-direction: column;
        }

        .mobile-menu.open {
            right: 0;
        }

        .mobile-menu-header {
            display: flex;
            justify-content: flex-end;
            margin-bottom: 20px;
        }

        .close-btn {
            cursor: pointer;
            font-size: 28px;
            color: #fdd835;
            transition: color 0.3s ease;
        }

        .close-btn:hover {
            color: white;
        }

        .mobile-search-form {
            display: flex;
            align-items: center;
            padding: 12px 16px;
            gap: 10px;
            background: white;
            border-radius: 25px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            margin-bottom: 20px;
        }

        .mobile-search-form input {
            flex: 1;
            padding: 10px 15px;
            border: none;
            border-radius: 20px;
            font-size: 14px;
            outline: none;
            background: #f5f5f5;
        }

        .mobile-search-form button {
            background-color: #2e652e;
            color: white;
            border: none;
            padding: 10px 15px;
            border-radius: 50%;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s ease, transform 0.3s ease;
            margin-left: -9px;
        }

        .mobile-search-form button:hover {
            background-color: #1f3f1f;
            transform: scale(1.1);
        }

        .mobile-nav {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .mobile-nav a {
            color: white;
            text-decoration: none;
            font-size: 18px;
            padding: 10px 15px;
            border-radius: 8px;
            transition: background-color 0.3s ease, color 0.3s ease;
        }

        .mobile-nav a:hover {
            background-color: #fdd835;
            color: #2e652e;
            transform: translateX(5px);
        }

        /* User info in mobile menu */
        .mobile-user-info {
            padding: 15px;
            margin-bottom: 10px;
            background-color: rgba(255, 255, 255, 0.1);
            border-radius: 8px;
            color: white;
        }

        .mobile-user-info .username {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 5px;
        }

        .mobile-user-info .email {
            font-size: 14px;
            opacity: 0.8;
        }

        /* Mobile List Selector */
        .mobile-list-selector-container {
            margin-bottom: 20px;
        }

        .mobile-list-selector {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 25px;
            background-color: white;
            font-size: 16px;
            color: #333;
            cursor: pointer;
            transition: all 0.3s ease;
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%232e652e' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 15px center;
            background-size: 12px;
        }

        .mobile-list-selector:focus {
            outline: none;
            border-color: #2e652e;
            box-shadow: 0 0 0 2px rgba(46, 101, 46, 0.2);
        }

        #overlay {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100vw;
            height: 100vh;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
        }

        #overlay.active {
            display: block;
        }

        /* Mobile Bottom Navigation */
        .mobile-bottom-nav {
            display: none;
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            background-color: white;
            box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
            z-index: 999;
            padding: 8px 0;
        }

        .mobile-bottom-nav-container {
            display: flex;
            justify-content: space-around;
            align-items: center;
        }

        .mobile-bottom-nav-btn {
            display: flex;
            flex-direction: column;
            align-items: center;
            text-decoration: none;
            color: #555;
            font-size: 12px;
            padding: 5px 0;
            flex: 1;
        }

        .mobile-bottom-nav-btn i {
            font-size: 20px;
            margin-bottom: 4px;
            color: #2e652e;
        }

        .mobile-bottom-nav-btn.active {
            color: #2e652e;
            font-weight: bold;
        }

        .mobile-bottom-nav-btn.active i {
            color: #2e652e;
        }

        .mobile-bottom-nav-selector {
            flex: 2;
            padding: 0 10px;
        }

        .mobile-bottom-list-selector {
            width: 100%;
            padding: 8px 15px;
            border: 1px solid #ddd;
            border-radius: 20px;
            background-color: #f5f5f5;
            font-size: 14px;
            color: #333;
            cursor: pointer;
            appearance: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%232e652e' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
            background-repeat: no-repeat;
            background-position: right 10px center;
            background-size: 10px;
        }

        @media (max-width: 768px) {
            .main-header {
                display: none;
            }

            #mobile-header {
                display: flex;
            }

            .mobile-bottom-nav {
                display: block;
            }

            .list-selector-container {
                min-width: 150px;
            }

            .auth-form {
                flex-direction: column;
                gap: 10px;
                width: 100%;
            }

            .list-selector-container {
                width: 100%;
            }

            .auth-btn.logout {
                width: 100%;
                justify-content: center;
            }

            /* Add padding to content to prevent bottom nav overlap */
            body {
                padding-bottom: 60px;
            }
        }

        @media (max-width: 1024px) {
            .header-search input {
                width: 96%;
            }
        }
    </style>
</head>
<body>

<!-- Header -->
<header class="main-header">
    <div class="header-top">
        <div class="logo">
            <a href="{% url 'home' %}"><img src="{% static 'images/shtedko_official1.png' %}" alt="Штедко лого"/></a>
        </div>
        <div class="header-search">
            <form method="GET" action="{% url 'product_list' %}" id="searchForm">
                <input type="text" name="search" id="searchInput" placeholder="Пребарај производи..."
                       value="{{ request.GET.search }}">
                <button type="submit"><i class="fas fa-search"></i></button>
            </form>
            <div id="suggestions"
                 style="position: absolute; background: white; border: 1px solid #ddd; border-radius: 5px; width: 98%; max-height: 200px; overflow-y: auto; display: none; z-index: 2"></div>
        </div>
        <div class="user-actions">
            {% if user.is_authenticated %}
                <form method="post" action="{% url 'logout' %}" class="auth-form">
                    {% csrf_token %}
                    <div class="list-selector-container" style="display: flex; align-items: center; gap: 8px;">
                        <a href="{% url 'favorites_list' %}" title="Омилени"
                           style="color: #2e652e; margin-left: 20px; margin-right: 20px">
                            <i class="fas fa-heart" style="font-size: 18px;"></i>
                        </a>
                        <select id="listSelector" class="list-selector" style="margin-right: 20px">
                            <option value="">-- Избери листа --</option>
                        </select>
                    </div>

                    <button type="submit" class="auth-btn logout">
                        <i class="fas fa-sign-out-alt"></i> Одјави се
                    </button>
                </form>
            {% else %}
                <a href="{% url 'login' %}" class="auth-btn">
                    <i class="fas fa-user"></i> Најави се
                </a>
                <a href="{% url 'register' %}" class="auth-btn">
                    <i class="fas fa-user-plus"></i> Регистрирај се
                </a>
            {% endif %}
        </div>
    </div>
    <nav class="main-nav">
        <div id="flex" style="display: flex; justify-content: space-between">
            <ul>
                <li><a href="{% url 'product_list' %}">Каталог</a></li>
                <li style="z-index: 0"><a href="{% url 'stats' %}">Статистика</a></li>
                <li><a href="{% url 'view_lists' %}">Листи</a></li>
            </ul>
            <div style="display: flex">
                <span class="user-greeting" title="{{ user.username }}" style="color: white">
                            <i class="fas fa-user-circle"></i> {{ user.username|truncatechars:15 }}
                </span>
            </div>
        </div>
    </nav>
</header>

<!-- Mobile Menu -->
<div id="mobile-header">
    <div id="mobile-logo">
        <a href="{% url 'home' %}"><img src="{% static 'images/shtedko_official1.png' %}" alt="Штедко лого"></a>
    </div>
    <div class="menu-toggle" onclick="toggleMobileMenu()">☰</div>
</div>

<!-- Mobile Menu -->
<div id="mobile-menu" class="mobile-menu">
    <div class="mobile-menu-header">
        <span class="close-btn" onclick="toggleMobileMenu()">×</span>
    </div>

    {% if user.is_authenticated %}
        <div class="mobile-user-info">
            <div class="username">
                <i class="fas fa-user-circle"></i> {{ user.username }}
            </div>
            <div class="email">{{ user.email }}</div>
        </div>
    {% endif %}

    <form method="GET" action="{% url 'product_list' %}" class="mobile-search-form" id="mobileSearchForm">
        <input type="text" name="search" id="mobileSearchInput" placeholder="Пребарај производ..."
               value="{{ request.GET.search }}">
        <button type="submit"><i class="fas fa-search"></i></button>
    </form>

    <nav class="mobile-nav">
        <a href="{% url 'home' %}">Дома</a>
        <a href="{% url 'product_list' %}">Каталог</a>
        <a href="{% url 'stats' %}">Статистика</a>
        <a href="{% url 'view_lists' %}">Листи</a>

        {% if user.is_authenticated %}
            <a href="{% url 'logout' %}"><i class="fas fa-sign-out-alt"></i> Одјави се</a>
        {% else %}
            <a href="{% url 'login' %}"><i class="fas fa-sign-in-alt"></i> Најави се</a>
            <a href="{% url 'register' %}"><i class="fas fa-user-plus"></i> Регистрирај се</a>
        {% endif %}
    </nav>
</div>
<div id="overlay" onclick="toggleMobileMenu()"></div>

<!-- Mobile Bottom Navigation -->
<nav class="mobile-bottom-nav">
    <div class="mobile-bottom-nav-container">
        <a href="{% url 'favorites_list' %}" class="mobile-bottom-nav-btn">
            <i class="fas fa-heart"></i>
            <span>Омилени</span>
        </a>

        <div class="mobile-bottom-nav-selector">
            <select id="mobileBottomListSelector" class="mobile-bottom-list-selector">
                <option value="">-- Избери листа --</option>
            </select>
        </div>

        <a href="{% url 'view_lists' %}" class="mobile-bottom-nav-btn">
            <i class="fas fa-list"></i>
            <span>Листи</span>
        </a>
    </div>
</nav>

{% block content %}

{% endblock %}

<script>
    function toggleMobileMenu() {
        const menu = document.getElementById("mobile-menu");
        const overlay = document.getElementById("overlay");
        menu.classList.toggle("open");
        overlay.classList.toggle("active");
    }

    document.addEventListener('DOMContentLoaded', function () {
        // Search functionality
        const searchInput = document.getElementById('searchInput');
        const mobileSearchInput = document.getElementById('mobileSearchInput');
        const suggestionsDiv = document.getElementById('suggestions');
        const searchForm = document.getElementById('searchForm');
        const mobileSearchForm = document.getElementById('mobileSearchForm');

        function transliterateLatinToCyrillic(text) {
            const map = {
                'dzh': 'џ', 'dzs': 'џ', 'dsh': 'џ',
                'zh': 'ж', 'ch': 'ч', 'sh': 'ш', 'lj': 'љ', 'nj': 'њ', 'kj': 'ќ', 'dj': 'ѓ',
                'zs': 'ж', 'hs': 'ш', 'cx': 'ч', 'sx': 'ш', 'jx': 'ж',
                'tz': 'ц', 'ts': 'ц', 'tc': 'ц', 'dz': 'џ',
                'a': 'а', 'b': 'б', 'v': 'в', 'g': 'г', 'd': 'д', 'e': 'е', 'z': 'з', 'i': 'и',
                'j': 'ј', 'k': 'к', 'l': 'л', 'm': 'м', 'n': 'н', 'o': 'о', 'p': 'п', 'r': 'р',
                's': 'с', 't': 'т', 'u': 'у', 'f': 'ф', 'h': 'х', 'c': 'ц',
                'y': 'ј', 'w': 'в', 'x': 'кс', 'q': 'к',
                'ia': 'ја', 'ie': 'је', 'io': 'јо', 'iu': 'ју'
            };

            const typoPatterns = [
                {pattern: /sampon/gi, replace: 'shampon'},
                {pattern: /cresi/gi, replace: 'creshi'},
                {pattern: /stipki/gi, replace: 'shtipki'},
                {pattern: /sch/gi, replace: 'sh'},
                {pattern: /ck/gi, replace: 'k'},
                {pattern: /ph/gi, replace: 'f'},
                {pattern: /th/gi, replace: 't'},
                {pattern: /([a-z]),([a-z])/gi, replace: '$1$2'},
                {pattern: /([a-z])\.([a-z])/gi, replace: '$1$2'},
                {pattern: /([a-z])\1/gi, replace: '$1'}
            ];

            let normalizedText = text.toLowerCase();
            for (const {pattern, replace} of typoPatterns) {
                normalizedText = normalizedText.replace(pattern, replace);
            }

            let result = '';
            let i = 0;
            while (i < normalizedText.length) {
                if (map[normalizedText.substring(i, i + 3)]) {
                    result += map[normalizedText.substring(i, i + 3)];
                    i += 3;
                } else if (map[normalizedText.substring(i, i + 2)]) {
                    result += map[normalizedText.substring(i, i + 2)];
                    i += 2;
                } else if (map[normalizedText[i]]) {
                    result += map[normalizedText[i]];
                    i++;
                } else {
                    result += normalizedText[i];
                    i++;
                }
            }
            return result;
        }

        function setupSearch(input, form, isMobile = false) {
            form.addEventListener('submit', function (e) {
                e.preventDefault();

                const originalQuery = input.value.trim();
                const cyrillicQuery = transliterateLatinToCyrillic(originalQuery);

                const originalInput = document.createElement('input');
                originalInput.type = 'hidden';
                originalInput.name = 'original_search';
                originalInput.value = originalQuery;
                form.appendChild(originalInput);

                const cyrillicInput = document.createElement('input');
                cyrillicInput.type = 'hidden';
                cyrillicInput.name = 'cyrillic_search';
                cyrillicInput.value = cyrillicQuery;
                form.appendChild(cyrillicInput);

                form.submit();
            });

            input.addEventListener('input', function () {
                const query = this.value.trim();
                if (query.length < 2) {
                    suggestionsDiv.style.display = 'none';
                    return;
                }

                fetch(`/search-suggestions/?q=${encodeURIComponent(query)}`)
                    .then(response => response.json())
                    .then(data => {
                        suggestionsDiv.innerHTML = '';
                        if (data.suggestions.length > 0) {
                            data.suggestions.forEach(suggestion => {
                                const div = document.createElement('div');
                                div.textContent = suggestion;
                                div.style.padding = '5px 10px';
                                div.style.cursor = 'pointer';
                                div.addEventListener('click', function () {
                                    input.value = suggestion;
                                    suggestionsDiv.style.display = 'none';
                                    form.dispatchEvent(new Event('submit'));
                                });
                                suggestionsDiv.appendChild(div);
                            });
                            suggestionsDiv.style.display = 'block';
                        } else {
                            suggestionsDiv.style.display = 'none';
                        }
                    });
            });

            input.addEventListener('keydown', function (e) {
                if (e.key === 'Enter') {
                    e.preventDefault();
                    suggestionsDiv.style.display = 'none';
                    form.dispatchEvent(new Event('submit'));
                }
            });

            if (isMobile) {
                const searchButton = form.querySelector('button[type="submit"]');
                if (searchButton) {
                    searchButton.addEventListener('click', function (e) {
                        e.preventDefault();
                        form.dispatchEvent(new Event('submit'));
                    });
                }
            }
        }

        setupSearch(searchInput, searchForm);
        setupSearch(mobileSearchInput, mobileSearchForm, true);

        document.addEventListener('click', function (e) {
            if (!searchInput.contains(e.target) &&
                !mobileSearchInput.contains(e.target) &&
                !suggestionsDiv.contains(e.target)) {
                suggestionsDiv.style.display = 'none';
            }
        });
    });

    document.addEventListener("DOMContentLoaded", function () {
        fetch('/api/lists/')
            .then(response => response.json())
            .then(data => {
                const dropdown = document.getElementById('listSelector');
                const mobileDropdown = document.getElementById('mobileListSelector');
                const mobileBottomDropdown = document.getElementById('mobileBottomListSelector');

                data.lists.forEach(list => {
                    const option = document.createElement('option');
                    option.value = list.id;
                    option.text = list.name;

                    if (dropdown) dropdown.appendChild(option);
                    if (mobileDropdown) {
                        const mobileOption = option.cloneNode(true);
                        mobileDropdown.appendChild(mobileOption);
                    }
                    if (mobileBottomDropdown) {
                        const mobileBottomOption = option.cloneNode(true);
                        mobileBottomDropdown.appendChild(mobileBottomOption);
                    }
                });

                const savedListId = localStorage.getItem('selectedListId');
                if (savedListId) {
                    if (dropdown) dropdown.value = savedListId;
                    if (mobileDropdown) mobileDropdown.value = savedListId;
                    if (mobileBottomDropdown) mobileBottomDropdown.value = savedListId;
                }
            });

        const listSelectors = [
            document.getElementById('listSelector'),
            document.getElementById('mobileListSelector'),
            document.getElementById('mobileBottomListSelector')
        ].filter(el => el !== null);

        listSelectors.forEach(selector => {
            selector.addEventListener('change', function () {
                const selectedValue = this.value;
                localStorage.setItem('selectedListId', selectedValue);

                listSelectors.forEach(s => {
                    if (s !== this) {
                        s.value = selectedValue;
                    }
                });
            });
        });
    });

    document.addEventListener('DOMContentLoaded', function () {
        const addToListButtons = document.querySelectorAll('.add-to-list-btn');

        addToListButtons.forEach(button => {
            button.addEventListener('click', async () => {
                const productId = button.dataset.productId;
                const selectedListId = document.getElementById('listSelector') ?
                    document.getElementById('listSelector').value :
                    document.getElementById('mobileListSelector').value;

                if (!selectedListId) {
                    alert("Ве молиме изберете листа.");
                    return;
                }

                try {
                    const response = await fetch('/api/lists/add-product/', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                            'X-CSRFToken': getCookie('csrftoken'),
                        },
                        body: JSON.stringify({
                            product_id: productId,
                            list_id: selectedListId
                        })
                    });

                    const result = await response.json();
                    if (!result.success) {
                        alert("Грешка: " + result.message);
                    }
                } catch (error) {
                    console.error("Fetch error:", error);
                    alert("Настана грешка. Обидете се повторно.");
                }
            });
        });

        function getCookie(name) {
            let cookieValue = null;
            if (document.cookie && document.cookie !== '') {
                const cookies = document.cookie.split(';');
                for (let i = 0; i < cookies.length; i++) {
                    const cookie = cookies[i].trim();
                    if (cookie.substring(0, name.length + 1) === (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    });

    document.querySelectorAll('.add-to-list-btn').forEach(button => {
        button.addEventListener('click', function () {
            button.classList.remove('pulse-on-click');
            void button.offsetWidth;
            button.classList.add('pulse-on-click');
        });
    });

    document.addEventListener('DOMContentLoaded', function () {
        if (typeof initializePriceChart === 'function') {
            initializePriceChart();
        }
    });
</script>
</body>
</html>