{% extends "layout.html" %}

{% block content %}
<div class="time-entries-container">
    <!-- Header Section -->
    <div class="page-header">
        <div class="header-content">
            <div class="header-left">
                <h1 class="page-title">
                    <span class="page-icon"><i class="ti ti-clock"></i></span>
                    System Admin - Time Entries
                </h1>
                <p class="page-subtitle">View time entries across all companies</p>
            </div>
            <div class="header-actions">
                <a href="{{ url_for('system_admin.system_admin_dashboard') }}" class="btn btn-secondary">
                    <i class="ti ti-arrow-left"></i>
                    Back to Dashboard
                </a>
            </div>
        </div>
    </div>

    <!-- Filter Section -->
    <div class="card">
        <div class="card-header">
            <h2 class="card-title">
                <span class="icon"><i class="ti ti-filter"></i></span>
                Filter Time Entries
            </h2>
        </div>
        <div class="card-body">
        <form method="GET" class="filter-form">
            <div class="filter-group">
                <label for="company">Company:</label>
                <select name="company" id="company" class="form-control" onchange="this.form.submit()">
                    <option value="">All Companies</option>
                    {% for company in companies %}
                    <option value="{{ company.id }}" {% if current_company|string == company.id|string %}selected{% endif %}>
                        {{ company.name }}
                        {% if company.is_personal %}(Personal){% endif %}
                    </option>
                    {% endfor %}
                </select>
            </div>
            {% if current_company %}
            <a href="{{ url_for('system_admin.system_admin_time_entries') }}" class="btn btn-sm btn-outline">Clear Filter</a>
            {% endif %}
        </form>
        </div>
    </div>

    <!-- Time Entries Table -->
    {% if entries.items %}
    <div class="card">
        <div class="card-body no-padding">
        <table class="table">
            <thead>
                <tr>
                    <th>User</th>
                    <th>Company</th>
                    <th>Project</th>
                    <th>Arrival</th>
                    <th>Departure</th>
                    <th>Duration</th>
                    <th>Status</th>
                    <th>Notes</th>
                </tr>
            </thead>
            <tbody>
                {% for entry_data in entries.items %}
                {% set entry = entry_data[0] %}
                {% set username = entry_data[1] %}
                {% set company_name = entry_data[2] %}
                {% set project_name = entry_data[3] %}
                <tr class="{% if entry.is_paused %}paused-entry{% endif %}">
                    <td>
                        <strong>{{ username }}</strong>
                    </td>
                    <td>
                        <span class="company-name">{{ company_name }}</span>
                    </td>
                    <td>
                        {% if project_name %}
                            <span class="project-name">{{ project_name }}</span>
                        {% else %}
                            <span class="text-muted">No project</span>
                        {% endif %}
                    </td>
                    <td>{{ entry.arrival_time.strftime('%Y-%m-%d %H:%M') }}</td>
                    <td>
                        {% if entry.departure_time %}
                            {{ entry.departure_time.strftime('%Y-%m-%d %H:%M') }}
                        {% else %}
                            <span class="text-muted">Still working</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if entry.duration %}
                            {% set hours = entry.duration // 3600 %}
                            {% set minutes = (entry.duration % 3600) // 60 %}
                            <span class="duration">{{ hours }}h {{ minutes }}m</span>
                        {% else %}
                            <span class="text-muted">Ongoing</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if entry.is_paused %}
                            <span class="status-badge status-paused">Paused</span>
                        {% elif not entry.departure_time %}
                            <span class="status-badge status-active">Active</span>
                        {% else %}
                            <span class="status-badge status-completed">Completed</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if entry.notes %}
                            <span class="notes" title="{{ entry.notes }}">
                                {{ entry.notes[:30] }}{% if entry.notes|length > 30 %}...{% endif %}
                            </span>
                        {% else %}
                            <span class="text-muted">No notes</span>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </div>
    </div>

    <!-- Pagination -->
    {% if entries.pages > 1 %}
    <div class="pagination-section">
        <div class="pagination">
            {% if entries.has_prev %}
                <a href="{{ url_for('system_admin.system_admin_time_entries', page=entries.prev_num, company=current_company) }}" class="page-link"><i class="ti ti-arrow-left"></i> Previous</a>
            {% endif %}
            
            {% for page_num in entries.iter_pages() %}
                {% if page_num %}
                    {% if page_num != entries.page %}
                        <a href="{{ url_for('system_admin.system_admin_time_entries', page=page_num, company=current_company) }}" class="page-link">{{ page_num }}</a>
                    {% else %}
                        <span class="page-link current">{{ page_num }}</span>
                    {% endif %}
                {% else %}
                    <span class="page-link">…</span>
                {% endif %}
            {% endfor %}
            
            {% if entries.has_next %}
                <a href="{{ url_for('system_admin.system_admin_time_entries', page=entries.next_num, company=current_company) }}" class="page-link">Next <i class="ti ti-arrow-right"></i></a>
            {% endif %}
        </div>
        
        <p class="pagination-info">
            Showing {{ entries.per_page * (entries.page - 1) + 1 }} - 
            {{ entries.per_page * (entries.page - 1) + entries.items|length }} of {{ entries.total }} time entries
        </p>
    </div>
    {% endif %}

    {% else %}
    <div class="empty-state">
        <h3>No time entries found</h3>
        {% if current_company %}
            <p>No time entries found for the selected company.</p>
        {% else %}
            <p>No time entries exist in the system yet.</p>
        {% endif %}
    </div>
    {% endif %}

    <!-- Summary Statistics -->
    {% if entries.items %}
    <div class="stats-section">
        <div class="stat-card">
            <div class="stat-value">{{ entries.total }}</div>
            <div class="stat-label">Total Entries</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ entries.items | selectattr('0.departure_time', 'equalto', None) | list | length }}</div>
            <div class="stat-label">Active Sessions</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">{{ entries.items | selectattr('0.is_paused', 'equalto', True) | list | length }}</div>
            <div class="stat-label">Paused Sessions</div>
        </div>
        <div class="stat-card">
            <div class="stat-value">
                {{ entries.items | selectattr('0.arrival_time') | selectattr('0.departure_time', 'defined') | 
                   list | length }}
            </div>
            <div class="stat-label">Completed Today</div>
        </div>
    </div>
    {% endif %}
