{% extends 'django_glue/form/glue_field/base_field.html' %}

{% block field_content %}
    <input
            type="text"
            x-model="value"
            x-ref="glue_field"
            hidden
    > 
    <div
        class="position-relative"
        x-data="{
            show_dropdown: false,
            get choice_verbose() {
                if (!glue_field.choices.some(choice => choice[0] === this.value)) {
                    return '------------';
                } else {
                    return glue_field.choices.find(
                        choice => choice[0] === this.value
                    )?.[1];
                }
            },
        }"
    >
        <button
            class="form-control text-start d-flex justify-content-between"
            type="button"
            @click="show_dropdown = !show_dropdown"
        >
            <span x-text="choice_verbose"></span>
            <span class="d-flex align-items-center">
                {% include 'django_glue/form/glue_field/element/select_down_arrow_element.html' %}
            </span>

        </button>
        <div
            x-cloak
            x-show="show_dropdown"
            @click.outside="show_dropdown = false"
            class="shadow border rounded-2 border-secondary-subtle mt-2 position-absolute z-3 bg-white w-100 p-0 list-group"
            style="max-height: 300px; overflow-y: auto; z-index: 3;"
            @keydown.escape="show_dropdown = false"
            x-trap.inert="show_dropdown"
        >
            {% block select_dropdown_header %}
            {% endblock %}

            <template x-for="(choice, index) in glue_field.choices" :key="choice[0]">
                <div
                    class="glue-cursor-pointer py-1 d-flex align-items-center list-group-item px-0 bg-app-glue-layer-one-hover"
                    tabindex="0"
                    @click="value = choice[0]; show_dropdown = false"
                    @keydown.enter.prevent="value = choice[0]; show_dropdown = false"
                >
                    {% block choice_item %}
                        {% include 'django_glue/form/glue_field/item/select_choice_item.html' %}
                    {% endblock %}
            </template>

            {% block select_dropdown_footer %}
            {% endblock %}
        </div>
    </div>
{% endblock %}
