{% extends 'base.html' %}

{% block content %}
<div class="py-4 animate-fade-in">
    <div class="d-flex flex-column flex-md-row justify-content-between align-items-md-center mb-5 gap-3">
        <div>
            <h1 class="section-title mb-1">User Management</h1>
            <p class="text-secondary mb-0">Create and manage access for your team members.</p>
        </div>
        <a href="{% url 'dashboard' %}" class="btn btn-outline-dark rounded-pill">
            <i class="bi bi-arrow-left me-2"></i> Back to Dashboard
        </a>
    </div>

    <div class="row g-4">
        <!-- Create User Form -->
        <div class="col-md-4">
            <div class="card h-100 border-0 shadow-sm rounded-4">
                <div class="card-body p-4">
                    <div class="d-flex align-items-center mb-4">
                        <div class="bg-soft text-vibrant p-2 rounded-3 me-3">
                            <i class="bi bi-person-plus-fill fs-4"></i>
                        </div>
                        <h3 class="fw-bold mb-0">Add User</h3>
                    </div>
                    
                    <form method="post">
                        {% csrf_token %}
                        <div class="mb-3">
                            <label class="form-label text-secondary small fw-bold text-uppercase ms-1">Username</label>
                            <input type="text" name="username" class="form-control rounded-3 py-2" placeholder="Enter username" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label text-secondary small fw-bold text-uppercase ms-1">Password</label>
                            <input type="password" name="password" class="form-control rounded-3 py-2" placeholder="Enter password" required>
                        </div>
                        <div class="mb-4">
                            <label class="form-label text-secondary small fw-bold text-uppercase ms-1">Access Role</label>
                            <select name="role" class="form-select rounded-3 py-2" required>
                                <option value="cashier" selected>Cashier (POS Only)</option>
                                <option value="manager">Manager (Full Access)</option>
                            </select>
                        </div>
                        <div class="d-grid mt-4">
                            <button type="submit" class="btn btn-vibrant py-3 rounded-4 shadow-sm">
                                <i class="bi bi-plus-lg me-1"></i> CREATE ACCOUNT
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <!-- User List -->
        <div class="col-md-8">
            <div class="card border-0 shadow-sm rounded-4">
                <div class="card-body p-4">
                    <div class="d-flex justify-content-between align-items-center mb-4">
                        <h4 class="fw-bold mb-0">System Accounts</h4>
                        <span class="badge bg-light text-dark rounded-pill px-3">{{ users|length }} Total Users</span>
                    </div>
                    
                    <div class="table-responsive">
                        <table class="table table-hover align-middle">
                            <thead class="bg-light">
                                <tr class="text-secondary">
                                    <th class="py-3 border-0 rounded-start">User</th>
                                    <th class="py-3 border-0">Role</th>
                                    <th class="py-3 border-0">Joined Date</th>
                                    <th class="py-3 border-0 rounded-end"></th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for u in users %}
                                <tr>
                                    <td class="py-3">
                                        <div class="d-flex align-items-center">
                                            <div class="bg-light rounded-circle p-2 me-3">
                                                <i class="bi bi-person-circle fs-4 text-secondary"></i>
                                            </div>
                                            <span class="fw-bold text-dark">{{ u.username }}</span>
                                        </div>
                                    </td>
                                    <td>
                                        {% if u.profile and u.profile.role == 'manager' %}
                                            <span class="badge bg-primary bg-opacity-10 text-primary border border-primary border-opacity-10 px-3 py-2 rounded-pill">
                                                <i class="bi bi-shield-check me-1"></i> Manager
                                            </span>
                                        {% else %}
                                            <span class="badge bg-info bg-opacity-10 text-info border border-info border-opacity-10 px-3 py-2 rounded-pill">
                                                <i class="bi bi-cart me-1"></i> Cashier
                                            </span>
                                        {% endif %}
                                    </td>
                                    <td class="text-secondary">
                                        <div class="fw-medium">{{ u.date_joined|date:"M d, Y" }}</div>
                                        <small class="opacity-50">at {{ u.date_joined|date:"H:i" }}</small>
                                    </td>
                                    <td class="text-end">
                                        {% if u.id != request.user.id %}
                                        <a href="{% url 'delete_user' u.id %}" class="btn btn-sm btn-outline-danger rounded-circle mx-1" 
                                           onclick="return confirm('Are you sure you want to delete user {{ u.username }}?')" title="Delete User">
                                            <i class="bi bi-trash"></i>
                                        </a>
                                        {% endif %}
                                        <button class="btn btn-sm btn-light rounded-circle" title="Edit User"><i class="bi bi-pencil"></i></button>
                                    </td>
                                </tr>
                                {% empty %}
                                <tr>
                                    <td colspan="4" class="text-center py-5 text-secondary">
                                        No users found.
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<style>
    .rounded-4 { border-radius: 1rem !important; }
</style>
{% endblock %}
