{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}

{% block title %}{% trans "Observations Console" %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .filter-panel {
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .filter-panel.collapsed .filter-body {
        display: none;
    }
    .table-toolbar {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 15px;
        margin-bottom: 15px;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    .status-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .status-new { background: #e3f2fd; color: #1976d2; }
    .status-triaged { background: #e0f7fa; color: #00838f; }
    .status-assigned { background: #e0f7fa; color: #00838f; }
    .status-in_progress { background: #fff3e0; color: #f57c00; }
    .status-resolved { background: #e8f5e9; color: #388e3c; }
    .status-closed { background: #f5f5f5; color: #616161; }
    .status-rejected { background: #ffebee; color: #d32f2f; }
    .status-duplicate { background: #f5f5f5; color: #616161; }
    
    .severity-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .severity-low { background: #e8f5e9; color: #388e3c; }
    .severity-medium { background: #fff3e0; color: #f57c00; }
    .severity-high { background: #ffebee; color: #d32f2f; }
    .severity-critical { background: #880e4f; color: #fff; }
    
    .anonymous-badge {
        background: #e9ecef;
        color: #6c757d;
        padding: 2px 8px;
        border-radius: 10px;
        font-size: 0.75rem;
        font-style: italic;
    }
    
    .observation-row:hover {
        background: #f8f9fa;
        cursor: pointer;
    }
    
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
    
    .tracking-code {
        font-family: 'Courier New', monospace;
        font-weight: 600;
        color: #667eea;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-eye-fill text-primary me-2"></i>
                {% trans "Observations Console" %}
            </h2>
            <p class="text-muted mb-0">{% trans "Manage and triage staff-reported observations" %}</p>
        </div>
        <div class="d-flex gap-2">
            <a href="{% url 'observations:category_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-tags me-1"></i> {% trans "Categories" %}
            </a>
            <a href="{% url 'observations:observation_create_public' %}" class="btn btn-primary" target="_blank">
                <i class="bi bi-plus-circle me-1"></i> {% trans "Public Form" %}
            </a>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card border-primary">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Total" %}</h6>
                            <h3 class="mb-0">{{ stats.total }}</h3>
                        </div>
                        <div class="text-primary">
                            <i class="bi bi-list-ul" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card border-info">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "New" %}</h6>
                            <h3 class="mb-0">{{ stats.new }}</h3>
                        </div>
                        <div class="text-info">
                            <i class="bi bi-inbox" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card border-warning">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "In Progress" %}</h6>
                            <h3 class="mb-0">{{ stats.in_progress }}</h3>
                        </div>
                        <div class="text-warning">
                            <i class="bi bi-hourglass-split" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card stat-card border-secondary">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Anonymous" %}</h6>
                            <h3 class="mb-0">{{ stats.anonymous_count }}</h3>
                        </div>
                        <div class="text-secondary">
                            <i class="bi bi-incognito" style="font-size: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Panel -->
    <div class="filter-panel" id="filterPanel">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">
                <i class="bi bi-funnel me-2"></i>{% trans "Filters" %}
            </h5>
            <button class="btn btn-sm btn-outline-secondary" onclick="toggleFilters()">
                <i class="bi bi-chevron-up" id="filterToggleIcon"></i>
            </button>
        </div>
        
        <div class="filter-body">
            <form method="get" action="{% url 'observations:observation_list' %}" id="filterForm">
                <div class="row g-3">
                    <!-- Search -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Search" %}</label>
                        <input type="text" class="form-control" name="search" 
                               placeholder="{% trans 'Tracking code, description...' %}" 
                               value="{{ filters.search }}">
                    </div>
                    
                    <!-- Status -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Status" %}</label>
                        <select class="form-select" name="status">
                            <option value="">{% trans "All Statuses" %}</option>
                            {% for value, label in status_choices %}
                            <option value="{{ value }}" {% if filters.status == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Severity -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Severity" %}</label>
                        <select class="form-select" name="severity">
                            <option value="">{% trans "All Severities" %}</option>
                            <option value="low" {% if filters.severity == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                            <option value="medium" {% if filters.severity == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                            <option value="high" {% if filters.severity == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                            <option value="critical" {% if filters.severity == 'critical' %}selected{% endif %}>{% trans "Critical" %}</option>
                        </select>
                    </div>
                    
                    <!-- Category -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Category" %}</label>
                        <select class="form-select" name="category">
                            <option value="">{% trans "All Categories" %}</option>
                            {% for cat in categories %}
                            <option value="{{ cat.id }}" {% if filters.category == cat.id|stringformat:"s" %}selected{% endif %}>
                                {{ cat.name_en }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Department -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Department" %}</label>
                        <select class="form-select" name="assigned_department">
                            <option value="">{% trans "All Departments" %}</option>
                            {% for dept in departments %}
                            <option value="{{ dept.id }}" {% if filters.assigned_department == dept.id|stringformat:"s" %}selected{% endif %}>
                                {{ dept.name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Assigned To -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Assigned To" %}</label>
                        <select class="form-select" name="assigned_to">
                            <option value="">{% trans "All Users" %}</option>
                            {% for user_obj in assignable_users %}
                            <option value="{{ user_obj.id }}" {% if filters.assigned_to == user_obj.id|stringformat:"s" %}selected{% endif %}>
                                {{ user_obj.get_full_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <!-- Anonymous Filter -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Reporter Type" %}</label>
                        <select class="form-select" name="is_anonymous">
                            <option value="">{% trans "All" %}</option>
                            <option value="yes" {% if filters.is_anonymous == 'yes' %}selected{% endif %}>{% trans "Anonymous Only" %}</option>
                            <option value="no" {% if filters.is_anonymous == 'no' %}selected{% endif %}>{% trans "Identified Only" %}</option>
                        </select>
                    </div>
                    
                    <!-- Date Range -->
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Date From" %}</label>
                        <input type="date" class="form-control" name="date_from" value="{{ filters.date_from }}">
                    </div>
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Date To" %}</label>
                        <input type="date" class="form-control" name="date_to" value="{{ filters.date_to }}">
                    </div>
                </div>
                
                <div class="mt-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-search me-1"></i> {% trans "Apply Filters" %}
                    </button>
                    <a href="{% url 'observations:observation_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> {% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Table Toolbar -->
    <div class="table-toolbar">
        <div>
            <span class="text-muted">
                {% trans "Showing" %} {{ page_obj.start_index }} {% trans "to" %} {{ page_obj.end_index }} {% trans "of" %} {{ page_obj.paginator.count }} {% trans "observations" %}
            </span>
        </div>
    </div>

    <!-- Observations Table -->
    <div class="card">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>{% trans "Tracking Code" %}</th>
                            <th>{% trans "Date" %}</th>
                            <th>{% trans "Category" %}</th>
                            <th>{% trans "Description" %}</th>
                            <th>{% trans "Severity" %}</th>
                            <th>{% trans "Status" %}</th>
                            <th>{% trans "Reporter" %}</th>
                            <th>{% trans "Department" %}</th>
                            <th>{% trans "Assigned To" %}</th>
                            <th>{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for observation in observations %}
                        <tr class="observation-row" onclick="window.location='{% url 'observations:observation_detail' observation.id %}'">
                            <td>
                                <span class="tracking-code">{{ observation.tracking_code }}</span>
                            </td>
                            <td>
                                <small>{{ observation.created_at|date:"M d, Y" }}</small><br>
                                <small class="text-muted">{{ observation.created_at|time:"H:i" }}</small>
                            </td>
                            <td>
                                {% if observation.category %}
                                <span class="badge bg-secondary">{{ observation.category.name_en }}</span>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                <div style="max-width: 200px;">
                                    {% if observation.title %}
                                    <strong>{{ observation.title|truncatewords:5 }}</strong><br>
                                    {% endif %}
                                    <small class="text-muted">{{ observation.description|truncatewords:10 }}</small>
                                </div>
                            </td>
                            <td>
                                <span class="severity-badge severity-{{ observation.severity }}">
                                    {{ observation.get_severity_display }}
                                </span>
                            </td>
                            <td>
                                <span class="status-badge status-{{ observation.status }}">
                                    {{ observation.get_status_display }}
                                </span>
                            </td>
                            <td>
                                {% if observation.is_anonymous %}
                                <span class="anonymous-badge">{% trans "Anonymous" %}</span>
                                {% else %}
                                <small>{{ observation.reporter_display }}</small>
                                {% endif %}
                            </td>
                            <td>
                                {% if observation.assigned_department %}
                                <small>{{ observation.assigned_department.name|truncatewords:3 }}</small>
                                {% else %}
                                <span class="text-muted">-</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if observation.assigned_to %}
                                <small>{{ observation.assigned_to.get_full_name }}</small>
                                {% else %}
                                <span class="text-muted"><em>{% trans "Unassigned" %}</em></span>
                                {% endif %}
                            </td>
                            <td onclick="event.stopPropagation();">
                                <div class="btn-group btn-group-sm">
                                    <a href="{% url 'observations:observation_detail' observation.id %}" 
                                       class="btn btn-outline-primary" title="{% trans 'View' %}">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="10" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">{% trans "No observations found" %}</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Observations pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page=1{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-left"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-right"></i>
                </a>
            </li>
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i class="bi bi-chevron-double-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('filterPanel');
    const icon = document.getElementById('filterToggleIcon');
    panel.classList.toggle('collapsed');
    icon.classList.toggle('bi-chevron-up');
    icon.classList.toggle('bi-chevron-down');
}
</script>
{% endblock %}
