{% extends "base.html" %}

{% block title %}{{ _('User Report') }} - {{ app_name }}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <div>
                    <nav aria-label="breadcrumb">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item"><a href="{{ url_for('reports.reports') }}">{{ _('Reports') }}</a></li>
                            <li class="breadcrumb-item active">{{ _('User Report') }}</li>
                        </ol>
                    </nav>
                    <h1 class="h3 mb-0">
                        <i class="fas fa-chart-pie text-primary"></i> {{ _('User Report') }}
                    </h1>
                </div>
                <div>
                    <a href="{{ url_for('reports.export_csv') }}?{{ request.query_string.decode() }}" class="btn btn-outline-primary">
                        <i class="fas fa-download"></i> {{ _('Export CSV') }}
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Filters -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white">
                    <h5 class="mb-0">
                        <i class="fas fa-filter"></i> {{ _('Filters') }}
                    </h5>
                </div>
                <div class="card-body">
                    <form method="GET" class="row g-3">
                        <div class="col-md-3">
                            <label for="start_date" class="form-label">{{ _('Start Date') }}</label>
                            <input type="date" class="form-control" id="start_date" name="start_date" 
                                   value="{{ request.args.get('start_date', '') }}">
                        </div>
                        <div class="col-md-3">
                            <label for="end_date" class="form-label">{{ _('End Date') }}</label>
                            <input type="date" class="form-control" id="end_date" name="end_date" 
                                   value="{{ request.args.get('end_date', '') }}">
                        </div>
                        <div class="col-md-3">
                            <label for="user" class="form-label">{{ _('User') }}</label>
                            <select class="form-select" id="user" name="user_id">
                                <option value="">{{ _('All Users') }}</option>
                                {% for user in users %}
                                <option value="{{ user.id }}" {% if request.args.get('user_id')|int == user.id %}selected{% endif %}>
                                    {{ user.display_name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-md-3">
                            <label for="project" class="form-label">{{ _('Project') }}</label>
                            <select class="form-select" id="project" name="project_id">
                                <option value="">{{ _('All Projects') }}</option>
                                {% for project in projects %}
                                <option value="{{ project.id }}" {% if request.args.get('project_id')|int == project.id %}selected{% endif %}>
                                    {{ project.name }}
                                </option>
                                {% endfor %}
                            </select>
                        </div>
                        <div class="col-12">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> {{ _('Apply Filters') }}
                            </button>
                            <a href="{{ url_for('reports.user_report') }}" class="btn btn-outline-secondary">
                                <i class="fas fa-times"></i> {{ _('Clear') }}
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <!-- Summary Statistics -->
    <div class="row mb-4">
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="summary-icon bg-primary bg-opacity-10 text-primary">
                                <i class="fas fa-clock"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <div class="summary-label">{{ _('Total Hours') }}</div>
                            <div class="summary-value">{{ "%.1f"|format(summary.total_hours) }}h</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="summary-icon bg-success bg-opacity-10 text-success">
                                <i class="fas fa-dollar-sign"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <div class="summary-label">{{ _('Billable Hours') }}</div>
                            <div class="summary-value">{{ "%.1f"|format(summary.billable_hours) }}h</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="summary-icon bg-info bg-opacity-10 text-info">
                                <i class="fas fa-users"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <div class="summary-label">{{ _('Users') }}</div>
                            <div class="summary-value">{{ summary.users_count }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-3 col-sm-6 mb-3">
            <div class="card border-0 shadow-sm h-100 summary-card">
                <div class="card-body p-3">
                    <div class="d-flex align-items-center">
                        <div class="flex-shrink-0">
                            <div class="summary-icon bg-warning bg-opacity-10 text-warning">
                                <i class="fas fa-project-diagram"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <div class="summary-label">{{ _('Projects') }}</div>
                            <div class="summary-value">{{ summary.projects_count }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- User Breakdown -->
    <div class="row">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0">
                        <i class="fas fa-list"></i> {{ _('User Breakdown') }} ({{ user_totals|length }})
                    </h5>
                </div>
                <div class="card-body p-0">
                    {% if user_totals %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>{{ _('User') }}</th>
                                    <th>{{ _('Total Hours') }}</th>
                                    <th>{{ _('Billable Hours') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for username, totals in user_totals.items() %}
                                <tr>
                                    <td><strong>{{ username }}</strong></td>
                                    <td>{{ "%.1f"|format(totals.hours) }}h</td>
                                    <td>
                                        {% if totals.billable_hours > 0 %}
                                        <span class="text-success">{{ "%.1f"|format(totals.billable_hours) }}h</span>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <div class="empty-state">
                            <i class="fas fa-users fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">{{ _('No Data Found') }}</h5>
                            <p class="text-muted">{{ _('Try adjusting your filters.') }}</p>
                        </div>
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

    <!-- Time Entries -->
    {% if entries %}
    <div class="row mt-4">
        <div class="col-12">
            <div class="card shadow-sm border-0">
                <div class="card-header bg-white py-3">
                    <h5 class="mb-0">
                        <i class="fas fa-clock"></i> {{ _('Time Entries') }} ({{ entries|length }})
                    </h5>
                </div>
                <div class="card-body p-0">
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>{{ _('User') }}</th>
                                    <th>{{ _('Project') }}</th>
                                    <th>{{ _('Date') }}</th>
                                    <th>{{ _('Time') }}</th>
                                    <th>{{ _('Duration') }}</th>
                                    <th>{{ _('Notes') }}</th>
                                    <th>{{ _('Tags') }}</th>
                                    <th>{{ _('Billable') }}</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for entry in entries %}
                                <tr>
                                    <td>{{ entry.user.display_name }}</td>
                                    <td>
                                        <a href="{{ url_for('projects.view_project', project_id=entry.project.id) }}">
                                            {{ entry.project.name }}
                                        </a>
                                    </td>
                                                                <td>{{ entry.start_time.strftime('%Y-%m-%d') }}</td>
                            <td>
                                {{ entry.start_time.strftime('%H:%M') }} -
                                {% if entry.end_time %}
                                {{ entry.end_time.strftime('%H:%M') }}
                                {% else %}
                                <span class="text-warning">{{ _('Running') }}</span>
                                {% endif %}
                            </td>
                                    <td>
                                        <strong>{{ entry.duration_formatted }}</strong>
                                    </td>
                                    <td>
                                        {% if entry.notes %}
                                        <span title="{{ entry.notes }}">{{ entry.notes[:50] }}{% if entry.notes|length > 50 %}...{% endif %}</span>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if entry.tag_list %}
                                        {% for tag in entry.tag_list %}
                                        <span class="badge bg-light text-dark">{{ tag }}</span>
                                        {% endfor %}
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if entry.billable %}
                                        <span class="badge bg-success">{{ _('Yes') }}</span>
                                        {% else %}
                                        <span class="badge bg-secondary">{{ _('No') }}</span>
                                        {% endif %}
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}


