<!DOCTYPE html>
<html data-theme="dark">
<head>
    <title>User Configuration</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="/static/pub/bulma.min.css">
    <link rel="stylesheet" href="/static/pub/all.min.css">
    <style>
        body {
            min-height: 100vh;
            background-color: #000;
            color: #f5f5f5;
        }
        .banner-container {
            background-image: url("/static/pub/long-banner.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center;
            position: relative;
            flex: 1 1 auto;
            width: 100%;
            min-height: 50px;
        }
        img.banner {
            height: 50px;
        }
        .page-shell {
            padding: 5rem 1.25rem 3rem;
        }
        .page-card {
            background: #111;
            border-radius: 18px;
            border: 1px solid #2f2f2f;
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
        }
        .title,
        .subtitle,
        .label,
        .table,
        .table th,
        .table td,
        strong,
        p {
            color: #f5f5f5;
        }
        .table {
            background: transparent;
            font-size: 0.9rem;
        }
        .table th,
        .table td {
            border-color: #2f2f2f;
            vertical-align: top;
            padding: 0.5rem 0.6rem;
        }
        .input,
        .select select {
            background: #1b1b1b;
            border-color: #3d3d3d;
            color: #f5f5f5;
        }
        .input::placeholder {
            color: #9a9a9a;
        }
        .user-actions .button,
        .user-actions .input,
        .user-actions .select select {
            font-size: 0.8rem;
            height: 2rem;
        }
        .user-actions .field:not(:last-child) {
            margin-bottom: 0.4rem;
        }
    </style>
</head>
<body>
    <nav class="navbar is-fixed-top" role="navigation" aria-label="main navigation">
        <div class="navbar-brand">
            <img class="banner" src="/static/pub/logo.jpg"/>
        </div>
        <div class="banner-container"></div>
    </nav>
    <section class="page-shell">
        <div class="container is-max-desktop">
            <div class="level mb-5">
                <div class="level-left">
                    <div>
                        <h1 class="title is-3">User Configuration</h1>
                        <p class="subtitle is-6">Signed in as {{.Username}} ({{.Role}})</p>
                    </div>
                </div>
                <div class="level-right">
                    <div class="buttons">
                        <a class="button is-light" href="/">Back to Proxies</a>
                        <form method="post" action="/logout" autocomplete="off">
                            <button class="button is-dark" type="submit">Logout</button>
                        </form>
                    </div>
                </div>
            </div>

            {{if .Error}}
            <article class="message is-danger">
                <div class="message-body">{{.Error}}</div>
            </article>
            {{end}}

            <div class="columns is-variable is-5">
                <div class="column is-5">
                    {{if ne .Role "admin"}}
                    <div class="box page-card">
                        <h2 class="title is-5">Change Your Password</h2>
                        <form method="post" action="/users/password" autocomplete="off">
                            <input type="hidden" name="username" value="{{.Username}}">
                            <div class="field">
                                <label class="label">New Password</label>
                                <div class="control">
                                    <input class="input" type="password" name="password" required>
                                </div>
                            </div>
                            <div class="field">
                                <label class="label">Confirm Password</label>
                                <div class="control">
                                    <input class="input" type="password" name="confirm_password" required>
                                </div>
                            </div>
                            <button class="button is-link is-fullwidth" type="submit">Update Password</button>
                        </form>
                    </div>
                    {{end}}

                    {{if eq .Role "admin"}}
                    <div class="box page-card">
                        <h2 class="title is-5">Add User</h2>
                        <form method="post" action="/users/add" autocomplete="off">
                            <div class="field">
                                <label class="label">Username</label>
                                <div class="control">
                                    <input class="input" type="text" name="username" required>
                                </div>
                            </div>
                            <div class="field">
                                <label class="label">Password</label>
                                <div class="control">
                                    <input class="input" type="password" name="password" required>
                                </div>
                            </div>
                            <div class="field">
                                <label class="label">Role</label>
                                <div class="control">
                                    <div class="select is-fullwidth">
                                        <select name="role">
                                            <option value="regular">regular</option>
                                            <option value="admin">admin</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <button class="button is-link is-fullwidth" type="submit">Create User</button>
                        </form>
                    </div>
                    {{end}}
                </div>
                <div class="column">
                    {{if eq .Role "admin"}}
                    <div class="box page-card">
                        <h2 class="title is-5">Existing Users</h2>
                        <table class="table is-fullwidth is-striped is-hoverable is-narrow">
                            <thead>
                                <tr>
                                    <th>Username</th>
                                    <th>Role</th>
                                    <th>Created</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                {{range .Users}}
                                <tr>
                                    <td>{{.Username}}</td>
                                    <td>{{.Role}}</td>
                                    <td>{{.CreatedAt.Format "2006-01-02 15:04:05 UTC"}}</td>
                                    <td class="user-actions">
                                        <form method="post" action="/users/role" autocomplete="off" class="mb-2">
                                            <input type="hidden" name="username" value="{{.Username}}">
                                            <div class="field has-addons">
                                                <div class="control is-expanded">
                                                    <div class="select is-small is-fullwidth">
                                                        <select name="role">
                                                            <option value="regular" {{if eq .Role "regular"}}selected{{end}}>regular</option>
                                                            <option value="admin" {{if eq .Role "admin"}}selected{{end}}>admin</option>
                                                        </select>
                                                    </div>
                                                </div>
                                                <div class="control">
                                                    <button class="button is-small is-info" type="submit">Update</button>
                                                </div>
                                            </div>
                                        </form>
                                        <form method="post" action="/users/password" autocomplete="off" class="mb-2">
                                            <input type="hidden" name="username" value="{{.Username}}">
                                            <div class="field has-addons">
                                                <div class="control is-expanded">
                                                    <input class="input is-small" type="password" name="password" placeholder="New password" required>
                                                </div>
                                                <div class="control">
                                                    <button class="button is-small is-warning" type="submit">Set Password</button>
                                                </div>
                                            </div>
                                        </form>
                                        <form method="post" action="/users/delete" autocomplete="off" onsubmit="return confirm('Delete user {{.Username}}?');">
                                            <input type="hidden" name="username" value="{{.Username}}">
                                            <button class="button is-small is-danger is-light" type="submit">Delete User</button>
                                        </form>
                                    </td>
                                </tr>
                                {{end}}
                            </tbody>
                        </table>
                    </div>
                    {{else}}
                    <div class="box page-card">
                        <h2 class="title is-5">Account</h2>
                        <p class="mb-2"><strong>Username:</strong> {{.Username}}</p>
                        <p><strong>Role:</strong> {{.Role}}</p>
                    </div>
                    {{end}}
                </div>
            </div>
        </div>
    </section>
</body>
</html>
