{% extends 'core/base.html' %}
{% load static %}
{% load plan_tags %}

{% block title %}Branches - SwiftPOS{% endblock %}
{% block topbar_title %}Branches{% endblock %}

{% block content %}
<div class="container-fluid px-0">
    <div class="sp-page-header">
        <div>
            <h1 class="sp-page-title"><i class="fas fa-code-branch"></i>Branches</h1>
            <p class="sp-page-subtitle">Manage your shops / outlets.</p>
        </div>
        <div class="sp-page-actions">
        {% if allow_multi_branch %}
            <a href="{% url 'branch_create' %}" class="btn btn-sm btn-primary d-flex align-items-center gap-2">
                <i class="fas fa-plus"></i> Add Branch
            </a>
        {% else %}
            <span class="small text-muted">Multi-branch is disabled on your plan.</span>
        {% endif %}
        </div>
    </div>

    <div class="card sp-animate-card">
        <div class="card-body p-0">
            <div class="sp-table-container">
                <table class="table table-hover align-middle mb-0">
                    <thead>
                        <tr>
                            <th>Name</th>
                            <th>Code</th>
                            <th>Phone</th>
                            <th>Address</th>
                            <th>Status</th>
                            <th class="text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for branch in branches %}
                            <tr>
                                <td> <a href="{% url 'branch_detail' branch.pk %}">
                                    {{ branch.name }}
                                </a></td>
                                <td>{{ branch.code }}</td>
                                <td>{{ branch.phone|default:"-" }}</td>
                                <td>{{ branch.address|default:"-" }}</td>
                                <td>
                                    {% if branch.is_active %}
                                        <span class="badge bg-success-subtle">Active</span>
                                    {% else %}
                                        <span class="badge bg-danger-subtle">Inactive</span>
                                    {% endif %}
                                </td>
                                <td class="text-end">
                                    <a href="{% url 'branch_update' branch.pk %}"
                                       class="btn btn-sm btn-outline-secondary">
                                        Edit
                                    </a>
                                    {% if branches|length > 1 %}
                                    <form method="post"
                                          action="{% url 'branch_delete' branch.pk %}"
                                          class="d-inline">
                                        {% csrf_token %}
                                        <button type="submit"
                                                class="btn btn-sm btn-outline-danger"
                                                onclick="return confirm('Delete this branch?');">
                                            Delete
                                        </button>
                                    </form>
                                    {% endif %}
                                </td>
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="6" class="text-center text-muted py-3">
                                    No branches found.
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
