{% 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="card sp-animate-card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <div>
                <h6 class="card-title mb-0">Branches</h6>
                <small class="text-muted">Manage your shops / outlets</small>
            </div>

            {% if allow_multi_branch %}
                <a href="{% url 'branch_create' %}" class="btn btn-sm btn-primary">
                    <i class="fas fa-plus me-1"></i> Add Branch
                </a>
            {% else %}
                <span class="small text-muted">
                    Multi-branch is disabled on your plan.
                </span>
            {% endif %}
        </div>

        <div class="card-body">
            <div class="sp-table-container">
                <table>
                    <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 %}
