{% extends "base.html" %}

{% block title %}{{ unit_id }} - Sound Level Meter{% endblock %}

{% block content %}
<div class="mb-6">
    {% if from_project and project %}
        <nav class="flex items-center space-x-2 text-sm">
            <a href="/projects" class="text-gray-500 hover:text-seismo-orange">Projects</a>
            <svg class="w-4 h-4 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path>
            </svg>
            <a href="/projects/{{ from_project }}" class="text-seismo-orange hover:text-seismo-orange-dark flex items-center">
                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
                </svg>
                {{ project.name }}
            </a>
        </nav>
    {% else %}
        <a href="#" onclick="goBack(event)" class="text-seismo-orange hover:text-seismo-orange-dark flex items-center">
            <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path>
            </svg>
            <span id="back-link-text">Back to Sound Level Meters</span>
        </a>
    {% endif %}

    <script>
    function goBack(event) {
        event.preventDefault();

        // Check if there's a previous page in history
        // and it's from the same site (not external)
        if (window.history.length > 1 && document.referrer) {
            const referrer = new URL(document.referrer);
            const current = new URL(window.location.href);

            // If referrer is from the same origin, go back
            if (referrer.origin === current.origin) {
                window.history.back();
                return;
            }
        }

        // Otherwise, go to SLM dashboard
        window.location.href = '/sound-level-meters';
    }

    // Update the back link text based on referrer
    document.addEventListener('DOMContentLoaded', function() {
        const backText = document.getElementById('back-link-text');
        if (backText && document.referrer) {
            try {
                const referrer = new URL(document.referrer);
                const current = new URL(window.location.href);

                // Only update if from same origin
                if (referrer.origin === current.origin) {
                    if (referrer.pathname.includes('/sound-level-meters')) {
                        backText.textContent = 'Back to Sound Level Meters';
                    } else if (referrer.pathname.includes('/roster')) {
                        backText.textContent = 'Back to Roster';
                    } else if (referrer.pathname.includes('/projects')) {
                        backText.textContent = 'Back to Projects';
                    } else if (referrer.pathname === '/') {
                        backText.textContent = 'Back to Dashboard';
                    }
                }
            } catch (e) {
                // Invalid referrer, keep default text
            }
        }
    });
    </script>
</div>

<div class="mb-8">
    <div class="flex justify-between items-start">
        <div>
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white flex items-center">
                <svg class="w-8 h-8 mr-3 text-seismo-orange" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                          d="M9 19V6l12-3v13M9 19c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zm12-3c0 1.105-1.343 2-3 2s-3-.895-3-2 1.343-2 3-2 3 .895 3 2zM9 10l12-3">
                    </path>
                </svg>
                {{ unit_id }}
            </h1>
            <p class="text-gray-600 dark:text-gray-400 mt-1">
                {{ unit.slm_model or 'NL-43' }} Sound Level Meter
            </p>
        </div>
        <div class="flex gap-2">
            <span class="px-3 py-1 rounded-full text-sm font-medium
                         {% if unit.deployed %}bg-blue-100 text-blue-800 dark:bg-blue-900 dark:text-blue-200
                         {% else %}bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-200{% endif %}">
                {% if unit.deployed %}Deployed{% else %}Benched{% endif %}
            </span>
        </div>
    </div>
</div>

<!-- Command Center -->
<div class="bg-white dark:bg-slate-800 rounded-xl shadow-lg p-6">
    <div id="slm-command-center"
         hx-get="/api/slm-dashboard/live-view/{{ unit_id }}"
         hx-trigger="load"
         hx-swap="innerHTML">
        <div class="text-center py-8 text-gray-500">
            <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-seismo-orange mx-auto mb-4"></div>
            <p>Loading command center...</p>
        </div>
    </div>
</div>
{% endblock %}
