{% extends 'base.html' %}
{% load static %}

{% block title %}Branch Management{% endblock %}

{% block content %}
<div class="container-fluid px-4 py-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2 class="text-2xl font-bold">Branch Management</h2>
        <a href="{% url 'users:branch_create' %}" class="btn btn-primary">
            <span>+</span> Add New Branch
        </a>
    </div>
    
    {% if messages %}
    <div class="mb-4">
        {% 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 %}
    </div>
    {% endif %}
    
    <div class="card shadow">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Code</th>
                            <th>Address</th>
                            <th>Phone</th>
                            <th>Email</th>
                            <th>Main Branch</th>
                            <th>Status</th>
                            <th>Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for branch in branches %}
                        <tr>
                            <td>{{ branch.name }}</td>
                            <td>{{ branch.code }}</td>
                            <td>{{ branch.address|default:"-" }}</td>
                            <td>{{ branch.phone_number|default:"-" }}</td>
                            <td>{{ branch.email|default:"-" }}</td>
                            <td>
                                {% if branch.is_main_branch %}
                                <span class="badge bg-success">Yes</span>
                                {% else %}
                                <span class="badge bg-secondary">No</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if branch.is_active %}
                                <span class="badge bg-success">Active</span>
                                {% else %}
                                <span class="badge bg-danger">Inactive</span>
                                {% endif %}
                            </td>
                            <td>
                                <div class="d-flex gap-2">
                                    <a href="{% url 'users:branch_update' pk=branch.pk %}" class="btn btn-sm btn-outline-primary">
                                        <span>✏️</span>
                                    </a>
                                    <a href="{% url 'users:switch_branch' branch_id=branch.pk %}" class="btn btn-sm btn-outline-secondary">
                                        <i class="fas fa-exchange-alt"></i> Switch
                                    </a>
                                </div>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-4">
                                <p class="text-muted">No branches found</p>
                                <a href="{% url 'users:branch_create' %}" class="btn btn-sm btn-primary">
                                    <span>+</span> Add New Branch
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}