<!-- bedrock_server_manager//web/templates/select_world.html -->
{# Extends the base layout template #}
{% extends "base.html" %}

{# --- Page Title Block --- #}
{% block title %}{{ super() }} - Manage World: {{ server_name }}{% endblock %}

{# --- Main Content Block --- #}
{% block content %}
    {# Main heading for the page #}
<h2>Manage World</h2>

    {# Display server context #}
<p style="margin-bottom: 15px;">Server: <strong>{{ server_name }}</strong></p>

    {# --- Status Message Area --- #}
    {# Targeted by JavaScript (showStatusMessage) #}
<div id="status-message-area" class="message-box" style="opacity: 0; transition: opacity 0.5s ease-out;">
    <!-- Status messages from import/export actions will appear here -->
</div>

    {# Server-side flash messages (handled by base.html) #}

    {# --- Current World Section --- #}
<div class="server-card">

    {# --- Export World Section --- #}
    <section class="export-world-section action-section">
        {# Added action-section class for consistency #}
        <h4>Export Current World</h4>
        <p>Create a <code class="endpoint-path">.mcworld</code> from the server's current world.</p>
        <button type="button" id="export-world-btn" class="action-button export-button">
            Export Current World
        </button>
    </section>

    {# --- Reset World Section --- #}
    <section class="reset-world-section action-section">
        {# Added action-section class for consistency #}
        <h4>Reset Current World</h4>
        <p>Deletes the server's current world.</p>
        <button type="button" id="reset-world-btn" class="action-button danger-button">
            Reset Current World
        </button>
    </section>

</div> {# End of sections-container #}

    {# --- Separator after BOTH sections --- #}
<hr style="margin: 20px 0;">

    {# --- Import World Section --- #}
<section class="select-world-section list-section" id="content-management-page" data-server-name="{{ server_name | e }}">
    {# Use list-section class #}
    <h4>Import World File</h4> {# Added heading for clarity #}

    {# Check if world_files list (passed from route) is not empty #}
    {% if world_files %}
    <p>
        Select a world file (<code class="endpoint-path">.mcworld</code>) to import into server '{{ server_name }}'.<br>
        <strong style="color: #ef9a9a;">Warning:</strong> This will replace the server's current world data!
    </p>

    {# Responsive wrapper for the table #}
    <div class="table-responsive-wrapper">
        <table class="server-table">
            {# Use standard table styling #}
            <thead>
                <tr>
                    <th scope="col">Available World File</th> {# Header for the filename #}
                    <th scope="col" style="width: 150px; text-align: center;">Action</th> {# Header for the button #}
                </tr>
            </thead>
            <tbody>
                {# Loop through the list of full world file paths #}
                {% for world_file_path in world_files %}
                <tr>
                    {# Display only the filename part #}
                    {# IMPORTANT: Assumes 'basename' filter is registered in Flask Jinja env #}
                    <td data-label="World File">{{ world_file_path | basename }}</td>
                    {# Cell containing the import button #}
                    <td data-label="Action" style="text-align: center;">
                        <button type="button"
                                class="action-button import-button import-world-btn"
                                data-world-path="{{ world_file_path | e | replace('\\', '\\\\') }}">
                            Import
                        </button>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div> {# --- End .table-responsive-wrapper --- #}

    {% else %}
    {# Message displayed if no world files are found #}
    {# Use consistent styling class #}
    <p class="no-servers-message">
        {# Or use the table row version if preferred #}
        No world files (<code class="endpoint-path">.mcworld</code>) found in the configured <code class="endpoint-path">content/worlds/</code> directory.
    </p>
    {% endif %}

</section> {# --- End .select-world-section --- #}

    {# --- Navigation Link Back --- #}
    {# Always show the back button #}
<div class="navigation-link" style="margin-top: 25px;">
    {# Link back to the main dashboard #}
    <a href="{{ request.url_for('index') }}" class="action-link">
        {# Assuming 'index' is the name of the main dashboard route #}
        « Back to Dashboard
    </a>
</div>

{% endblock %}

{% block body_scripts %}
<script src="{{ request.url_for('static', path='js/content_management.js') }}" defer></script>
{% endblock %}