<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Торренты: {{ movie_title }}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            margin-bottom: 20px;
        }
        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #007bff;
            text-decoration: none;
            padding: 8px 16px;
            border: 1px solid #007bff;
            border-radius: 5px;
        }
        .back-link:hover {
            background-color: #007bff;
            color: white;
        }
        .movie-info {
            background-color: #e9ecef;
            padding: 15px;
            border-radius: 5px;
            margin-bottom: 20px;
        }
        .torrents-list {
            display: grid;
            gap: 15px;
        }
        .torrent-item {
            border: 1px solid #ddd;
            border-radius: 8px;
            padding: 20px;
            background: white;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            transition: box-shadow 0.3s ease;
        }
        .torrent-item:hover {
            box-shadow: 0 4px 10px rgba(0,0,0,0.15);
        }
        .torrent-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #333;
        }
        .torrent-details {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-bottom: 15px;
        }
        .detail-item {
            display: flex;
            flex-direction: column;
        }
        .detail-label {
            font-size: 12px;
            color: #666;
            text-transform: uppercase;
            margin-bottom: 4px;
        }
        .detail-value {
            font-size: 14px;
            font-weight: bold;
            color: #333;
        }
        .resolution-1080p { color: #28a745; }
        .resolution-720p { color: #ffc107; }
        .resolution-480p { color: #dc3545; }
        .resolution-2160p { color: #6f42c1; }
        
        .torrent-actions {
            display: flex;
            gap: 10px;
            margin-top: 15px;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            text-decoration: none;
            display: inline-block;
            font-size: 14px;
            transition: background-color 0.3s ease;
        }
        .btn-primary {
            background-color: #007bff;
            color: white;
        }
        .btn-primary:hover {
            background-color: #0056b3;
        }
        .btn-success {
            background-color: #28a745;
            color: white;
        }
        .btn-success:hover {
            background-color: #1e7e34;
        }
        .btn-secondary {
            background-color: #6c757d;
            color: white;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .no-torrents {
            text-align: center;
            color: #666;
            font-size: 18px;
            padding: 40px;
        }
        .quality-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 12px;
            font-weight: bold;
            text-transform: uppercase;
        }
        .quality-bluray { background-color: #007bff; color: white; }
        .quality-web-dl { background-color: #28a745; color: white; }
        .quality-hdtv { background-color: #ffc107; color: #333; }
    </style>
</head>
<body>
    <div class="container">
        <a href="/" class="back-link">← Назад к поиску</a>
        
        <h1>🔍 Торренты для: "{{ movie_title }}"</h1>
        
        <div class="movie-info">
            <strong>Фильм:</strong> {{ movie_title }}
            {% if year %}
                <br><strong>Год:</strong> {{ year }}
            {% endif %}
        </div>
        
        {% if torrents %}
            <div class="torrents-list">
                {% for torrent in torrents %}
                <div class="torrent-item">
                    <div class="torrent-title">{{ torrent.title }}</div>
                    
                    <div class="torrent-details">
                        <div class="detail-item">
                            <div class="detail-label">Размер</div>
                            <div class="detail-value">{{ torrent.size_readable }}</div>
                        </div>
                        
                        <div class="detail-item">
                            <div class="detail-label">Разрешение</div>
                            <div class="detail-value resolution-{{ torrent.resolution }}">{{ torrent.resolution }}</div>
                        </div>
                        
                        <div class="detail-item">
                            <div class="detail-label">Качество</div>
                            <div class="detail-value">
                                <span class="quality-badge quality-{{ torrent.quality.lower() }}">{{ torrent.quality }}</span>
                            </div>
                        </div>
                        
                        <div class="detail-item">
                            <div class="detail-label">Сиды</div>
                            <div class="detail-value">{{ torrent.seeds }}</div>
                        </div>
                        
                        <div class="detail-item">
                            <div class="detail-label">Пиры</div>
                            <div class="detail-value">{{ torrent.peers }}</div>
                        </div>
                        
                        {% if torrent.category %}
                        <div class="detail-item">
                            <div class="detail-label">Категория</div>
                            <div class="detail-value">{{ torrent.category }}</div>
                        </div>
                        {% endif %}
                        
                        {% if torrent.date %}
                        <div class="detail-item">
                            <div class="detail-label">Дата</div>
                            <div class="detail-value">{{ torrent.date }}</div>
                        </div>
                        {% endif %}
                        
                        {% if torrent.provider %}
                        <div class="detail-item">
                            <div class="detail-label">Провайдер</div>
                            <div class="detail-value">{{ torrent.provider }}</div>
                        </div>
                        {% endif %}
                    </div>
                    
                    <div class="torrent-actions">
                        <a href="{{ torrent.magnet }}" class="btn btn-primary">Magnet</a>
                        <a href="{{ torrent.download_url }}" class="btn btn-success">Скачать .torrent</a>
                        <button onclick="addToTorrentClient('{{ torrent.id }}')" class="btn btn-secondary">Добавить в клиент</button>
                    </div>
                </div>
                {% endfor %}
            </div>
        {% else %}
            <div class="no-torrents">
                Торренты не найдены. Попробуйте изменить поисковый запрос.
            </div>
        {% endif %}
    </div>

    <script>
        function addToTorrentClient(torrentId) {
            // Показываем индикатор загрузки
            const button = event.target;
            const originalText = button.textContent;
            button.textContent = '⏳ Получаем magnet...';
            button.disabled = true;
            
            // Отправляем ID торрента для получения magnet-ссылки
            const formData = new FormData();
            formData.append('torrent_id', torrentId);
            
            fetch('/api/add-torrent', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.status === 'success') {
                    alert('✅ ' + data.message);
                    button.textContent = '✅ Добавлено';
                    button.style.backgroundColor = '#28a745';
                } else {
                    alert('❌ ' + data.message);
                    button.textContent = originalText;
                    button.disabled = false;
                }
            })
            .catch(error => {
                alert('❌ Ошибка при добавлении торрента: ' + error);
                button.textContent = originalText;
                button.disabled = false;
            });
        }
    </script>
</body>
</html>
