{% extends "base.html" %}
{% block title_suffix %} - {% if req %}Edit{% else %}New{% endif %} Request{% endblock %}

{% block content %}
<div class="page-header">
    <h1>
        <img src="{{ url_for('static', filename='icons/ui_editrequest.png') }}" alt="">
        {% if req %}Edit{% else %}New{% endif %} Request
    </h1>
</div>

<form method="POST" class="form">
    <div class="form-group">
        <label for="project">Project *</label>
        {% if edit_mode %}
        {# In edit mode, project is locked - show as text and submit hidden field #}
        <input type="hidden" name="project" value="{{ req.prjid }}">
        <input type="text" value="{{ projects|selectattr('prjid', 'eq', req.prjid)|map(attribute='prjname')|first }}" disabled class="form-control-disabled">
        <small class="help-text">Project cannot be changed after creation</small>
        {% else %}
        <select id="project" name="project" required>
            <option value="">Select a project...</option>
            {% for p in projects %}
            <option value="{{ p.prjid }}"
                    {% if req and req.prjid == p.prjid %}selected{% endif %}
                    {% if request.args.get('project')|int == p.prjid %}selected{% endif %}>
                {{ p.prjname }}
            </option>
            {% endfor %}
        </select>
        {% endif %}
    </div>

    <div class="form-group">
        <label for="name">Name *</label>
        <input type="text" id="name" name="name" required
               value="{{ req.reqname if req else '' }}"
               placeholder="Add user authentication">
    </div>

    <div class="form-group">
        <label for="prompt">Prompt/Description *</label>
        <textarea id="prompt" name="prompt" rows="5" required
                  placeholder="Describe what needs to be done...">{{ req.reqprompt if req else '' }}</textarea>
    </div>

    <div class="form-group">
        <label for="type">Type</label>
        <select id="type" name="type">
            {% for t in VALID_REQUEST_TYPES %}
            <option value="{{ t }}" {% if req and req.reqtype == t %}selected{% endif %}>{{ TYPE_NAMES.get(t, t) }}</option>
            {% endfor %}
        </select>
    </div>

    <div class="form-group">
        <label for="priority">Priority (1-10)</label>
        <input type="number" id="priority" name="priority" min="1" max="10"
               value="{{ req.reqpriority if req else 5 }}">
    </div>

    <div class="form-group">
        <label for="codedir">Code Directory (optional)</label>
        <input type="text" id="codedir" name="codedir"
               value="{{ req.reqcodedir if req else '' }}"
               placeholder="/path/to/project">
    </div>

    <div class="button-group">
        <button type="submit" class="btn btn-primary">
            {% if req %}Update{% else %}Create{% endif %}
        </button>
        <a href="{{ url_for('requests_list') }}" class="btn">Cancel</a>
    </div>
</form>
{% endblock %}
