<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% if item %}Update{% else %}Add{% endif %} Item</title>
    {% include 'favicon.html' %}
    <link href="{{ url_for('static', filename='css/bootstrap.min.css') }}" rel="stylesheet">
    <link href="{{ url_for('static', filename='css/bootstrap-icons.min.css') }}" rel="stylesheet">
    <style>
        .form-container {
            max-width: 1000px;
            margin: 0 auto;
        }
        .form-card {
            background: white;
            border-radius: 0.5rem;
            box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1);
        }
        .form-field {
            margin-bottom: 1.5rem;
        }
        .sticky-actions {
            position: sticky;
            bottom: 0;
            background: white;
            padding: 1rem 0;
            border-top: 1px solid #dee2e6;
            margin-top: 2rem;
        }
        .error-message {
            color: #dc3545;
            margin-bottom: 1.5rem;
            padding: 0.75rem;
            background-color: #f8d7da;
            border-radius: 0.25rem;
        }
        select.form-control {
            appearance: auto; /* Keep native select dropdown arrow */
        }
    </style>
</head>
<body>
    {% include 'header.html' %}
    
    <main class="container mt-5">
        <div class="form-container">
            <h2 class="mb-4">{% if item %}Update{% else %}Add{% endif %} Item</h2>

            <!-- Display error message if any -->
            {% if error %}
                <div class="error-message">
                    {{ error }}
                </div>
            {% endif %}

            <div class="form-card p-4">
                <form method="POST">
                    <div class="row">
                        {% for attrib in item_attributes -%}
                        <div class="col-md-6">
                            <div class="form-field">
                                <label for="{{ attrib.attrib_name }}" class="form-label fw-bold mb-2">
                                    {{ attrib.display_name }}
                                    {% if attrib.required %}<span class="text-danger">*</span>{% endif %}
                                </label>
                                
                                {%- if attrib.html_input_type == "select" %}
                                <!-- Dropdown for select fields -->
                                <select class="form-control" 
                                        id="{{ attrib.attrib_name }}" 
                                        name="{{ attrib.attrib_name }}"
                                        {%- if attrib.required %} required {% endif %}>
                                    {% for option in attrib.options -%}
                                        <option value="{{ option }}"
                                            {%- if item and item[attrib.attrib_name] == option %} selected {% endif %}
                                            {%- if not item and attrib.default_val is not none and attrib.default_val == option %} selected {% endif %}>
                                            {{ option }}
                                        </option>
                                    {% endfor %}
                                </select>
                                {%- else %}
                                <!-- Input field for other inputs -->
                                <input class="form-control" 
                                       id="{{ attrib.attrib_name }}" 
                                       name="{{ attrib.attrib_name }}" 
                                       type="{{ attrib.html_input_type }}"
                                       {% if attrib.required %} required {% endif %}
                                       {% if attrib.min_val %} min="{{ attrib.min_val }}" {% endif %}
                                       {% if attrib.max_val %} max="{{ attrib.max_val }}" {% endif %}
                                       {% if attrib.step %} step="{{ attrib.step }}" {% endif %}
                                       {% if attrib.max_length %} maxlength="{{ attrib.max_length }}" {% endif %}
                                       {% if attrib.min_length %} minlength="{{ attrib.min_length }}" {% endif %}
                                       {% if attrib.regex %} pattern="{{ attrib.regex }}" {% endif %}
                                       {% if item %} value="{{ item[attrib.attrib_name] }}" {% endif %}
                                       {% if not item and attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %}>
                                {%- endif %}
                            </div>
                        </div>
                        {% endfor %}
                    </div>

                    <!-- Sticky action buttons -->
                    <div class="sticky-actions">
                        <div class="d-flex justify-content-between">
                            <button type="button" 
                                    onclick="window.location.href='/'" 
                                    class="btn btn-outline-secondary px-4">
                                <i class="bi bi-x-lg me-1"></i>Cancel
                            </button>
                            <button type="submit" class="btn btn-primary px-4">
                                <i class="bi bi-check-lg me-1"></i>
                                {% if item %}Update{% else %}Submit{% endif %}
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </main>

    <!-- Bootstrap JS -->
    <script src="{{ url_for('static', filename='js/bootstrap.bundle.min.js') }}"></script>
</body>
</html>