{% extends "base.html" %}

{% block title %}{{ title }}{% endblock %}

{% block content %}
<div class="container mt-4">
    <h1 class="mb-4">{{ title }}</h1>

    <ul class="nav nav-tabs">
        <li class="nav-item">
            <a class="nav-link {% if active_tab == 'open' %}active{% endif %}" href="{{ url_for('admin.manage_open_requests') }}">Open Requests</a>
        </li>
        <li class="nav-item">
            <a class="nav-link {% if active_tab == 'archived' %}active{% endif %}" href="{{ url_for('admin.manage_archived_requests') }}">Archived Requests</a>
        </li>
    </ul>

    <div class="tab-content mt-3">
        <div class="tab-pane fade show active">
            {% for group_id, group in requests.items() %}
            <div class="card mb-4">
                <div class="card-header">
                    <strong>Consultant:</strong> {{ group.consultant.username }} |
                    <strong>Request Date:</strong> {{ group.request_date.strftime('%Y-%m-%d %H:%M') }} |
                    <strong>Total Cost:</strong> ₹{{ "%.2f"|format(group.total_cost) }}
                </div>
                <div class="card-body">
                    <table class="table table-sm table-bordered">
                        <thead class="thead-light">
                            <tr>
                                <th>Assessment</th>
                                <th>Quantity</th>
                                <th>Cost</th>
                                {% if active_tab == 'archived' %}
                                <th>Status</th>
                                <th>Admin Notes</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbody>
                            {% for req in group.requests %}
                            <tr>
                                <td>{{ req.assessment.name }}</td>
                                <td>{{ req.requested_quantity }}</td>
                                <td>₹{{ "%.2f"|format(req.total_cost) }}</td>
                                {% if active_tab == 'archived' %}
                                <td>{{ req.status.capitalize() }}</td>
                                <td>{{ req.admin_notes or 'N/A' }}</td>
                                {% endif %}
                            </tr>
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                <div class="card-footer">
                    <a href="{{ url_for('static', filename='payment_slips/' + group.payment_slip_url) }}" class="btn btn-info btn-sm" target="_blank">View Slip</a>
                    {% if active_tab == 'open' %}
                    <div class="float-right">
                        <form method="POST" action="{{ url_for('admin.approve_request_group') }}" class="d-inline">
                            <input type="hidden" name="group_id" value="{{ group_id }}">
                            <input type="text" name="admin_notes" placeholder="Approval Notes" class="form-control form-control-sm d-inline-block" style="width: auto;">
                            <button type="submit" class="btn btn-success btn-sm">Approve All</button>
                        </form>
                        <form method="POST" action="{{ url_for('admin.reject_request_group') }}" class="d-inline ml-2">
                            <input type="hidden" name="group_id" value="{{ group_id }}">
                            <input type="text" name="admin_notes" placeholder="Rejection Reason" class="form-control form-control-sm d-inline-block" style="width: auto;" required>
                            <button type="submit" class="btn btn-danger btn-sm">Reject All</button>
                        </form>
                    </div>
                    {% endif %}
                </div>
            </div>
            {% else %}
            <p class="text-center">No assessment requests found.</p>
            {% endfor %}
        </div>
    </div>
</div>
{% endblock %}
