{% extends "layouts/base.html" %}
{% load i18n %}
{% load static %}

{% block title %}{% if folder %}{{ _("Edit Folder") }}{% else %}{{ _("New Folder") }}{% endif %} - PX360{% endblock %}

{% block extra_css %}
<style>
    .form-section {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 25px;
        margin-bottom: 20px;
    }
    .form-section-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: #495057;
        margin-bottom: 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #667eea;
    }
    .required-field::after {
        content: " *";
        color: #dc3545;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="mb-4">
        {% if folder %}
        <a href="{% url 'references:folder_view' folder.id %}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Folder")}}
        </a>
        {% elif parent %}
        <a href="{% url 'references:folder_view' parent.id %}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Folder")}}
        </a>
        {% else %}
        <a href="{% url 'references:dashboard' %}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Dashboard")}}
        </a>
        {% endif %}
        <h2 class="mb-1">
            {% if folder %}
            <i class="bi bi-pencil-square text-primary me-2"></i>{{ _("Edit Folder")}}
            {% else %}
            <i class="bi bi-folder-plus text-primary me-2"></i>{{ _("New Folder")}}
            {% endif %}
        </h2>
        <p class="text-muted mb-0">
            {% if folder %}
            {{ _("Update folder information")}}
            {% else %}
            {{ _("Create a new folder to organize your documents")}}
            {% endif %}
        </p>
    </div>

    <form method="post" id="folderForm">
        {% csrf_token %}

        <div class="row">
            <div class="col-lg-8">
                <!-- Folder Details -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i class="bi bi-info-circle me-2"></i>{{ _("Folder Information")}}
                    </h5>

                    <div class="mb-3">
                        <label class="form-label required-field">{% trans "Name (English)" %}</label>
                        <input type="text" name="name" class="form-control"
                               value="{% if folder %}{{ folder.name }}{% endif %}"
                               placeholder="{% trans 'Enter folder name in English' %}" required>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">{% trans "Name (Arabic)" %}</label>
                        <input type="text" name="name_ar" class="form-control"
                               value="{% if folder %}{{ folder.name_ar }}{% endif %}"
                               placeholder="{% trans 'Enter folder name in Arabic (optional)' %}">
                    </div>

                    <div class="mb-3">
                        <label class="form-label">{% trans "Description (English)" %}</label>
                        <textarea name="description" class="form-control" rows="4"
                                  placeholder="{% trans 'Optional description of this folder' %}">{% if folder %}{{ folder.description }}{% endif %}</textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">{% trans "Description (Arabic)" %}</label>
                        <textarea name="description_ar" class="form-control" rows="4"
                                  placeholder="{% trans 'Optional Arabic description' %}">{% if folder %}{{ folder.description_ar }}{% endif %}</textarea>
                    </div>

                    <div class="mb-3">
                        <label class="form-label">{% trans "Parent Folder" %}</label>
                        <select name="parent" class="form-select">
                            <option value="">{{ _("Root Level (No Parent)")}}</option>
                            {% for folder_option in all_folders %}
                            {% if folder_option.id != folder.id %}
                            <option value="{{ folder_option.id }}"
                                    {% if folder and folder.parent_id == folder_option.id %}selected{% endif %}
                                    {% if parent and parent.id == folder_option.id %}selected{% endif %}>
                                {{ folder_option.level_display }}{{ folder_option.name }}
                                {% if folder_option.name_ar %}/ {{ folder_option.name_ar }}{% endif %}
                            </option>
                            {% endif %}
                            {% endfor %}
                        </select>
                        <small class="form-text text-muted">{{ _("Leave empty to create at root level")}}</small>
                    </div>

                    <div class="row">
                        <div class="col-md-4 mb-3">
                            <label class="form-label">{% trans "Icon" %}</label>
                            <input type="text" name="icon" class="form-control"
                                   value="{% if folder %}{{ folder.icon }}{% endif %}"
                                   placeholder="{% trans 'e.g., fa-folder, fa-file-pdf' %}">
                            <small class="form-text text-muted">{{ _("FontAwesome icon class")}}</small>
                        </div>

                        <div class="col-md-4 mb-3">
                            <label class="form-label">{% trans "Color" %}</label>
                            <input type="text" name="color" class="form-control"
                                   value="{% if folder %}{{ folder.color }}{% endif %}"
                                   placeholder="{% trans 'e.g., #007bff' %}">
                            <small class="form-text text-muted">{{ _("Hex color code")}}</small>
                        </div>

                        <div class="col-md-4 mb-3">
                            <label class="form-label">{% trans "Order" %}</label>
                            <input type="number" name="order" class="form-control"
                                   value="{% if folder %}{{ folder.order }}{% else %}0{% endif %}"
                                   min="0">
                            <small class="form-text text-muted">{{ _("Display order")}}</small>
                        </div>
                    </div>
                </div>

                <!-- Access Control -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i class="bi bi-shield-lock me-2"></i>{{ _("Access Control")}}
                    </h5>

                    <div class="mb-3">
                        <label class="form-label">{% trans "Access Roles" %}</label>
                        <div class="border p-3 rounded" style="max-height: 200px; overflow-y: auto;">
                            {% for choice in form.access_roles %}
                            <div class="form-check mb-2">
                                {{ choice.tag }}
                                <label class="form-check-label" for="{{ choice.id_for_label }}">
                                    {{ choice.choice_label }}
                                </label>
                            </div>
                            {% endfor %}
                        </div>
                        <small class="form-text text-muted">
                            {{ _("Leave all unchecked to make folder accessible to all users")}}
                        </small>
                    </div>

                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" name="is_active" id="isActiveCheck"
                               {% if not folder or folder.is_active %}checked{% endif %}>
                        <label class="form-check-label" for="isActiveCheck">
                            {% trans "Active" %}
                        </label>
                        <small class="form-text text-muted d-block">
                            {{ _("Uncheck to hide this folder")}}
                        </small>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">
                <!-- Information -->
                <div class="alert alert-info">
                    <h6 class="alert-heading">
                        <i class="bi bi-info-circle me-2"></i>{{ _("Folder Organization")}}
                    </h6>
                    <p class="mb-0 small">
                        {{ _("Create folders to organize documents by category, department, or any structure that works for your team.")}}
                    </p>
                    <hr class="my-2">
                    <ul class="mb-0 mt-2 small">
                        <li>{{ _("Use meaningful names in both languages")}}</li>
                        <li>{{ _("Add descriptions for clarity")}}</li>
                        <li>{{ _("Set access roles to control visibility")}}</li>
                        <li>{{ _("Use icons and colors for visual organization")}}</li>
                    </ul>
                </div>

                <!-- Current Folder Info (if editing) -->
                {% if folder %}
                <div class="alert alert-secondary">
                    <h6 class="alert-heading">
                        <i class="bi bi-folder me-2"></i>{{ _("Current Folder")}}
                    </h6>
                    <ul class="mb-0 small">
                        <li><strong>{{ _("Created:") }} </strong>{{ folder.created_at|date:"M d, Y" }}</li>
                        <li><strong>{{ _("Last Modified:") }} </strong>{{ folder.updated_at|date:"M d, Y" }}</li>
                        <li><strong>{{ _("Documents:") }} </strong>{{ folder.documents.count }}</li>
                        <li><strong>{{ _("Subfolders:") }} </strong>{{ folder.subfolders.count }}</li>
                    </ul>
                </div>
                {% endif %}

                <!-- Action Buttons -->
                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="bi bi-check-circle me-2"></i>
                        {% if folder %}{{ _("Update Folder")}}{% else %}{{ _("Create Folder")}}{% endif %}
                    </button>
                    {% if folder %}
                    <a href="{% url 'references:folder_view' folder.id %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{{ _("Cancel")}}
                    </a>
                    {% elif parent %}
                    <a href="{% url 'references:folder_view' parent.id %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{{ _("Cancel")}}
                    </a>
                    {% else %}
                    <a href="{% url 'references:dashboard' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{{ _("Cancel")}}
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const form = document.getElementById('folderForm');

    form.addEventListener('submit', function(e) {
        if (!form.checkValidity()) {
            e.preventDefault();
            e.stopPropagation();
        }
        form.classList.add('was-validated');
    });
});
</script>
{% endblock %}
