{% extends 'index.html' %}
{% load static i18n %}
{% load i18n %}
{% load widget_tweaks %}

{% block styles %}
<link rel="stylesheet" href="{% static 'css/pms.css' %}" />
{% endblock styles %}

{% block content %}
{% if feedback_form.errors %}
<!-- form errors  -->
<div class="oh-wrapper">
    <div class="oh-alert-container">

        {% for error in feedback_form.non_field_errors %}
        <div class="oh-alert oh-alert--animated oh-alert--danger">
            {{ error }}
        </div>
        {% endfor %}
    </div>
</div>
{% endif %}

<div class="oh-wrapper" id="message">

</div>
<!-- Feedback form'-->
<main class="oh-onboarding">
    <div class="oh-onboarding-card oh-profile-section" style="max-width: 868px;">
        <form action="{%url 'feedback-creation' %}" id="feedbackCreationForm" method="post">
        {{feedback_form.non_field_errors}}
            {% csrf_token %}
            <div class="oh-onboarding-card__address-group">
                <h2 class="oh-onboarding-card__section-title">{% trans "Feedback" %}</h2>
                <div class="oh-input__group ">
                    <label class="oh-input__label" for="{{feedback_form.review_cycle.id_for_label}}">{% trans "Title" %}</label>
                    {{feedback_form.review_cycle}}
                    {{feedback_form.review_cycle.errors}}
                </div>
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.employee_id.id_for_label}}">{% trans "Employee" %}</label>
                                {{feedback_form.employee_id }}
                                {{feedback_form.employee_id.errors }}
                                <ul id="error-list" class="errorlist" style="display: none">
                                    <li>{% trans "This field is required" %}.</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.manager_id.id_for_label}}">{% trans "Manager" %}</label>
                                {{feedback_form.manager_id}}
                                {{feedback_form.manager_id.errros}}
                                <ul id="error-list" class="errorlist" style="display: none">
                                    <li>{% trans "This field is required" %}.</li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.subordinate_id.id_for_label}}">{% trans "Subordinates" %}</label>
                                {{feedback_form.subordinate_id}}
                                {{feedback_form.subordinate_id.errors}}
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.colleague_id.id_for_label}}">{% trans "Colleague" %}</label>
                                {{feedback_form.colleague_id}}
                                {{feedback_form.colleague_id.errors}}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-4">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.period.id_for_label}}">{% trans "Period" %}</label>
                                {{feedback_form.period}}
                                {{feedback_form.period.errors}}
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-4">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.start_date.id_for_label}}">{% trans "Start Date" %}</label>
                                {{feedback_form.start_date}}
                                {{feedback_form.start_date.errors}}
                            </div>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-4">
                        <div class="oh-input__group">
                            <div class="oh-input__group">
                                <label class="oh-input__label" for="{{feedback_form.end_date.id_for_label}}">{% trans "End Date" %}</label>
                                {{feedback_form.end_date}}
                                {{feedback_form.end_date.errors}}
                            </div>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <label class="oh-input__label" for="{{feedback_form.question_template_id.id_for_label}}">{% trans "Question Template" %}</label>
                            {{feedback_form.question_template_id}}
                            {{feedback_form.question_template_id.errors}}
                            <ul id="error-list" class="errorlist" style="display: none">
                                <li>{% trans "This field is required" %}.</li>
                            </ul>
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6">
                        <div class="oh-input__group">
                            <label class="oh-input__label" for="id_employee_key_results_id">{% trans "Key Result" %}</label>
                            <select name="employee_key_results_id" id="id_employee_key_results_id" class="oh-select  oh-select--lg oh-select-no-search w-100 oh-select-2--large" multiple="multiple" style="display: none;">
                                <option value="">------------------</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6 mb-5" id="">
                        <div class="oh-input__group">
                            <label class="oh-input__label" for="{{feedback_form.cyclic_feedback.id_for_label}}">{% trans "Is Cyclic Feedback" %}</label>
                            <div class="oh-switch">
                            {{feedback_form.cyclic_feedback}}
                            </div>
                            {{feedback_form.cyclic_feedback.errors}}
                        </div>
                    </div>
                    <div class="col-12 col-sm-12 col-md-6 col-lg-6" style="display:none" id="cyclic_feedback_period">
                        <div class="oh-input__group">
                            <label class="oh-input__label">{% trans "Cycle Period" %}</label>
                            <div class="w-100 d-flex">
                                {{feedback_form.cyclic_feedback_days_count}}
                                {{feedback_form.cyclic_feedback_period}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            </section>

            <div class="w-100 d-flex align-items-center justify-content-end">
                <button
                    type="submit"
                    class="oh-btn oh-btn--secondary oh-btn--w-100-resp"
                >
                    {% trans "Save" %}
                </button>
            </div>

        </form>
    </div>

    <!-- period modal -->
    <div class="oh-modal" id="PeriodModal" role="dialog" aria-labelledby="editKeyResultModal" aria-hidden="true">
        <div class="oh-modal__dialog">
          <!-- for creating period -->
          <div class="oh-modal__dialog-header">
            <button type="button" class="oh-modal__close" aria-label="Close">
              <ion-icon name="close-outline"></ion-icon>
            </button>
          </div>
          <div class="oh-modal__dialog-body" id="periodModalTarget">

            </div>
        </div>
      </div>
      <!-- end of period modal -->

</main>
<button id="colleguesButton" hx-get="{% url 'get-collegues' %}" hx-target="#id_colleague_id" hidden > </button>
<button id="managerButton" hx-get="{% url 'get-collegues' %}" hx-target="#id_manager_id" hidden > </button>
<button id="subordinatesButton" hx-get="{% url 'get-collegues' %}" hx-target="#id_subordinate_id" hidden > </button>
<button id="keyresultButtton" hx-get="{% url 'get-collegues' %}" hx-target="#id_employee_key_results_id" hidden > </button>

<script>
    $(document).ready(function(){
        get_collegues($('#id_employee_id'))
    });
    function get_collegues(element) {
        var employee_id = $(element).val();

        // Check if the employee_id is valid
        if (employee_id) {
            // Dynamically set the hx-vals attribute for the manager button
            $('#managerButton').attr('hx-vals', JSON.stringify({ employee_id: employee_id, data: 'manager' })).click();
            // Dynamically set the hx-vals attribute for the colleagues button
            $('#colleguesButton').attr('hx-vals', JSON.stringify({ employee_id: employee_id, data: 'colleagues' })).click();
            // Dynamically set the hx-vals attribute for the subordinates button
            $('#subordinatesButton').attr('hx-vals', JSON.stringify({ employee_id: employee_id, data: 'subordinates' })).click();
            // Dynamically set the hx-vals attribute for the keyresult button
            $('#keyresultButtton').attr('hx-vals', JSON.stringify({ employee_id: employee_id, data: 'keyresults' })).click();
        } else {
            console.error('Invalid employee_id');
        }
    }
</script>
<script src="{% static 'src/feedback/feedback_creation.js' %}"></script>
<script src="{% static 'src/period/period.js' %}"></script>


{% endblock%}
