<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Employee Activity Dashboard</title>
    <!-- Favicon -->
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}" type="image/x-icon">
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap Icons -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <!-- Custom CSS -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/dashboard.css') }}">
</head>
<body>
    <div class="container">
        <header class="mb-4">
            <h1 class="text-center my-4">Employee Working Time Report</h1>
            <p class="text-center text-muted">Aggregated working hours by user</p>
        </header>

        <div class="row">
            <!-- Time Filter & User Filter -->
            <div class="col-12 mb-3">
                <div class="card">
                    <div class="card-header">Filters</div>
                    <div class="card-body d-flex justify-content-between align-items-center">
                        <div>
                            <span class="me-2 fw-bold">Time Period:</span>
                            <div class="btn-group">
                                <button class="btn btn-outline-primary btn-sm btn-filter active" data-period="daily">Today</button>
                                <button class="btn btn-outline-primary btn-sm btn-filter" data-period="weekly">This Week</button>
                                <button class="btn btn-outline-primary btn-sm btn-filter" data-period="monthly">This Month</button>
                            </div>
                            
                            <!-- Date navigation controls (initially hidden) -->
                            <div id="dateNavControls" class="mt-2 d-none">
                                <div class="d-flex align-items-center">
                                    <button id="prevDateBtn" class="btn btn-sm btn-outline-secondary me-2">
                                        <i class="bi bi-chevron-left"></i>
                                    </button>
                                    <span id="currentDateDisplay" class="me-2"></span>
                                    <button id="nextDateBtn" class="btn btn-sm btn-outline-secondary me-2">
                                        <i class="bi bi-chevron-right"></i>
                                    </button>
                                    <button id="calendarBtn" class="btn btn-sm btn-outline-secondary" title="Select specific date">
                                        <i class="bi bi-calendar3"></i>
                                    </button>
                                    <input type="date" id="dateSelector" class="form-control form-control-sm ms-2" style="display: none;">
                                </div>
                            </div>
                            
                            <!-- Day selector for weekly view (initially hidden) -->
                            <div id="weekDaySelector" class="mt-2 d-none">
                                <span class="me-2">Select day:</span>
                                <select id="weekDaySelect" class="form-select form-select-sm" style="display: inline-block;">
                                    <!-- Options will be populated by JavaScript -->
                                </select>
                            </div>
                        </div>
                        <div class="d-flex align-items-center">
                            <label for="userFilter" class="form-label me-2 mb-0 fw-bold">User:</label>
                            <div id="userFilterInputGroup" class="input-group">
                                <input type="text" id="userFilter" class="form-control form-control-sm" placeholder="Filter by username...">
                                <button class="btn btn-outline-secondary btn-sm" type="button" id="clearUserFilter">
                                    <i class="bi bi-x"></i> Clear
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- State Filter -->
        <div class="row">
            <div class="col-12 mb-3">
                <div class="card">
                    <div class="card-header">State Filter</div>
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div class="btn-group" role="group" aria-label="State filter">
                                <button type="button" class="btn btn-outline-secondary active" id="stateFilterAll">All</button>
                                <button type="button" class="btn btn-outline-success" id="stateFilterWorking">Working</button>
                                <button type="button" class="btn btn-outline-secondary" id="stateFilterNotWorking">Not Working</button>
                            </div>
                            <button type="button" class="btn btn-outline-primary" id="refreshStates">
                                <i class="bi bi-arrow-clockwise"></i> Refresh States
                            </button>
                            <!-- Add UI elements for auto-refresh near the Refresh States button -->
                            <button id="autoRefreshBtn" class="btn btn-sm btn-outline-info ms-2">
                                <i class="bi bi-pause-circle"></i> Pause Auto-refresh
                            </button>
                            <span id="autoRefreshIndicator" class="badge bg-light text-dark ms-2">Auto-refresh in 30s</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <!-- Working Time Report Table -->
            <div class="col-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <span>Working Time Report</span>
                        <div id="loadingSpinner" class="spinner-border text-primary d-none" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-hover" id="reportTable">
                                <thead>
                                    <tr>
                                        <th>User</th>
                                        <th id="periodHeader">Day</th> <!-- Will be updated by JS -->
                                        <th class="sortable" data-sort="duration">Real Work Hours <i class="bi bi-sort-down sort-icon"></i></th>
                                        <th class="sortable" data-sort="state">State <i class="bi bi-sort-down sort-icon"></i></th>
                                    </tr>
                                </thead>
                                <tbody id="reportBody">
                                    <!-- Report data will be populated via JavaScript -->
                                    <tr>
                                        <td colspan="4" class="text-center text-muted">Select filters and load data.</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div id="errorMessage" class="alert alert-danger d-none mt-3" role="alert">
                            Error loading report data. Please try again.
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- User Activity Details Modal -->
    <div class="modal fade" id="userActivityModal" tabindex="-1" aria-labelledby="userActivityModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="userActivityModalLabel">User Activity Details</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <div class="d-flex justify-content-between mb-3">
                        <div>
                            <strong>User:</strong> <span id="modalUsername"></span>
                        </div>
                        <div>
                            <div class="input-group input-group-sm">
                                <span class="input-group-text">Date Range</span>
                                <input type="date" id="startDate" class="form-control">
                                <span class="input-group-text">to</span>
                                <input type="date" id="endDate" class="form-control">
                                <button class="btn btn-outline-primary" id="applyDateRange">Apply</button>
                            </div>
                        </div>
                    </div>
                    <div id="modalLoadingSpinner" class="d-flex justify-content-center py-3 d-none">
                        <div class="spinner-border text-primary" role="status">
                            <span class="visually-hidden">Loading...</span>
                        </div>
                    </div>
                    <div class="table-responsive">
                        <table class="table table-striped" id="userActivityTable">
                            <thead>
                                <tr>
                                    <th>Date</th>
                                    <th>Start Time</th>
                                    <th>End Time</th>
                                    <th>Duration (Hours)</th>
                                </tr>
                            </thead>
                            <tbody id="userActivityBody">
                                <!-- Activity data will be populated here -->
                            </tbody>
                        </table>
                    </div>
                    <div id="modalErrorMessage" class="alert alert-danger d-none mt-3" role="alert">
                        Error loading activity data.
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    <!-- Toast Notification for Logs -->
    <div class="position-fixed bottom-0 end-0 p-3 toast-container-custom">
        <div id="logToast" class="toast" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="toast-header">
                <strong class="me-auto">System Log</strong>
                <small class="text-muted" id="logTime"></small>
                <button type="button" class="btn-close" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
            <div class="toast-body" id="logMessage">
                States refreshed successfully.
            </div>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Custom JS Modules -->
    <script src="{{ url_for('static', filename='js/dashboard.js') }}" type="module"></script>
</body>
</html> 