{% extends 'layouts/base.html' %}
{% load i18n %}

{% block title %}{{ form_title|default:_("New RCA") }} - PX360{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="bg-white rounded-2xl shadow-sm border border-slate-100 p-6 mb-6">
    <div class="flex items-center gap-2 text-sm text-slate mb-3">
        <a href="{% url 'rca:rca_list' %}" class="hover:text-navy">{% trans "RCA" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <span class="font-bold text-navy">{{ form_title|default:_("New RCA") }}</span>
    </div>
    <h1 class="text-2xl font-bold text-navy flex items-center gap-3">
        <div class="p-2 bg-purple-500/10 rounded-lg">
            <i data-lucide="search" class="w-6 h-6 text-purple-600"></i>
        </div>
        {{ form_title|default:_("New Root Cause Analysis") }}
    </h1>
</div>

<!-- Linked Item Alert -->
{% if related_item %}
<div class="bg-blue-50 border border-blue-200 rounded-xl px-5 py-4 mb-6 flex items-center gap-3">
    <div class="p-2 bg-blue-100 rounded-lg">
        <i data-lucide="link" class="w-5 h-5 text-blue-600"></i>
    </div>
    <div>
        <span class="text-sm font-bold text-blue-800">{% trans "Linked to" %} {{ related_item_type|title }}:</span>
        <a href="{{ related_item.get_absolute_url }}" class="text-sm font-semibold text-blue-700 hover:text-blue-900 hover:underline ml-1">{{ related_item }}</a>
    </div>
</div>
{% endif %}

<!-- Form -->
<div class="bg-white rounded-2xl shadow-sm border border-slate-100">
    <form method="post" novalidate>
        {% csrf_token %}
        {{ form.related_model }}
        {{ form.related_id }}
        <div class="grid grid-cols-1 lg:grid-cols-3 gap-0 lg:divide-x divide-slate-100">
            <!-- Main Form Fields -->
            <div class="lg:col-span-2 p-6 space-y-5">
                <!-- Title -->
                <div>
                    <label for="id_title" class="form-label text-xs font-bold text-slate uppercase">{% trans "Title" %} <span class="text-danger">*</span></label>
                    <input type="text" name="title" id="id_title" class="form-control" maxlength="500" value="{{ form.title.value|default:'' }}" required>
                    {% if form.title.errors %}
                    <p class="text-danger text-xs mt-1 flex items-center gap-1"><i data-lucide="alert-circle" class="w-3 h-3"></i> {{ form.title.errors.0 }}</p>
                    {% endif %}
                </div>

                <!-- Description -->
                <div>
                    <label for="id_description" class="form-label text-xs font-bold text-slate uppercase">{% trans "Description" %} <span class="text-danger">*</span></label>
                    <textarea name="description" id="id_description" class="form-control" rows="4" required>{{ form.description.value|default:'' }}</textarea>
                    {% if form.description.errors %}
                    <p class="text-danger text-xs mt-1 flex items-center gap-1"><i data-lucide="alert-circle" class="w-3 h-3"></i> {{ form.description.errors.0 }}</p>
                    {% endif %}
                </div>

                <!-- Background -->
                <div>
                    <label for="id_background" class="form-label text-xs font-bold text-slate uppercase">{% trans "Background" %}</label>
                    <textarea name="background" id="id_background" class="form-control" rows="3">{{ form.background.value|default:'' }}</textarea>
                </div>

                <!-- Root Cause Summary -->
                <div>
                    <label for="id_root_cause_summary" class="form-label text-xs font-bold text-slate uppercase">{% trans "Root Cause Summary" %}</label>
                    <textarea name="root_cause_summary" id="id_root_cause_summary" class="form-control" rows="3">{{ form.root_cause_summary.value|default:'' }}</textarea>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="lg:col-span-1 p-6 bg-slate-50/50 space-y-4">
                <!-- Department -->
                <div>
                    <label for="id_department" class="form-label text-xs font-bold text-slate uppercase">{% trans "Department" %}</label>
                    {{ form.department }}
                </div>

                <!-- Status -->
                <div>
                    <label for="id_status" class="form-label text-xs font-bold text-slate uppercase">{% trans "Status" %}</label>
                    {{ form.status }}
                </div>

                <!-- Severity -->
                <div>
                    <label for="id_severity" class="form-label text-xs font-bold text-slate uppercase">{% trans "Severity" %}</label>
                    {{ form.severity }}
                </div>

                <!-- Priority -->
                <div>
                    <label for="id_priority" class="form-label text-xs font-bold text-slate uppercase">{% trans "Priority" %}</label>
                    {{ form.priority }}
                </div>

                <!-- Assigned To -->
                <div>
                    <label for="id_assigned_to" class="form-label text-xs font-bold text-slate uppercase">{% trans "Assigned To" %}</label>
                    {{ form.assigned_to }}
                </div>

                <!-- Target Completion -->
                <div>
                    <label for="id_target_completion_date" class="form-label text-xs font-bold text-slate uppercase">{% trans "Target Completion" %}</label>
                    {{ form.target_completion_date }}
                </div>

                <!-- Action Buttons -->
                <div class="pt-4 border-t border-slate-200 space-y-3">
                    <button type="submit" class="w-full bg-gradient-to-r from-navy to-blue text-white px-4 py-3 rounded-xl text-sm font-bold shadow-md hover:shadow-lg hover:-translate-y-0.5 transition inline-flex items-center justify-center gap-2">
                        <i data-lucide="save" class="w-4 h-4"></i> {% trans "Save" %}
                    </button>
                    <a href="{% url 'rca:rca_list' %}" class="block text-center px-4 py-3 border border-slate-200 rounded-xl text-sm font-semibold text-slate hover:bg-white transition">
                        {% trans "Cancel" %}
                    </a>
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}
