{% extends "base.html" %}
{% block title_suffix %} - Requests{% endblock %}

{% block content %}
<div class="page-header">
    <h1>
        <img src="{{ url_for('static', filename='icons/ui_editrequest.png') }}" alt="">
        Requests
    </h1>
    <a href="{{ url_for('request_new') }}" class="btn btn-primary">
        <img src="{{ url_for('static', filename='icons/ui_editrequest.png') }}" alt="">
        New Request
    </a>
</div>

<form method="GET" class="filter-form">
    <img src="{{ url_for('static', filename='icons/ui_filter.png') }}" alt="" style="height: 32px;">
    <div class="filter-group">
        <label for="project">Project</label>
        <select id="project" name="project">
            <option value="">All Projects</option>
            {% for p in projects %}
            <option value="{{ p.prjid }}" {% if filter_project == p.prjid %}selected{% endif %}>{{ p.prjname }}</option>
            {% endfor %}
        </select>
    </div>

    <div class="filter-group">
        <label for="status">Status</label>
        <select id="status" name="status">
            <option value="">All Statuses</option>
            {% for s in V2_STATUSES %}
            <option value="{{ s }}" {% if filter_status == s %}selected{% endif %}>{{ STATUS_NAMES.get(s, s) }}</option>
            {% endfor %}
        </select>
    </div>

    <div class="filter-group">
        <label for="phase">Phase</label>
        <select id="phase" name="phase">
            <option value="">All Phases</option>
            {% for p in PHASES[:-1] %}
            <option value="{{ p }}" {% if filter_phase == p %}selected{% endif %}>{{ PHASE_NAMES.get(p, p) }}</option>
            {% endfor %}
        </select>
    </div>

    <button type="submit" class="btn btn-primary">
        <img src="{{ url_for('static', filename='icons/ui_filterrecords.png') }}" alt="">
        Filter
    </button>
    <a href="{{ url_for('requests_list') }}" class="btn">Clear</a>
</form>

{% if requests %}
<table class="table">
    <thead>
        <tr>
            <th>ID</th>
            <th>Project</th>
            <th>Name</th>
            <th>Type</th>
            <th>Phase</th>
            <th>Status</th>
            <th>Priority</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        {% for r in requests %}
        <tr>
            <td><a href="{{ url_for('request_view', reqid=r.reqid) }}">{{ r.reqid }}</a></td>
            <td>{{ r.prjname }}</td>
            <td>{{ r.reqname }}</td>
            <td>{{ TYPE_NAMES.get(r.reqtype, r.reqtype) }}</td>
            <td><span class="badge badge-phase">{{ PHASE_NAMES.get(r.reqphase, r.reqphase) }}</span></td>
            <td><span class="badge badge-{{ r.reqstatus }}">{{ STATUS_NAMES.get(r.reqstatus, r.reqstatus) }}</span></td>
            <td>{{ r.reqpriority }}</td>
            <td>
                <a href="{{ url_for('request_view', reqid=r.reqid) }}" class="btn btn-sm">
                    <img src="{{ url_for('static', filename='icons/ui_viewdetails.png') }}" alt="">
                    View
                </a>
                <a href="{{ url_for('request_edit', reqid=r.reqid) }}" class="btn btn-sm">
                    <img src="{{ url_for('static', filename='icons/ui_editrequest.png') }}" alt="">
                    Edit
                </a>
            </td>
        </tr>
        {% endfor %}
    </tbody>
</table>
{% else %}
<p class="empty-state">No requests found. <a href="{{ url_for('request_new') }}">Create one</a></p>
{% endif %}
{% endblock %}
