<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>CSV Preview</title>
    {% include 'favicon.html' %}
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/bootstrap-icons.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/layout.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/csv_preview.css') }}" rel="stylesheet">
</head>
<body class="d-flex flex-column">
    {% include 'header.html' %}
    
    <main class="container-fluid mt-5 pt-3 px-4">
        <h1 class="mb-4">CSV Preview</h1>

        <!-- Display error messages -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                <div class="error-details mb-4">
                    <details open>
                        <summary>Errors found during submission (click to expand/collapse):</summary>
                        <ul class="mt-2 mb-0">
                            {% for category, message in messages %}
                                <li><strong>{{ category }}:</strong> {{ message }}</li>
                            {% endfor %}
                        </ul>
                    </details>
                </div>
            {% endif %}
        {% endwith %}

        <!-- Render tables for new, existing, and invalid entries -->
        {% for table_name, entries, editable, tableclass in [
            ('New Entries', new_entries, true, 'table-valid-entries'),
            ('Existing Entries', existing_entries, true, 'table-valid-entries'),
            ('Invalid Entries', invalid_entries, false, 'table-invalid-entries')
        ] %}
            {% if entries %}
                <div class="mb-4">
                    <h2 class="h4 mb-3">{{ table_name }}</h2>
                    {% if table_name == 'Invalid Entries' %}
                        <div class="alert alert-warning mb-3">
                            {% if mode == 'import' %}
                                These entries already exist and won't be imported
                            {% elif mode == 'edit' %}
                                These entries don't exist and can't be edited
                            {% endif %}
                        </div>
                    {% endif %}
                    
                    <div class="table-container">
                        <table class="table table-bordered table-hover mb-0 {{ tableclass }}">
                            <thead class="table-light">
                                <tr>
                                    {% for attrib in item_attributes %}
                                        <th data-attrib="{{ attrib.attrib_name }}">{{ attrib.display_name }}</th>
                                    {% endfor %}
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in entries %}
                                    <tr>
                                        {% for attrib in item_attributes %}
                                            <td {% if editable and not attrib.primary %}contenteditable="true" class="editable-cell"{% endif %}>
                                                {{ entry[attrib.attrib_name] }}
                                                {% if attrib.primary and editable %}
                                                    <input type="hidden" name="{{ attrib.attrib_name }}" value="{{ entry[attrib.attrib_name] }}">
                                                {% endif %}
                                            </td>
                                        {% endfor %}
                                    </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            {% endif %}
        {% endfor %}

        <!-- Sticky action buttons -->
        <div class="sticky-actions">
            <div class="d-flex justify-content-between">
                <button type="button" 
                        onclick="window.location.href='/'" 
                        class="btn btn-outline-secondary">
                    <i class="bi bi-x-circle me-1"></i>Cancel
                </button>
                
                {% if new_entries or existing_entries %}
                <form action="/confirm_save" method="POST" onsubmit="return collectEditedData(event)" class="mb-0">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-circle me-1"></i>Confirm and Save
                    </button>
                </form>
                {% endif %}
            </div>
        </div>
    </main>

    {% include 'footer.html' %}
    <!-- JavaScript -->
    <script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
    <script src="{{ url_for('static', filename='js/edited_csv.js') }}"></script>
</body>
</html>