{% extends "base.html" %}

{% block title %}Consumption Sheets - ScanLook{% endblock %}

{% block content %}
<div class="dashboard-container">
    <div class="page-header">
        <div>
            <a href="{{ url_for('home') }}" class="breadcrumb">← Back to Home</a>
            <h1 class="page-title">Consumption Sheets</h1>
            <p class="page-subtitle">Scan and record lot consumption</p>
        </div>
    </div>

    <!-- New Session Button -->
    <div class="new-session-section">
        <h2 class="section-title">Start New Session</h2>
        <div class="process-buttons">
            {% for p in processes %}
            <a href="{{ url_for('cons_sheets.new_session', process_id=p.id) }}" class="btn btn-primary">
                <span class="btn-icon">+</span> {{ p.process_name }}
            </a>
            {% endfor %}
            {% if not processes %}
            <p class="empty-text">No process types configured yet. Contact your administrator.</p>
            {% endif %}
        </div>
    </div>

    <!-- Active Sessions -->
    {% if sessions %}
    <div class="section-card">
        <h2 class="section-title">📋 My Active Sessions</h2>
        <div class="sessions-list">
            {% for s in sessions %}
            <div class="session-list-item-container">
                <a href="{{ url_for('cons_sheets.scan_session', session_id=s.id) }}" class="session-list-item">
                    <div class="session-list-info">
                        <h3 class="session-list-name">{{ s.process_name }}</h3>
                        <div class="session-list-meta">
                            <span>Started: {{ s.created_at[:16] }}</span>
                            <span>•</span>
                            <span>{{ s.scan_count or 0 }} lots scanned</span>
                        </div>
                    </div>
                    <div class="session-list-action">
                        <span class="arrow-icon">→</span>
                    </div>
                </a>
                <button class="btn-archive" onclick="archiveSession(this)" data-id="{{ s.id }}" data-name="{{ s.process_name }}" title="Archive this session">
                    🗑️
                </button>
            </div>
            {% endfor %}
        </div>
    </div>
    {% else %}
    <div class="empty-state">
        <div class="empty-icon">📝</div>
        <h2 class="empty-title">No Active Sessions</h2>
        <p class="empty-text">Start a new session by selecting a process type above</p>
    </div>
    {% endif %}
</div>

<style>
.new-session-section {
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-lg);
    padding: var(--space-xl);
    margin-bottom: var(--space-xl);
}

.process-buttons {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-md);
    margin-top: var(--space-md);
}

.section-card {
    margin-top: var(--space-xl);
}

.session-list-item-container {
    display: flex;
    align-items: stretch;
    gap: var(--space-sm);
    margin-bottom: var(--space-sm);
}

.session-list-item {
    flex: 1;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-lg);
    text-decoration: none;
    transition: var(--transition);
}

.session-list-item:hover {
    border-color: var(--color-primary);
    transform: translateX(4px);
}

.session-list-name {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--color-text);
    margin: 0 0 var(--space-xs) 0;
}

.session-list-meta {
    display: flex;
    gap: var(--space-sm);
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

.arrow-icon {
    font-size: 1.5rem;
    color: var(--color-primary);
}

.btn-archive {
    background: var(--color-surface);
    border: 2px solid var(--color-border);
    border-radius: var(--radius-md);
    padding: var(--space-md);
    cursor: pointer;
    font-size: 1.25rem;
    transition: var(--transition);
}

.btn-archive:hover {
    border-color: var(--color-danger);
    background: rgba(255, 51, 102, 0.1);
}
</style>

<script>
function archiveSession(sessionId, processName) {
    if (!confirm(`Archive this ${processName} session?\n\nYou can still view it later from the admin panel.`)) {
        return;
    }
    
    fetch(`/cons-sheets/session/${sessionId}/archive`, {
        method: 'POST',
        headers: {'Content-Type': 'application/json'}
    })
    .then(r => r.json())
    .then(data => {
        if (data.success) {
            location.reload();
        } else {
            alert(data.message || 'Error archiving session');
        }
    });
}
</script>
{% endblock %}
