{% extends 'base.html' %}
{% load static %}

{% block title %}Create Project - Packs Site{% endblock %}

{% block content %}
<h1><i class="fas fa-plus-circle"></i> New Project</h1>
<p class="bio-help">
    <a href="{% url 'library:my_projects' %}"><i class="fas fa-arrow-left"></i> Back to My Projects</a>
</p>
<form method="post" class="card project-form create-form" id="project-form">
    {% csrf_token %}

    <div class="create-grid">
        <section class="create-panel create-panel-main">
            <h2 class="form-section-title"><i class="fas fa-folder-open"></i> Project</h2>

            <div class="form-group">
                {{ project_form.title.errors }}
                <label for="{{ project_form.title.id_for_label }}">Title</label>
                {{ project_form.title }}
            </div>
            <div class="form-group">
                {{ project_form.summary.errors }}
                <label for="{{ project_form.summary.id_for_label }}">Summary</label>
                {{ project_form.summary }}
            </div>
            <div class="form-group">
                {{ project_form.category.errors }}
                <label for="{{ project_form.category.id_for_label }}">Category</label>
                {{ project_form.category }}
            </div>
            <div class="form-group" id="description-group">
                {{ project_form.description.errors }}
                <label for="{{ project_form.description.id_for_label }}">Description</label>
                {{ project_form.description }}
                <p class="bio-help"><i class="fas fa-info-circle"></i> Supports Markdown — headings, lists, links, code, tables and more. Guides don't use this field.</p>
            </div>
        </section>

        <section class="create-panel">
            <h2 class="form-section-title"><i class="fas fa-image"></i> Thumbnail</h2>
            <div class="upload-zone" data-upload="thumbnail">
                <div class="drop-zone" role="button" tabindex="0">
                    <div class="drop-zone-inner">
                        <i class="fas fa-cloud-upload-alt drop-zone-icon"></i>
                        <p class="drop-zone-text">Drag &amp; drop an image</p>
                        <p class="drop-zone-sub">or click to browse</p>
                    </div>
                </div>
                <input type="file" id="thumb-input" accept="image/*" hidden>
                <div class="upload-preview" id="thumb-preview"></div>
            </div>

            <h2 class="form-section-title" style="margin-top:16px;"><i class="fas fa-tags"></i> Tags</h2>
            <div class="tag-editor">
                <input type="text" id="tag-input" placeholder="Type a tag and press Enter… (e.g. content:threesome, species:dragon)" autocomplete="off">
                <div class="tag-suggestions" id="tag-suggestions" hidden></div>
                <div class="tag-chips" id="tag-chips"></div>
                {{ project_form.tags }}
            </div>
            <p class="bio-help"><i class="fas fa-info-circle"></i> Categories: loader, version, general, creator, content, species, non_version. Unprefixed tags go to General.</p>
        </section>

        <section class="create-panel">
            <h2 class="form-section-title"><i class="fas fa-box-open"></i> First version</h2>
            <div class="form-group">
                {{ version_form.version_name.errors }}
                <label for="{{ version_form.version_name.id_for_label }}">{{ version_form.version_name.label }}</label>
                {{ version_form.version_name }}
            </div>
            <div class="upload-zone" data-upload="version">
                <div class="drop-zone" role="button" tabindex="0">
                    <div class="drop-zone-inner">
                        <i class="fas fa-cloud-upload-alt drop-zone-icon"></i>
                        <p class="drop-zone-text">Drag &amp; drop the version files</p>
                        <p class="drop-zone-sub">or click to browse — several at once</p>
                    </div>
                </div>
                <input type="file" id="version-input" multiple hidden>
                <div class="file-preview-grid" id="version-preview"></div>
            </div>
            <div class="form-group">
                {{ version_form.changelog.errors }}
                <label for="{{ version_form.changelog.id_for_label }}">{{ version_form.changelog.label }}</label>
                {{ version_form.changelog }}
                <p class="bio-help"><i class="fas fa-info-circle"></i> Supports Markdown.</p>
            </div>

            <h2 class="form-section-title" style="margin-top:16px;"><i class="fas fa-images"></i> Gallery media</h2>
            <div class="upload-zone" data-upload="media">
                <div class="drop-zone" role="button" tabindex="0">
                    <div class="drop-zone-inner">
                        <i class="fas fa-cloud-upload-alt drop-zone-icon"></i>
                        <p class="drop-zone-text">Drag &amp; drop media</p>
                        <p class="drop-zone-sub">or click to browse — several at once</p>
                    </div>
                </div>
                <input type="file" id="media-input" accept="image/*,video/*" multiple hidden>
                <div class="upload-batch" id="media-batch" hidden>
                    <i class="fas fa-circle-notch fa-spin"></i>
                    <span class="upload-batch-status" id="media-batch-status"></span>
                </div>
                <div class="file-preview-grid" id="media-preview"></div>
            </div>
        </section>
    </div>

    <div class="form-actions">
        <span class="autosave-status" id="autosave-status"><i class="fas fa-spinner fa-spin"></i></span>
        <button type="submit" class="btn btn-primary" id="create-submit"><i class="fas fa-check"></i> Create project</button>
    </div>
</form>
{% endblock %}

{% block extra_js %}
{{ draft_uuid|json_script:'packs-draft-uuid' }}
{{ draft_uploads|json_script:'packs-draft-uploads' }}
{{ tag_categories|json_script:'packs-tag-categories' }}
<script src="{% static 'js/tags.js' %}"></script>
<script src="{% static 'js/uploads.js' %}"></script>
<script src="{% static 'js/create.js' %}"></script>
<script>
(function () {
    const cat = document.getElementById('id_category');
    const group = document.getElementById('description-group');
    if (!cat || !group) return;
    function toggle() { group.style.display = cat.value === 'guide' ? 'none' : ''; }
    cat.addEventListener('change', toggle);
    toggle();
})();
</script>
{% endblock %}
