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

{% block title %}{% if form.instance.pk %}Update Task{% else %}Create New Task{% endif %} | TaskFlow{% endblock %}

{% block content %}
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-10">
            <div class="card shadow-sm border-0 rounded-lg">
                <div class="card-header bg-primary text-white">
                    <h2 class="h4 mb-0">
                        <i class="fas {% if form.instance.pk %}fa-edit{% else %}fa-plus-circle{% endif %} me-2"></i>
                        {% if form.instance.pk %}Update Task{% else %}Create New Task{% endif %}
                    </h2>
                </div>
                <div class="card-body p-4">
                    <form method="post" class="needs-validation" novalidate enctype="multipart/form-data">
                        {% csrf_token %}
                        
                        <div class="row g-4">
                            <!-- Left Column -->
                            <div class="col-md-6">
                                <!-- Title -->
                                <div class="mb-3">
                                    <label for="id_title" class="form-label fw-bold">
                                        <i class="fas fa-heading me-2 text-primary"></i>Task Title
                                    </label>
                                    {{ form.title }}
                                    <div class="invalid-feedback">
                                        Please provide a task title.
                                    </div>
                                </div>
                                
                                <!-- Description -->
                                <div class="mb-3">
                                    <label for="id_description" class="form-label fw-bold">
                                        <i class="fas fa-align-left me-2 text-primary"></i>Description
                                    </label>
                                    {{ form.description }}
                                    <div class="form-text">Provide a clear and detailed description of the task.</div>
                                </div>
                                
                                <!-- Priority -->
                                <div class="mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-flag me-2 text-primary"></i>Priority
                                    </label>
                                    {{ form.priority }}
                                </div>
                                
                                <!-- Scope -->
                                <div class="mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-globe me-2 text-primary"></i>Scope
                                    </label>
                                    {{ form.scope }}
                                    <div class="form-text">Is this task for your department or national level?</div>
                                </div>
                            </div>
                            
                            <!-- Right Column -->
                            <div class="col-md-6">
                                <!-- Status -->
                                <div class="mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-tasks me-2 text-primary"></i>Status
                                    </label>
                                    {{ form.status }}
                                </div>
                                
                                <!-- Due Date -->
                                <div class="mb-3">
                                    <label for="id_due_date" class="form-label fw-bold">
                                        <i class="far fa-calendar-alt me-2 text-primary"></i>Due Date
                                    </label>
                                    {{ form.due_date }}
                                    <div class="form-text">When should this task be completed?</div>
                                </div>
                                
                                <!-- Assigned To -->
                                <div class="mb-3">
                                    <label class="form-label fw-bold">
                                        <i class="fas fa-user-tie me-2 text-primary"></i>Assigned To
                                    </label>
                                    {{ form.assigned_to }}
                                    <div class="form-text">Leave empty if unassigned</div>
                                </div>
                                
                                <!-- Requires Approval -->
                                <div class="mb-3">
                                    <div class="form-check form-switch">
                                        {{ form.requires_approval }}
                                        <label class="form-check-label fw-bold" for="id_requires_approval">
                                            <i class="fas fa-clipboard-check me-2 text-primary"></i>Requires Approval
                                        </label>
                                    </div>
                                    <div class="form-text">Check if this task needs approval before being marked complete</div>
                                </div>
                            </div>
                            
                            <!-- Full Width Row for Additional Fields -->
                            <div class="col-12">
                                <div class="card bg-light mb-4">
                                    <div class="card-header bg-light">
                                        <h5 class="mb-0">
                                            <i class="fas fa-paperclip me-2"></i>Attachments (Optional)
                                        </h5>
                                    </div>
                                    <div class="card-body">
                                        <div class="mb-3">
                                            <label for="id_attachments" class="form-label">Upload files</label>
                                            <input type="file" class="form-control" name="attachments" id="id_attachments" multiple>
                                            <div class="form-text">You can select multiple files (max 10MB each)</div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        
                        <!-- Form Actions -->
                        <div class="d-flex justify-content-between align-items-center mt-4 pt-3 border-top">
                            <a href="{% url 'tasks:task-list' %}" class="btn btn-outline-secondary">
                                <i class="fas fa-arrow-left me-2"></i>Cancel
                            </a>
                            <div>
                                {% if form.instance.pk %}
                                    <a href="{% url 'tasks:task-delete' form.instance.pk %}" class="btn btn-danger me-2">
                                        <i class="fas fa-trash-alt me-2"></i>Delete
                                    </a>
                                {% endif %}
                                <button type="submit" class="btn btn-primary px-4">
                                    <i class="fas {% if form.instance.pk %}fa-save{% else %}fa-plus-circle{% endif %} me-2"></i>
                                    {% if form.instance.pk %}Update Task{% else %}Create Task{% endif %}
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- Add datepicker initialization script -->
{% block extra_js %}
<script>
    // Initialize datepicker if using one
    $(document).ready(function() {
        $('#id_due_date').attr('type', 'datetime-local');
        
        // Add form validation feedback
        (function () {
            'use strict'
            
            // Fetch all the forms we want to apply custom Bootstrap validation styles to
            var forms = document.querySelectorAll('.needs-validation')
            
            // Loop over them and prevent submission
            Array.prototype.slice.call(forms)
                .forEach(function (form) {
                    form.addEventListener('submit', function (event) {
                        if (!form.checkValidity()) {
                            event.preventDefault()
                            event.stopPropagation()
                        }
                        
                        form.classList.add('was-validated')
                    }, false)
                })
        })()
    });
</script>
{% endblock %}

{% endblock %}
