{% extends 'main/header.html' %}

{% load static %}
{% load category_filters %}
{% load custom_filters %}

{% block content %}

<div class="main-content">
    <main class="content-area">
        <div class="page-header">
            <h1 class="lists-main-title">Мои листи</h1>
            <div class="header-decoration"></div>
        </div>
        <div class="lists-actions">
            <button id="create-list-btn" class="action-button">
                <i class="fas fa-plus"></i> Нова листа
            </button>
            <div id="create-list-form" class="create-list-form">
                <form method="post" action="{% url 'create_list' %}" id="list-form">
                    {% csrf_token %}
                    <div class="form-group">
                        <input type="text" name="list_name" id="list-name-input" placeholder="Име на листата" required
                               class="form-input" maxlength="100">
                        <small id="list-name-error" style="color: red; display: none;"></small>
                    </div>
                    <div class="form-actions">
                        <button type="submit" class="action-button">Креирај</button>
                        <button type="button" id="cancel-create" class="action-button secondary-button">Откажи</button>
                    </div>
                </form>
            </div>
        </div>
        <div class="lists-grid">
            {% for list in user_lists %}
                <div class="list-card">
                    <a href="{% url 'view_list' list.id %}" class="list-card-link">
                        <div class="list-icon">
                            <i class="fas fa-list-ul"></i>
                        </div>
                        <h3 class="list-title">{{ list.name }}</h3>
                        <div class="list-meta">
                                <span class="meta-item">
                                    <i class="fas fa-shopping-basket"></i> {{ list.items.count }} продукти
                                </span>
                            <span class="meta-item">
                                    <i class="far fa-calendar-alt"></i> {{ list.created_at|date:"d.m.Y" }}
                                </span>
                        </div>
                    </a>
                    <form method="post" action="{% url 'delete_list' list.id %}" class="delete-list-form">
                        {% csrf_token %}
                        <button type="submit" class="action-button danger-button" onclick="return confirm('Дали сте сигурни дека сакате да ја избришете оваа листа?')">
                            <i class="fas fa-trash-alt"></i>
                        </button>
                    </form>
                </div>
            {% empty %}
                <div class="empty-lists-message">
                    <div class="empty-icon">
                        <i class="far fa-folder-open"></i>
                    </div>
                    <h3>Немате креирано листи</h3>
                    <p>Кликнете на "Нова листа" за да креирате ваша прва листа</p>
                </div>
            {% endfor %}
        </div>
    </main>
</div>

<!-- Footer -->
<footer>
    <div class="footer-content">
        <div class="footer-section">
            <h3>За нас</h3>
            <p>Ние сме водечка онлајн продавница со најдобри цени и квалитетни производи.</p>
        </div>
        <div class="footer-section">
            <h3>Контакт</h3>
            <p>Телефон: +389 70 123 456</p>
            <p>Email: kontakt@nasataprodavnica.com</p>
            <p>Адреса: Улица ББ, Скопје</p>
        </div>
        <div class="footer-section">
            <h3>Брзи линкови</h3>
            <a href="{% url 'home' %}">Дома</a>
            <a href="{% url 'product_list' %}">Каталог</a>
            <a href="#">Услови на користење</a>
            <a href="#">Политика на приватност</a>
        </div>
        <div class="footer-section social">
            <h3>Следете нè</h3>
            <a href="#" target="_blank"><i class="fab fa-facebook-f"></i> Facebook</a>
            <a href="#" target="_blank"><i class="fab fa-instagram"></i> Instagram</a>
            <a href="#" target="_blank"><i class="fab fa-twitter"></i> Twitter</a>
        </div>
    </div>
    <div class="copyright">
        © 2023 Нашата Продавница. Сите права се задржани.
    </div>
</footer>

