{% extends "main.html" %}
{%load static%}
{% block content %}

<div class="w-full px-5 s:px-9 mb-5">
    <div class="w-full h-full shadow-md rounded-md mt-5 py-5 px-3 bg-white">
        <h1 class="text-3xl text-secondosiblue text-center font-semibold">
            Edit Task
        </h1>

        <form class="w-full flex flex-col gap-5 justify-center items-center mt-5" method="POST"
            action="{% url 'edittask' task.task_id %}">
            {% csrf_token %}

            <div class="w-full">
                <label class="text-gray-500">Name:</label>
                <input required name="name" type="text"
                value="{{task.name}}"
                class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md mt-1">
            </div>

            <div class="w-full">
                <label class="text-gray-500">Project:</label>
                <select required name="project" id="projectDropdown"
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                    {% for project in projects %}
                    <option value="{{project.id}}" {% if project.id == task.project.id %} selected {% endif %}>{{project.name}}</option>
                    {% endfor %}
                </select>
            </div>
            
            <div class="w-full">
                <label class="text-gray-500">Epic:</label>
                <select required name="epic" id="epicDropdown"
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                    {% for epic in epics_of_my_project %}
                    <option value="{{epic.id}}" {% if epic.id == task.epic.id %} selected {% endif %}>{{epic.title}}</option>
                    {% endfor %}
                </select>
            </div>

            <div class="w-full">
                <label class="text-gray-500">Status:</label>
                <select required name="status"
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                    <option value="Open" {% if task.status == 'Open' %} selected {% endif %}>Open</option>
                    <option value="Working On" {% if task.status == 'Working On' %} selected {% endif %}>Working On</option>
                    <option value="Closed" {% if task.status == 'Closed' %} selected {% endif %} >Closed</option>
                </select>
            </div>

            <div class="w-full">
                <label class="text-gray-500">Assigned To:</label>
                <select name="assigned_to" required id=""
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">
                    {% for staff in staffs %}
                    <option value="{{staff.id}}" {% if task.assigned_to.id == staff.id %} selected {% endif %}>{{staff.user.first_name}} {{staff.user.last_name}}</option>
                    {% endfor %}
                </select>
            </div>

            <div class="w-full">
                <label class="text-gray-500">Description:</label>
                <textarea required name="description" type="text" rows="5" cols="5"
                    class="w-full py-3 px-3 border border-gray-300 outline-none rounded-md resize-none mt-1">{{task.description}}
                </textarea>
            </div>

            <div class="w-full">
                <label class="text-gray-500">Extra:</label>
                <select required name="extra" id=""
                    class="w-full h-[50px] py-1 px-3 border border-gray-300 outline-none rounded-md text-gray-500 mt-1">

                    <option value="True" {% if task.extra == True %} selected {% endif %}>Yes</option>
                    <option value="False" {% if task.extra == False %} selected {% endif %}>No</option>
                </select>
            </div>

            <div class="w-full">
                <label class="text-gray-500">Start Date:</label>
                <input required name="start_date" type="date"  id="startDateDisplay"
                value='{{task.start_date|date:"Y-m-d"}}'
                class="w-full p-3 border border-gray-300 rounded-md bg-transparent outline-none mt-1">
            </div>

            <div class="w-full">
                <label class="text-gray-500">End Date:</label>
                <input required name="end_date" type="date"  id="closingDateDisplay"
                value='{{task.end_date|date:"Y-m-d"}}'
                class="w-full p-3 border border-gray-300 rounded-md bg-transparent outline-none mt-1">
            </div>

            <script>
                const startDatePicker = document.getElementById('startDate');
                const startDateDisplay = document.getElementById('startDateDisplay');
                const closingDatePicker = document.getElementById('closingDate');
                const closingDateDisplay = document.getElementById('closingDateDisplay');
                // Adding an input event listener to the starting date picker input
                startDatePicker.addEventListener('input', () => {
                    // When a date is selected, update the value of the starting date display input
                    startDateDisplay.value = startDatePicker.value;
                });

                closingDatePicker.addEventListener('input', () => {
                    closingDateDisplay.value = closingDatePicker.value;
                });

                drawDatePicker.addEventListener('input', () => {
                    drawDateDisplay.value = drawDatePicker.value;
                });
            </script>

            <div class="w-full flex justify-center items-center mt-4">
                <button type="submit"
                    class="w-fit bg-osiblue border border-osiblue rounded-md text-white text-xl px-5 py-1 hover:bg-white hover:text-osiblue duration-300">Save</button>
            </div>
        </form>
    </div>
</div>
</div>


<!---------------------- JS SCRIPTS -------------------->
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>

<script type="text/javascript" src='{% static "js/fetch-epics-in-edit-task.js" %}'></script>


<!-- POPUP MODAL -->
<div class="w-full h-full bg-black bg-opacity-40 z-20 fixed justify-center items-center hidden" id="popUpModal">
    <div class="w-[95%] md:w-fit h-fit bg-white rounded-md p-9 relative">
        <button class="absolute top-3 right-5 text-slate-800 text-xl cursor-pointer outline-none border-none"
            id="closeModalButton">
            <i class="fa fa-close"></i>
        </button>
        <iframe id="popupModalFrame" frameborder="0"></iframe>
    </div>
</div>

<script type="text/javascript" src='{% static "js/pop-modals.js" %}'></script>
</div>

{% endblock content %}