{% extends 'base.html' %}

{% block title %}Departments - Task Manager{% endblock %}

{% block content %}
<div class="container mt-5 pt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3 mb-0">Departments</h1>
        {% if perms.users.add_department %}
        <a href="{% url 'departments:department_create' %}" class="btn btn-primary">
            <i class="fas fa-plus me-1"></i> Add Department
        </a>
        {% endif %}
    </div>

    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                {{ message }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endfor %}
    {% endif %}

    <div class="card shadow-sm">
        <div class="card-body">
            {% if departments %}
                <div class="table-responsive">
                    <table class="table table-hover align-middle">
                        <thead class="table-light">
                            <tr>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Parent Department</th>
                                <th class="text-end">Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            {% for department in departments %}
                                <tr>
                                    <td>
                                        <a href="{% url 'departments:department_detail' department.pk %}" class="text-decoration-none fw-medium">
                                            {{ department.name }}
                                        </a>
                                    </td>
                                    <td>{{ department.description|truncatechars:50|default:"-" }}</td>
                                    <td>{{ department.parent.name|default:"-" }}</td>
                                    <td class="text-end">
                                        <div class="btn-group" role="group">
                                            <a href="{% url 'departments:department_detail' department.pk %}" 
                                               class="btn btn-sm btn-outline-primary"
                                               data-bs-toggle="tooltip" title="View">
                                                <i class="fas fa-eye"></i>
                                            </a>
                                            {% if perms.users.change_department %}
                                            <a href="{% url 'departments:department_update' department.pk %}" 
                                               class="btn btn-sm btn-outline-secondary"
                                               data-bs-toggle="tooltip" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            {% endif %}
                                            {% if perms.users.delete_department %}
                                            <a href="{% url 'departments:department_delete' department.pk %}" 
                                               class="btn btn-sm btn-outline-danger"
                                               data-bs-toggle="tooltip" title="Delete"
                                               onclick="return confirm('Are you sure you want to delete this department?')">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                            {% endif %}
                                        </div>
                                    </td>
                                </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
            {% else %}
                <div class="text-center py-4">
                    <i class="fas fa-building fa-3x text-muted mb-3"></i>
                    <p class="text-muted">No departments found. Add your first department to get started.</p>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // Initialize tooltips
    document.addEventListener('DOMContentLoaded', function() {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
    });
</script>
{% endblock %}
