{% extends "base.html" %}

{% block title %}Project 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">Project Report</li>
                        </ol>
                    </nav>
                    <h1 class="h3 mb-0">
                        <i class="fas fa-chart-bar text-primary"></i> Project 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="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-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-12">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-search"></i> Apply Filters
                            </button>
                            <a href="{{ url_for('reports.project_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-success bg-opacity-10 text-success">
                                <i class="fas fa-sack-dollar"></i>
                            </div>
                        </div>
                        <div class="flex-grow-1 ms-3">
                            <div class="summary-label">Billable Amount</div>
                            <div class="summary-value">{{ currency }} {{ "%.2f"|format(summary.total_billable_amount) }}</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-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>

    <!-- Project 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> Project Breakdown ({{ projects_data|length }})
                    </h5>
                </div>
                <div class="card-body p-0">
                    {% if projects_data %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>Project</th>
                                    <th>Client</th>
                                    <th>Total Hours</th>
                                    <th>Billable Hours</th>
                                    <th>Billable Amount</th>
                                    <th>Users</th>
                                    <th>Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for project in projects_data %}
                                <tr>
                                    <td>
                                        <div>
                                            <strong>{{ project.name }}</strong>
                                            {% if project.description %}
                                            <br><small class="text-muted">{{ project.description[:50] }}{% if project.description|length > 50 %}...{% endif %}</small>
                                            {% endif %}
                                        </div>
                                    </td>
                                    <td>
                                        {% if project.client %}
                                        {{ project.client }}
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <strong>{{ "%.1f"|format(project.total_hours) }}h</strong>
                                    </td>
                                    <td>
                                        {% if project.billable %}
                                        <span class="text-success">{{ "%.1f"|format(project.billable_hours) }}h</span>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if project.billable and project.billable_amount > 0 %}
                                        <span class="text-success">{{ currency }} {{ "%.2f"|format(project.billable_amount) }}</span>
                                        {% else %}
                                        <span class="text-muted">-</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        <div class="d-flex flex-column">
                                            {% for user_total in project.user_totals %}
                                            <small>
                                                {{ user_total.username }}: {{ "%.1f"|format(user_total.hours) }}h
                                            </small>
                                            {% endfor %}
                                        </div>
                                    </td>
                                    <td>
                                        <div class="btn-group" role="group">
                                            <a href="{{ url_for('projects.view_project', project_id=project.id) }}" 
                                               class="btn btn-sm btn-outline-primary" title="View Project">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            <a href="{{ url_for('reports.project_report') }}?project_id={{ project.id }}&{{ request.query_string.decode() }}" 
                                               class="btn btn-sm btn-outline-secondary" title="Filter by Project">
                                                <i class="fas fa-filter"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <div class="empty-state">
                            <i class="fas fa-chart-bar fa-3x text-muted mb-3"></i>
                            <h5 class="text-muted">No Data Found</h5>
                            <p class="text-muted">
                            {% if request.args.get('start_date') or request.args.get('end_date') or request.args.get('project_id') or request.args.get('user_id') %}
                            Try adjusting your filters or 
                            <a href="{{ url_for('reports.project_report') }}">view all projects</a>.
                            {% else %}
                            No time entries have been recorded yet.
                            {% endif %}
                            </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 %}