</div>

<style>
/* Container */
.time-entries-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 2rem;
}

/* Page Header */
.page-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 16px;
    padding: 2rem;
    margin-bottom: 2rem;
    color: white;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2rem;
}

.page-title {
    font-size: 2rem;
    font-weight: 700;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-icon {
    font-size: 2.5rem;
    display: inline-block;
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

.page-subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    margin: 0.5rem 0 0 0;
}

/* Cards */
.card {
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    border: 1px solid #e5e7eb;
    margin-bottom: 1.5rem;
    overflow: hidden;
    transition: all 0.3s ease;
}

.card:hover {
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    transform: translateY(-2px);
}

.card-header {
    background: #f8f9fa;
    padding: 1.5rem;
    border-bottom: 1px solid #e5e7eb;
}

.card-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin: 0;
    color: #1f2937;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card-title .icon {
    font-size: 1.5rem;
}

.card-body {
    padding: 1.5rem;
}

.card-body.no-padding {
    padding: 0;
}

/* Filter Form */
.filter-form {
    display: flex;
    align-items: end;
    gap: 1rem;
    flex-wrap: wrap;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.filter-group label {
    font-weight: 600;
    color: #374151;
    font-size: 0.875rem;
}

.form-control {
    padding: 0.625rem 1rem;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    font-size: 1rem;
    min-width: 250px;
    transition: all 0.2s ease;
}

.form-control:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
}

.table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}

.table th,
.table td {
    padding: 0.75rem;
    text-align: left;
    border-bottom: 1px solid #dee2e6;
    font-size: 0.875rem;
}

.table th {
    background: #f8f9fa;
    font-weight: 600;
    color: #374151;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-size: 0.8rem;
}

.paused-entry {
    background-color: #fff3cd !important;
}

.company-name {
    font-weight: 600;
    color: #1f2937;
}

.project-name {
    color: #667eea;
    font-weight: 600;
}

.duration {
    font-weight: 700;
    color: #10b981;
}

.notes {
    color: #6c757d;
    font-style: italic;
}

.text-muted {
    color: #6c757d;
}

.status-badge {
    padding: 0.25rem 0.75rem;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
}

.status-active {
    background: #d1fae5;
    color: #065f46;
}

.status-paused {
    background: #fef3c7;
    color: #92400e;
}

.status-completed {
    background: #dbeafe;
    color: #1e40af;
}

.pagination-section {
    margin: 2rem 0;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 1rem;
}

.pagination {
    display: flex;
    gap: 0.25rem;
}

.page-link {
    padding: 0.5rem 1rem;
    border: 1px solid #e5e7eb;
    color: #667eea;
    text-decoration: none;
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
}

.page-link:hover {
    background: #f3f4f6;
    border-color: #667eea;
}

.page-link.current {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border-color: transparent;
}

.pagination-info {
    color: #6c757d;
    margin: 0;
    font-size: 0.9rem;
}

.empty-state {
    text-align: center;
    padding: 3rem;
    color: #6c757d;
}

/* Stats Section */
.stats-section {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 1.5rem;
    margin-top: 2rem;
}

.stat-card {
    background: white;
    padding: 1.5rem;
    border-radius: 12px;
    text-align: center;
    border: 1px solid #e5e7eb;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.3s ease;
}

.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 0.5rem;
    color: #667eea;
}

.stat-label {
    font-size: 0.9rem;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

/* Buttons */
.btn {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
}

.btn-secondary {
    background: white;
    color: #667eea;
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.5);
}

.btn-sm {
    font-size: 0.875rem;
    padding: 0.5rem 1rem;
}

.btn-outline {
    background: transparent;
    color: #667eea;
    border: 2px solid #667eea;
}

.btn-outline:hover {
    background: #667eea;
    color: white;
}

/* Responsive Design */
@media (max-width: 768px) {
    .time-entries-container {
        padding: 1rem;
    }
    
    .page-header {
        padding: 1.5rem;
    }
    
    .header-content {
        flex-direction: column;
        text-align: center;
    }
    
    .table {
        font-size: 0.8rem;
    }
    
    .table th,
    .table td {
        padding: 0.5rem;
    }
}

/* Animations */
@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.card {
    animation: slideIn 0.3s ease-out;
    animation-fill-mode: both;
}

.card:nth-child(1) { animation-delay: 0.1s; }
.card:nth-child(2) { animation-delay: 0.2s; }
.card:nth-child(3) { animation-delay: 0.3s; }
</style>
{% endblock %}