{% extends "base.html" %}

{% block title %}Dashboard · Delphi Database{% endblock %}

{% block content %}
<div class="row g-3 align-items-center mb-3">
    <div class="col-auto">
        <h2 class="mb-0">Cases</h2>
    </div>
    <div class="col ms-auto">
        <form class="d-flex" method="get" action="/dashboard">
            <input
                class="form-control me-2"
                type="search"
                name="q"
                placeholder="Search file # or name/company"
                aria-label="Search"
                value="{{ q or '' }}"
            >
            <input type="hidden" name="page_size" value="{{ page_size }}">
            <button class="btn btn-outline-primary" type="submit">
                <i class="bi bi-search me-1"></i>Search
            </button>
        </form>
    </div>
    <div class="col-auto">
        <a class="btn btn-outline-secondary" href="/dashboard">
            <i class="bi bi-x-circle me-1"></i>Clear
        </a>
    </div>
    <div class="col-12 text-muted small">
        {% if total and total > 0 %}
            Showing {{ start_index }}–{{ end_index }} of {{ total }}
        {% else %}
            No results
        {% endif %}
    </div>
    <div class="col-12">
        <div class="table-responsive">
            <table class="table table-hover align-middle">
                <thead class="table-light">
                    <tr>
                        <th scope="col" style="width: 140px;">File #</th>
                        <th scope="col">Client</th>
                        <th scope="col">Company</th>
                        <th scope="col">Type</th>
                        <th scope="col">Status</th>
                        <th scope="col">Opened</th>
                        <th scope="col" class="text-end">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    {% if cases and cases|length > 0 %}
                        {% for c in cases %}
                        <tr>
                            <td><span class="fw-semibold">{{ c.file_no }}</span></td>
                            <td>
                                {% set client = c.client %}
                                {% if client %}
                                    {{ client.last_name }}, {{ client.first_name }}
                                {% else %}
                                    <span class="text-muted">Unknown</span>
                                {% endif %}
                            </td>
                            <td>{{ client.company if client else '' }}</td>
                            <td>{{ c.case_type or '' }}</td>
                            <td>
                                {% if c.status == 'active' %}
                                    <span class="badge bg-success">Active</span>
                                {% elif c.status == 'closed' %}
                                    <span class="badge bg-secondary">Closed</span>
                                {% else %}
                                    <span class="badge bg-light text-dark">{{ c.status or 'n/a' }}</span>
                                {% endif %}
                            </td>
                            <td>{{ c.open_date.strftime('%Y-%m-%d') if c.open_date else '' }}</td>
                            <td class="text-end">
                                <a class="btn btn-sm btn-outline-primary" href="/case/{{ c.id }}">
                                    <i class="bi bi-folder2-open me-1"></i>View
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    {% else %}
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">No cases found.</td>
                        </tr>
                    {% endif %}
                </tbody>
            </table>
        </div>
    </div>
    <div class="col-12">
        {% if total_pages and total_pages > 1 %}
        <nav aria-label="Cases pagination">
            <ul class="pagination mb-0">
                {# Previous #}
                <li class="page-item {% if page <= 1 %}disabled{% endif %}">
                    <a class="page-link" href="/dashboard?page={{ page - 1 if page > 1 else 1 }}&page_size={{ page_size }}{% if q %}&q={{ q | urlencode }}{% endif %}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                    </a>
                </li>

                {# Page numbers window #}
                {% for p in page_numbers %}
                    <li class="page-item {% if p == page %}active{% endif %}">
                        <a class="page-link" href="/dashboard?page={{ p }}&page_size={{ page_size }}{% if q %}&q={{ q | urlencode }}{% endif %}">{{ p }}</a>
                    </li>
                {% endfor %}

                {# Next #}
                <li class="page-item {% if page >= total_pages %}disabled{% endif %}">
                    <a class="page-link" href="/dashboard?page={{ page + 1 if page < total_pages else total_pages }}&page_size={{ page_size }}{% if q %}&q={{ q | urlencode }}{% endif %}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </ul>
        </nav>
        {% endif %}
    </div>
</div>
{% endblock %}
