{% extends 'base.html' %}

{% block content %}
<div class="container py-5">
    <div class="d-flex justify-content-between align-items-center mb-5">
        <h1 class="display-4 fw-bold" style="color: var(--vibrant-accent);">User Management</h1>
        <a href="{% url 'dashboard' %}" class="btn btn-outline-light">Back to Dashboard</a>
    </div>

    <div class="row">
        <!-- Create User Form -->
        <div class="col-md-4">
            <div class="card bg-dark text-light border-0 shadow-lg mb-4" style="border-radius: 15px;">
                <div class="card-body p-4">
                    <h3 class="mb-4">Create New Account</h3>
                    <form method="post">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label class="form-label">Username</label>
                            <input type="text" name="username" class="form-control bg-secondary text-light border-0" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Password</label>
                            <input type="password" name="password" class="form-control bg-secondary text-light border-0" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label">Role</label>
                            <select name="role" class="form-select bg-secondary text-light border-0" required>
                                <option value="cashier">Cashier</option>
                                <option value="manager">Manager</option>
                            </select>
                        </div>
                        <div class="d-grid mt-4">
                            <button type="submit" class="btn" style="background-color: var(--vibrant-accent); color: white;">Create User</button>
                        </div>
                    </form>
                    {% if messages %}
                        {% for message in messages %}
                            <div class="alert alert-{% if message.tags == 'error' %}danger{% else %}success{% endif %} mt-3 bg-opacity-25 border-0">
                                {{ message }}
                            </div>
                        {% endfor %}
                    {% endif %}
                </div>
            </div>
        </div>

        <!-- User List -->
        <div class="col-md-8">
            <div class="card bg-dark text-light border-0 shadow-lg" style="border-radius: 15px;">
                <div class="card-body p-4">
                    <h3 class="mb-4">Existing Accounts</h3>
                    <div class="table-responsive">
                        <table class="table table-dark table-hover border-0">
                            <thead>
                                <tr class="text-secondary">
                                    <th>Username</th>
                                    <th>Role</th>
                                    <th>Date Joined</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for user in users %}
                                <tr>
                                    <td class="fw-bold">{{ user.username }}</td>
                                    <td>
                                        <span class="badge {% if user.profile.role == 'manager' %}bg-primary{% else %}bg-info{% endif %}">
                                            {{ user.profile.role|title }}
                                        </span>
                                    </td>
                                    <td class="text-secondary">{{ user.date_joined|date:"M d, Y" }}</td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="3" class="text-center text-secondary py-4">No users found.</td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
