<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FRED Economic Data Dashboard</title>
    <link rel="stylesheet" href="/static/css/style.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <nav class="navbar navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="/">
                <i class="bi bi-graph-up-arrow me-2"></i>
                FRED Economic Data Dashboard
            </a>
            <span class="navbar-text text-light" id="current-time"></span>
        </div>
    </nav>

    <div class="container-fluid mt-4">
        <div class="row">
            <div class="col-md-3">
                <div class="card mb-4">
                    <div class="card-header bg-primary text-white">
                        <h5 class="mb-0"><i class="bi bi-search me-2"></i>Search</h5>
                    </div>
                    <div class="card-body">
                        <div class="input-group mb-3">
                            <input type="text" class="form-control" id="search-input" placeholder="Search series (e.g., GDP)">
                            <button class="btn btn-primary" onclick="searchSeries()">
                                <i class="bi bi-search"></i>
                            </button>
                        </div>
                        <div class="d-grid gap-2">
                            <button class="btn btn-outline-secondary" onclick="loadPopularSeries()">
                                <i class="bi bi-star me-2"></i>Popular Series
                            </button>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header bg-success text-white">
                        <h5 class="mb-0"><i class="bi bi-collection me-2"></i>Categories</h5>
                    </div>
                    <div class="card-body" id="categories-list" style="max-height: 300px; overflow-y: auto;">
                        <div class="text-center text-muted">
                            <div class="spinner-border spinner-border-sm" role="status"></div>
                            <span class="ms-2">Loading...</span>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header bg-info text-white">
                        <h5 class="mb-0"><i class="bi bi-newspaper me-2"></i>Recent Releases</h5>
                    </div>
                    <div class="card-body" id="releases-list" style="max-height: 300px; overflow-y: auto;">
                        <div class="text-center text-muted">
                            <div class="spinner-border spinner-border-sm" role="status"></div>
                            <span class="ms-2">Loading...</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-md-9">
                <div class="card mb-4">
                    <div class="card-header bg-dark text-white d-flex justify-content-between align-items-center">
                        <h5 class="mb-0" id="chart-title">Select a series to view data</h5>
                        <div>
                            <button class="btn btn-sm btn-outline-light" onclick="exportData()" title="Export Data">
                                <i class="bi bi-download"></i>
                            </button>
                        </div>
                    </div>
                    <div class="card-body">
                        <div id="series-selector" class="mb-3">
                            <div class="row">
                                <div class="col-md-8">
                                    <select class="form-select" id="series-select" onchange="loadSeriesData()">
                                        <option value="">-- Select a Series --</option>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <button class="btn btn-primary w-100" onclick="showSeriesModal()">
                                        <i class="bi bi-plus-circle me-2"></i>Add Series
                                    </button>
                                </div>
                            </div>
                        </div>
                        <div style="height: 400px;">
                            <canvas id="dataChart"></canvas>
                        </div>
                        <div id="chart-placeholder" class="text-center text-muted py-5">
                            <i class="bi bi-graph-up" style="font-size: 4rem;"></i>
                            <p class="mt-3">Select or search for a series to visualize data</p>
                        </div>
                    </div>
                </div>

                <div class="card mb-4">
                    <div class="card-header bg-secondary text-white">
                        <h5 class="mb-0"><i class="bi bi-table me-2"></i>Data Table</h5>
                    </div>
                    <div class="card-body">
                        <div class="table-responsive" style="max-height: 400px; overflow-y: auto;">
                            <table class="table table-striped table-hover" id="data-table">
                                <thead class="table-dark">
                                    <tr>
                                        <th>Date</th>
                                        <th>Value</th>
                                    </tr>
                                </thead>
                                <tbody id="data-table-body">
                                    <tr>
                                        <td colspan="2" class="text-center text-muted">No data available</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                        <div class="mt-3">
                            <nav>
                                <ul class="pagination justify-content-center" id="pagination"></ul>
                            </nav>
                        </div>
                    </div>
                </div>

                <div class="card">
                    <div class="card-header bg-warning text-dark">
                        <h5 class="mb-0"><i class="bi bi-info-circle me-2"></i>Series Information</h5>
                    </div>
                    <div class="card-body" id="series-info">
                        <p class="text-muted">Select a series to view details</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="addSeriesModal" tabindex="-1">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Add Series</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Series ID</label>
                        <input type="text" class="form-control" id="modal-series-id" placeholder="e.g., GNPCA, GDP">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Observation Limit</label>
                        <select class="form-select" id="modal-limit">
                            <option value="10">Last 10</option>
                            <option value="50" selected>Last 50</option>
                            <option value="100">Last 100</option>
                            <option value="1000">All</option>
                        </select>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-primary" onclick="addSeriesFromModal()">Load</button>
                </div>
            </div>
        </div>
    </div>

    <footer class="bg-dark text-white text-center py-3 mt-4">
        <p class="mb-0">FRED Economic Data Dashboard | Data provided by Federal Reserve Bank of St. Louis</p>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <script src="/static/js/app.js"></script>
</body>
</html>