<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>KTVManager</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.9/css/responsive.dataTables.min.css">
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}" />    
</head>
<body>
    <!-- Navbar -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="/">KTVManager</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ml-auto">
                <li class="nav-item"><a class="nav-link" href="/">Home</a></li>
                <li class="nav-item"><a class="nav-link" href="/accounts">Accounts</a></li>
                <li class="nav-item"><a class="nav-link" href="/urls">URLs</a></li>
            </ul>
        </div>
    </nav>

    <!-- Sub-navigation for Accounts -->
    <div class="bg-light py-2">
        <div class="container">
            <ul class="nav nav-pills">
                <li class="nav-item">
                    <a class="nav-link active" href="/accounts">List Accounts</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="/accounts/add">Add Account</a>
                </li>
            </ul>
        </div>
    </div>

    <!-- Main Content -->
    <main div class="container mt-5">
        <h2>{{ username }}'s Accounts</h2>
        <div class="table-responsive">
            <table class="table table-striped" id="accountsTable">
                <thead>
                    <tr>
                        <!-- <th>#</th> -->
                        <th>Username</th>
                        <th>Stream</th>
                        <th>Stream URL</th>
                        <th>Expiry Date</th>
                        <th>Password</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% for account in user_accounts %}
                    <tr>
                        <!-- <td>{{ loop.index }}</td> -->
                        <td>{{ account.username }}</td>
                        <td>{{ account.stream }}</td>
                        <td><a href="{{ account.streamURL }}" target="_blank">{{ account.streamURL }}</a></td>
                        <td>{{ account.expiaryDate_rendered }}</td>
                        <td>{{ account.password }}</td>
                        <td>
                            <form action="/accounts/delete" method="POST" style="display:inline;">
                                <input type="hidden" name="stream" value="{{ account.stream }}">
                                <input type="hidden" name="username" value="{{ account.username }}">
                                <button type="submit" class="btn btn-danger" onclick="return confirm('Are you sure you want to delete this account?');">
                                    Delete
                                </button>
                            </form>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
    </main>

    <footer class="bg-dark text-white text-center py-3 mt-5">
        <p></p>
    </footer>

    <script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.0.7/dist/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.9/js/dataTables.responsive.min.js"></script>
    <script>
        $(document).ready(function() {
            // Extend DataTables date sorting for DD/MM/YYYY format
            $.fn.dataTable.ext.type.order['date-eu-pre'] = function(data) {
                const parts = data.split('/');
                // Check if the data is in the correct format before processing
                if (parts.length === 3) {
                    return new Date(parts[2], parts[1] - 1, parts[0]).getTime();
                }
                return data; // return as is if the format is unexpected
            };
    
            // Initialize DataTable with custom sorting and default ordering by Expiry Date
            $('#accountsTable').DataTable({
                "searching": true,
                "ordering": true,
                "responsive": true,
                "order": [[3, 'asc']], // Default order by Expiry Date column in ascending order
                "columnDefs": [
                    { "type": "date-eu", "targets": 3 } // Use custom date-eu type for the date column
                ]
            });
        });
    </script>
    
    
</body>
</html>