{% extends "themis/base.html" %}

{% block title %}{% if editing %}Edit Item{% else %}New Item{% endif %} — {{ themis_app_name }}{% endblock %}

{% block content %}
<div class="mb-4">
    {% if collection %}
    <a href="{% url 'library:collection-detail' uid=collection.uid %}" class="btn btn-ghost btn-sm">← {{ collection.name }}</a>
    {% else %}
    <a href="{% url 'library:library-list' %}" class="btn btn-ghost btn-sm">← Libraries</a>
    {% endif %}
</div>

<h1 class="text-3xl font-bold mb-6">
    {% if editing %}Edit Item: {{ item.title }}{% else %}New Item{% endif %}
</h1>

<form method="post" enctype="multipart/form-data" class="max-w-2xl">
    {% csrf_token %}
    <div class="space-y-4">
        <div class="form-control">
            <label class="label"><span class="label-text font-medium">Title</span></label>
            {{ form.title }}
            {% if form.title.errors %}<p class="text-error text-sm mt-1">{{ form.title.errors.0 }}</p>{% endif %}
        </div>
        <div class="form-control">
            <label class="label"><span class="label-text font-medium">Item Type</span></label>
            {{ form.item_type }}
        </div>
        <div class="form-control">
            <label class="label"><span class="label-text font-medium">File Type</span></label>
            {{ form.file_type }}
            <label class="label"><span class="label-text-alt">Auto-detected from uploaded file if left blank</span></label>
        </div>

        {% if not editing %}
        <!-- File upload (only on create) -->
        <div class="form-control">
            <label class="label"><span class="label-text font-medium">Document File</span></label>
            {{ form.file }}
            <label class="label">
                <span class="label-text-alt">{{ form.file.help_text }}</span>
            </label>
            {% if form.file.errors %}<p class="text-error text-sm mt-1">{{ form.file.errors.0 }}</p>{% endif %}
        </div>

        <div class="form-control">
            <label class="label cursor-pointer justify-start gap-3">
                {{ form.auto_embed }}
                <span class="label-text">Auto-embed after upload</span>
            </label>
            <label class="label">
                <span class="label-text-alt">{{ form.auto_embed.help_text }}</span>
            </label>
        </div>
        {% endif %}
    </div>
    <div class="flex gap-2 mt-6">
        <button type="submit" class="btn btn-primary">
            {% if editing %}Save Changes{% else %}Create Item{% endif %}
        </button>
        {% if collection %}
        <a href="{% url 'library:collection-detail' uid=collection.uid %}" class="btn btn-ghost">Cancel</a>
        {% endif %}
    </div>
</form>
{% endblock %}
