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

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

{% block extra_css %}
<style>
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    .page-header-gradient {
        background: linear-gradient(135deg, var(--hh-navy) 0%, #0069a8 50%, var(--hh-blue) 100%);
        color: white;
        padding: 1.5rem 2rem;
        border-radius: 1rem;
        margin-bottom: 1.5rem;
        box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
    }

    .form-section {
        background: #fff;
        border: 2px solid #e2e8f0;
        border-radius: 1rem;
        padding: 1.5rem;
        margin-bottom: 1.5rem;
        transition: all 0.3s ease;
    }
    .form-section:hover {
        border-color: #005696;
        box-shadow: 0 4px 12px rgba(0, 86, 150, 0.1);
    }

    .form-section-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: #1e293b;
        margin-bottom: 1.25rem;
        padding-bottom: 0.75rem;
        border-bottom: 2px solid #005696;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }

    .required-field::after {
        content: " *";
        color: #ef4444;
    }

    .form-control-px360 {
        width: 100%;
        padding: 0.625rem 0.875rem;
        border: 2px solid #e2e8f0;
        border-radius: 0.5rem;
        font-size: 0.875rem;
        transition: all 0.2s;
    }
    .form-control-px360:focus {
        outline: none;
        border-color: #005696;
        box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
    }

    .form-select-px360 {
        width: 100%;
        padding: 0.625rem 0.875rem;
        border: 2px solid #e2e8f0;
        border-radius: 0.5rem;
        font-size: 0.875rem;
        transition: all 0.2s;
        background: white;
    }
    .form-select-px360:focus {
        outline: none;
        border-color: #005696;
        box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.1);
    }

    .btn-transition {
        transition: all 0.2s ease-in-out;
    }

    .alert-info-px360 {
        background: #e7f3ff;
        border: 1px solid #005696;
        border-radius: 0.75rem;
        padding: 1rem;
    }

    .alert-secondary-px360 {
        background: #f1f5f9;
        border: 1px solid #cbd5e1;
        border-radius: 0.75rem;
        padding: 1rem;
    }

    .hh-btn-primary {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.75rem 1.5rem;
        background: #005696;
        color: white;
        border-radius: 0.75rem;
        font-weight: 600;
        transition: all 0.2s ease;
        border: none;
        cursor: pointer;
    }
    .hh-btn-primary:hover {
        background: #007bbd;
    }

    .hh-btn-secondary {
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.75rem 1.5rem;
        background: white;
        color: #64748b;
        border: 2px solid #e2e8f0;
        border-radius: 0.75rem;
        font-weight: 600;
        transition: all 0.2s ease;
    }
    .hh-btn-secondary:hover {
        background: #f1f5f9;
        border-color: #005696;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <!-- Page Header -->
    <div class="page-header-gradient mb-6">
        <div class="flex items-center gap-3">
            <div class="w-12 h-12 bg-white/20 rounded-xl flex items-center justify-center">
                {% if folder %}
                <i data-lucide="folder-edit" class="w-6 h-6 text-white"></i>
                {% else %}
                <i data-lucide="folder-plus" class="w-6 h-6 text-white"></i>
                {% endif %}
            </div>
            <div>
                <h2 class="text-2xl font-bold">
                    {% if folder %}{{ _("Edit Folder")}}{% else %}{{ _("New Folder")}}{% endif %}
                </h2>
                <p class="text-blue-100 text-sm">
                    {% if folder %}{{ _("Update folder information")}}{% else %}{{ _("Create a new folder")}}{% endif %}
                </p>
            </div>
        </div>
    </div>

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

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
            <div class="lg:col-span-2">
                <!-- Folder Details -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i data-lucide="info" class="w-5 h-5"></i>{{ _("Folder Information")}}
                    </h5>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1.5 required-field">{% trans "Name (English)" %}</label>
                        <input type="text" name="name" class="form-control-px360" 
                               value="{% if folder %}{{ folder.name }}{% endif %}"
                               placeholder="{% trans 'Enter folder name' %}" required>
                    </div>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Name (Arabic)" %}</label>
                        <input type="text" name="name_ar" class="form-control-px360" dir="rtl"
                               value="{% if folder %}{{ folder.name_ar }}{% endif %}">
                    </div>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Description (English)" %}</label>
                        <textarea name="description" class="form-control-px360" rows="4">{% if folder %}{{ folder.description }}{% endif %}</textarea>
                    </div>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Description (Arabic)" %}</label>
                        <textarea name="description_ar" class="form-control-px360" rows="4" dir="rtl">{% if folder %}{{ folder.description_ar }}{% endif %}</textarea>
                    </div>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Parent Folder" %}</label>
                        <select name="parent" class="form-select-px360">
                            <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 }}
                            </option>
                            {% endif %}
                            {% endfor %}
                        </select>
                    </div>

                    <div class="grid grid-cols-1 md:grid-cols-3 gap-4">
                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Icon" %}</label>
                            <input type="text" name="icon" class="form-control-px360"
                                   value="{% if folder %}{{ folder.icon }}{% endif %}"
                                   placeholder="{% trans 'fa-folder' %}">
                        </div>

                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Color" %}</label>
                            <input type="text" name="color" class="form-control-px360"
                                   value="{% if folder %}{{ folder.color }}{% endif %}"
                                   placeholder="{% trans '#007bff' %}">
                        </div>

                        <div class="mb-4">
                            <label class="block text-sm font-medium text-gray-700 mb-1.5">{% trans "Order" %}</label>
                            <input type="number" name="order" class="form-control-px360"
                                   value="{% if folder %}{{ folder.order }}{% else %}0{% endif %}"
                                   min="0">
                        </div>
                    </div>
                </div>

                <!-- Access Control -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i data-lucide="shield" class="w-5 h-5"></i>{{ _("Access Control")}}
                    </h5>

                    <div class="mb-4">
                        <label class="block text-sm font-medium text-gray-700 mb-2">{% trans "Access Roles" %}</label>
                        <div class="border-2 border-gray-200 rounded-lg p-4 max-h-48 overflow-y-auto">
                            {% for choice in form.access_roles %}
                            <div class="flex items-center gap-2 mb-2">
                                {{ choice.tag }}
                                <label for="{{ choice.id_for_label }}" class="text-sm text-gray-700">{{ choice.choice_label }}</label>
                            </div>
                            {% endfor %}
                        </div>
                    </div>

                    <div class="flex items-center gap-2 p-3 bg-slate-50 rounded-lg">
                        <input type="checkbox" name="is_active" id="isActiveCheck" class="w-4 h-4 text-navy rounded"
                               {% if not folder or folder.is_active %}checked{% endif %}>
                        <label for="isActiveCheck" class="text-sm text-gray-700 font-medium">
                            {% trans "Active" %}
                        </label>
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="lg:col-span-1">
                <!-- Guidelines -->
                <div class="alert-info-px360 mb-4">
                    <h6 class="font-semibold text-navy mb-2 flex items-center gap-2">
                        <i data-lucide="info" class="w-5 h-5"></i>{{ _("Folder Organization")}}
                    </h6>
                    <ul class="text-xs text-slate-600 space-y-1 list-disc list-inside">
                        <li>{{ _("Use meaningful names")}}</li>
                        <li>{{ _("Add descriptions")}}</li>
                        <li>{{ _("Set access roles")}}</li>
                        <li>{{ _("Use icons and colors")}}</li>
                    </ul>
                </div>

                <!-- Current Info -->
                {% if folder %}
                <div class="alert-secondary-px360 mb-4">
                    <h6 class="font-semibold text-slate-700 mb-2 flex items-center gap-2">
                        <i data-lucide="folder" class="w-5 h-5"></i>{{ _("Current Folder")}}
                    </h6>
                    <ul class="text-xs text-slate-600 space-y-1">
                        <li><strong>{{ _("Created:") }}</strong> {{ folder.created_at|date:"M d, Y" }}</li>
                        <li><strong>{{ _("Modified:") }}</strong> {{ folder.updated_at|date:"M d, Y" }}</li>
                        <li><strong>{{ _("Documents:") }}</strong> {{ folder.documents.count }}</li>
                    </ul>
                </div>
                {% endif %}

                <!-- Action Buttons -->
                <div class="flex flex-col gap-2 sticky top-4">
                    <button type="submit" class="hh-btn hh-btn-primary justify-center shadow-lg">
                        <i data-lucide="check-circle" class="w-5 h-5"></i>
                        {% if folder %}{{ _("Update")}}{% else %}{{ _("Create")}}{% endif %}
                    </button>
                    <a href="{% if folder %}{% url 'references:folder_view' folder.id %}{% elif parent %}{% url 'references:folder_view' parent.id %}{% else %}{% url 'references:dashboard' %}{% endif %}" 
                       class="hh-btn hh-btn-secondary justify-center">
                        <i data-lucide="x-circle" class="w-5 h-5"></i>{{ _("Cancel")}}
                    </a>
                </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 %}
