<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fixtures - Fixture Manager</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f5f5f5;
        }
        .header {
            background-color: #007bff;
            color: white;
            padding: 1rem 2rem;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .logo {
            font-size: 1.5rem;
            font-weight: bold;
        }
        .nav {
            display: flex;
            gap: 1rem;
        }
        .nav a {
            color: white;
            text-decoration: none;
            padding: 0.5rem 1rem;
            border-radius: 4px;
            transition: background-color 0.3s;
        }
        .nav a:hover {
            background-color: rgba(255,255,255,0.1);
        }
        .container {
            max-width: 1200px;
            margin: 2rem auto;
            padding: 0 2rem;
        }
        .content {
            background: white;
            padding: 2rem;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        .btn {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            display: inline-block;
            margin: 0.5rem 0;
            border: none;
            cursor: pointer;
            font-size: 0.9rem;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn-danger {
            background-color: #dc3545;
        }
        .btn-danger:hover {
            background-color: #c82333;
        }
        .btn-sm {
            padding: 4px 8px;
            font-size: 0.8rem;
            margin: 0 2px;
        }
        .alert {
            padding: 12px;
            margin-bottom: 1rem;
            border-radius: 4px;
        }
        .alert-error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .alert-success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .controls {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 2rem;
            flex-wrap: wrap;
            gap: 1rem;
        }
        .search-filters {
            display: flex;
            gap: 1rem;
            align-items: center;
            flex-wrap: wrap;
        }
        .search-filters input, .search-filters select {
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 1rem;
        }
        th, td {
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        th {
            background-color: #f8f9fa;
            font-weight: bold;
        }
        .status-active {
            color: #28a745;
            font-weight: bold;
        }
        .status-partial {
            color: #ffc107;
            font-weight: bold;
        }
        .status-inactive {
            color: #dc3545;
            font-weight: bold;
        }
        .fixture-row {
            cursor: pointer;
            transition: background-color 0.2s;
        }
        .fixture-row:hover {
            background-color: #f8f9fa;
        }
        .pagination {
            display: flex;
            justify-content: center;
            align-items: center;
            margin-top: 2rem;
            gap: 0.5rem;
        }
        .pagination a, .pagination span {
            padding: 8px 12px;
            text-decoration: none;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        .pagination a:hover {
            background-color: #f8f9fa;
        }
        .pagination .current {
            background-color: #007bff;
            color: white;
            border-color: #007bff;
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="logo">🥊 Fixture Manager</div>
        <div class="nav">
            <a href="{{ url_for('main.dashboard') }}">Dashboard</a>
            <a href="{{ url_for('main.fixtures') }}">Fixtures</a>
            <a href="{{ url_for('main.uploads') }}">Uploads</a>
            <a href="{{ url_for('main.statistics') }}">Statistics</a>
            {% if current_user.is_admin %}
                <a href="{{ url_for('main.admin_panel') }}">Admin</a>
            {% endif %}
            <a href="{{ url_for('auth.logout') }}">Logout</a>
        </div>
    </div>

    <div class="container">
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ 'error' if category == 'error' else 'success' }}">{{ message }}</div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <div class="content">
            <h1>Fixtures</h1>
            
            <div class="controls">
                <a href="{{ url_for('main.dashboard') }}" class="btn">← Back to Dashboard</a>
                
                <div class="search-filters">
                    <form method="GET" style="display: flex; gap: 1rem; align-items: center;">
                        <input type="text" name="search" placeholder="Search fixtures..." 
                               value="{{ search_query or '' }}" style="width: 200px;">
                        <select name="status">
                            <option value="">All Fixtures</option>
                            <option value="active" {{ 'selected' if status_filter == 'active' }}>Has Active Matches</option>
                            <option value="inactive" {{ 'selected' if status_filter == 'inactive' }}>No Active Matches</option>
                            <option value="complete" {{ 'selected' if status_filter == 'complete' }}>All Matches Active</option>
                        </select>
                        <button type="submit" class="btn">Filter</button>
                        {% if search_query or status_filter %}
                            <a href="{{ url_for('main.fixtures') }}" class="btn" style="background-color: #6c757d;">Clear</a>
                        {% endif %}
                    </form>
                </div>
            </div>
            
            {% if fixtures %}
            <table>
                <thead>
                    <tr>
                        <th>Fixture ID</th>
                        <th>Filename</th>
                        <th>Upload Date</th>
                        <th>Matches</th>
                        <th>Status</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for fixture in fixtures %}
                    <tr class="fixture-row" onclick="window.location.href='{{ url_for('main.fixture_detail', fixture_id=fixture.fixture_id) }}'">
                        <td>{{ fixture.fixture_id[:8] }}...</td>
                        <td>{{ fixture.filename }}</td>
                        <td>{{ fixture.upload_date.strftime('%Y-%m-%d %H:%M') if fixture.upload_date else 'N/A' }}</td>
                        <td>{{ fixture.match_count }} matches</td>
                        <td>
                            {% if fixture.active_matches == fixture.match_count %}
                                <span class="status-active">All Active ({{ fixture.active_matches }}/{{ fixture.match_count }})</span>
                            {% elif fixture.active_matches > 0 %}
                                <span class="status-partial">Partial ({{ fixture.active_matches }}/{{ fixture.match_count }})</span>
                            {% else %}
                                <span class="status-inactive">Inactive (0/{{ fixture.match_count }})</span>
                            {% endif %}
                        </td>
                        <td onclick="event.stopPropagation();">
                            <a href="{{ url_for('main.fixture_detail', fixture_id=fixture.fixture_id) }}" class="btn btn-sm">View</a>
                            <form method="POST" action="{{ url_for('main.delete_fixture', fixture_id=fixture.fixture_id) }}" 
                                  style="display: inline;" 
                                  onsubmit="return confirm('Are you sure you want to delete this fixture and all {{ fixture.match_count }} matches? This action cannot be undone.');">
                                <button type="submit" class="btn btn-danger btn-sm">Delete</button>
                            </form>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
            
            <!-- Pagination -->
            {% if pagination and pagination.pages > 1 %}
            <div class="pagination">
                {% if pagination.has_prev %}
                    <a href="{{ url_for('main.matches', page=pagination.prev_num, search=search_query, status=status_filter) }}">« Previous</a>
                {% endif %}
                
                {% for page_num in range(1, pagination.pages + 1) %}
                    {% if page_num == pagination.page %}
                        <span class="current">{{ page_num }}</span>
                    {% else %}
                        <a href="{{ url_for('main.matches', page=page_num, search=search_query, status=status_filter) }}">{{ page_num }}</a>
                    {% endif %}
                {% endfor %}
                
                {% if pagination.has_next %}
                    <a href="{{ url_for('main.matches', page=pagination.next_num, search=search_query, status=status_filter) }}">Next »</a>
                {% endif %}
            </div>
            {% endif %}
            
            {% else %}
            <div style="text-align: center; padding: 3rem;">
                <h3>No fixtures found</h3>
                <p>Upload a fixture file to get started.</p>
                <a href="{{ url_for('upload.upload_fixture') }}" class="btn">Upload Fixture File</a>
            </div>
            {% endif %}
        </div>
    </div>
</body>
</html>