<script>
    document.addEventListener('DOMContentLoaded', function () {
        // Create list form toggle
        const createBtn = document.getElementById('create-list-btn');
        const createForm = document.getElementById('create-list-form');
        const cancelBtn = document.getElementById('cancel-create');
        const listForm = document.getElementById('list-form');
        const listNameInput = document.getElementById('list-name-input');
        const listNameError = document.getElementById('list-name-error');

        if (createBtn && createForm && cancelBtn) {
            createBtn.addEventListener('click', function () {
                createForm.style.display = 'block';
                this.style.display = 'none';
                listNameInput.focus();
            });

            cancelBtn.addEventListener('click', function () {
                createForm.style.display = 'none';
                createBtn.style.display = 'inline-flex';
                listNameError.style.display = 'none';
                listNameInput.value = '';
            });
        }

        // List form submission with AJAX
        if (listForm) {
            listForm.addEventListener('submit', function(e) {
                e.preventDefault();

                const formData = new FormData(this);
                const listName = formData.get('list_name').trim();

                // Validate list name
                if (listName.length === 0) {
                    listNameError.textContent = 'Внесете име за листата';
                    listNameError.style.display = 'block';
                    return;
                }

                if (listName.length > 100) {
                    listNameError.textContent = 'Името не смее да биде подолго од 100 карактери';
                    listNameError.style.display = 'block';
                    return;
                }

                fetch(this.action, {
                    method: 'POST',
                    body: formData,
                    headers: {
                        'X-CSRFToken': formData.get('csrfmiddlewaretoken'),
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        // Reload the page to show the new list
                        window.location.reload();
                    } else {
                        // Show error message
                        listNameError.textContent = data.error || 'Настана грешка. Обидете се повторно.';
                        listNameError.style.display = 'block';
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    listNameError.textContent = 'Настана грешка. Обидете се повторно.';
                    listNameError.style.display = 'block';
                });
            });
        }

        // Clear error when typing
        if (listNameInput) {
            listNameInput.addEventListener('input', function() {
                listNameError.style.display = 'none';
            });
        }

        // Auto-dismiss messages
        setTimeout(() => {
            document.querySelectorAll('.alert').forEach(alert => {
                alert.style.opacity = '0';
                setTimeout(() => alert.remove(), 300);
            });
        }, 5000);

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

<style>
    /* Common Styles */
    body {
        font-family: Arial, sans-serif;
        margin: 0;
        padding: 0;
        display: flex;
        flex-direction: column;
        min-height: 100vh;
        background-color: #f5f5f5;
    }

    /* Main Content */
    .main-content {
        max-width: 1200px;
        margin: 20px auto;
        padding: 0 15px;
        flex-grow: 1;
    }

    .content-area {
        background-color: white;
        border-radius: 8px;
        padding: 20px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .page-header {
        text-align: center;
        margin-bottom: 20px;
    }

    .lists-main-title {
        font-size: 1.5rem;
        font-weight: 600;
        color: #2e652e;
        margin-bottom: 10px;
    }

    .header-decoration {
        height: 4px;
        width: 80px;
        background: linear-gradient(to right, #2e652e, #4caf50);
        margin: 0 auto;
        border-radius: 2px;
    }

    .lists-actions {
        margin-bottom: 20px;
        text-align: center;
    }

    .action-button {
        padding: 10px 25px;
        font-size: 1rem;
        background-color: #2e652e;
        color: white;
        border: none;
        border-radius: 30px;
        font-weight: 600;
        cursor: pointer;
        transition: background-color 0.3s ease, transform 0.3s ease;
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .action-button:hover {
        background-color: #1f3f1f;
        transform: translateY(-2px);
    }

    .secondary-button {
        background-color: #f5f5f5;
        color: #2e652e;
    }

    .secondary-button:hover {
        background-color: #e0e8e0;
    }

    .danger-button {
        background-color: #e74c3c;
    }

    .danger-button:hover {
        background-color: #c62828;
    }

    .create-list-form {
        max-width: 500px;
        margin: 20px auto;
        padding: 15px;
        background: #f9f9f9;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        display: none;
    }

    .form-group {
        margin-bottom: 15px;
    }

    .form-input {
        width: 100%;
        padding: 12px 15px;
        border: 1px solid #ddd;
        border-radius: 6px;
        font-size: 1rem;
        background-color: #f5f5f5;
    }

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

    .form-actions {
        display: flex;
        gap: 10px;
        justify-content: flex-end;
    }

    .lists-grid {
        display: grid;
        grid-template-columns: repeat(3, 1fr); /* Exactly 3 columns */
        gap: 30px;
    }

    .list-card {
        background: white;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        transition: transform 0.3s ease, box-shadow 0.3s ease;
        position: relative;
        padding-right: 50px;
    }

    .list-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    .list-card-link {
        text-decoration: none;
        color: inherit;
        display: block;
        padding: 15px;
    }

    .list-icon {
        width: 40px;
        height: 40px;
        background-color: #f0f7f0;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 10px;
        color: #2e652e;
        font-size: 1rem;
    }

    .list-title {
        font-weight: 500;
        margin-bottom: 8px;
        font-size: 14px;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
    }

    .list-meta {
        display: flex;
        flex-direction: column;
        gap: 5px;
        font-size: 12px;
        color: #666;
    }

    .meta-item {
        display: flex;
        align-items: center;
        gap: 5px;
    }

    .delete-list-form {
        position: absolute;
        top: 10px;
        right: 10px;
    }

    .delete-list-form .action-button {
        padding: 5px 10px;
        font-size: 0.85rem;
    }

    .empty-lists-message {
        grid-column: 1 / -1;
        text-align: center;
        padding: 20px;
        background: white;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .empty-icon {
        font-size: 2rem;
        margin-bottom: 10px;
        color: #2e652e;
    }

    .empty-lists-message h3 {
        font-size: 1.2rem;
        margin-bottom: 10px;
        color: #2e652e;
    }

    .empty-lists-message p {
        font-size: 0.9rem;
        color: #666;
    }

    /* Footer */
    footer {
        background: linear-gradient(135deg, #2e652e, #1f3f1f);
        color: white;
        padding: 40px 20px;
        margin-top: auto;
    }

    .footer-content {
        max-width: 1200px;
        margin: 0 auto;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
        gap: 30px;
    }

    .footer-section h3 {
        margin-bottom: 20px;
        font-size: 1.2rem;
        color: #fdd835;
    }

    .footer-section p, .footer-section a {
        color: #ddd;
        margin-bottom: 10px;
        display: block;
        text-decoration: none;
        transition: color 0.3s ease;
        font-size: 14px;
    }

    .footer-section a:hover {
        color: #fff;
        text-decoration: underline;
    }

    .footer-section.social a {
        display: inline-flex;
        align-items: center;
        gap: 8px;
    }

    .copyright {
        text-align: center;
        padding-top: 20px;
        margin-top: 20px;
        border-top: 1px solid rgba(255, 255, 255, 0.2);
        color: #bbb;
        font-size: 0.9rem;
    }

    /* Responsiveness */
    @media (max-width: 768px) {
        .main-content {
            padding: 10px;
        }

        .content-area {
            padding: 15px;
            border-radius: 15px;
        }

        .lists-main-title {
            font-size: 1.2rem;
        }

        .lists-grid {
            grid-template-columns: 1fr;
            gap: 15px;
        }

        .list-card {
            border-radius: 15px;
        }

        .list-icon {
            width: 50px;
            height: 50px;
            font-size: 1.2rem;
        }

        .list-title {
            font-size: 16px;
        }

        .list-meta {
            font-size: 14px;
        }

        .action-button {
            width: 100%;
            justify-content: center;
            padding: 12px;
            border-radius: 25px;
        }

        .form-actions {
            flex-direction: column;
        }

        .form-actions button {
            width: 100%;
        }

        .create-list-form {
            padding: 20px;
            border-radius: 15px;
        }

        .form-input {
            padding: 10px 15px;
        }
    }

    @media (max-width: 480px) {
        .lists-main-title {
            font-size: 1rem;
        }

        .list-icon {
            width: 40px;
            height: 40px;
            font-size: 1rem;
        }

        .list-title {
            font-size: 14px;
        }

        .list-meta {
            font-size: 12px;
        }

        .empty-icon {
            font-size: 1.5rem;
        }

        .empty-lists-message h3 {
            font-size: 1rem;
        }

        .empty-lists-message p {
            font-size: 0.8rem;
        }
    }
</style>
{% endblock %}