{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2026 Teclib' and contributors.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/form/fields_macros.html.twig' as fields %}

{% set rand = rand|default(random()) %}
<div class="mb-3">
   <form id="{{ link_itemtype|lower ~ "_form" ~ rand }}" name="{{ link_itemtype|lower ~ "_form" ~ rand }}" method="post" action="{{ link_itemtype|itemtype_form_path }}">
      {{ form_label }}
      {% if generic_source ?? false %}
          {{ fields.hiddenField('itemtype', source_itemtype) }}
          {{ fields.hiddenField('items_id', source_items_id) }}
      {% else %}
          {{ fields.hiddenField(source_itemtype|itemtype_foreign_key, source_items_id) }}
      {% endif %}
      <div class="d-flex">
      {% if generic_target ?? false %}
          {{ fields.dropdownItemsFromItemtypes('items_id', '', {
              itemtypes: link_types,
              used: used ?? [],
              field_class: 'd-flex',
              width: 'auto',
              mb: '',
              no_label: true,
          }|merge(dropdown_options)) }}
      {% else %}
          {% set primary_dropdown_itemtype = dropdown_options['itemtype']|default(target_itemtype) %}
          {% if link_types is defined and link_types is not empty %}
            <div class="col-auto">
               {{ fields.dropdownArrayField('link', link_types|first, link_types, '', {
                  no_label: true,
                  field_class: ''
               }) }}
            </div>
         {% endif %}
         <div class="col-auto">
            {{ fields.dropdownField(primary_dropdown_itemtype, primary_dropdown_itemtype|itemtype_foreign_key, '', '', {
               no_label: true,
               field_class: '',
               rand: rand
            }|merge(dropdown_options)) }}
            {% if ajax_dropdown is defined %}
               {% do call('Ajax::updateItemOnSelectEvent', [
                  ajax_dropdown['toobserve'],
                  ajax_dropdown['toupdate']['id'],
                  ajax_dropdown['url'],
                  ajax_dropdown['params'],
               ]) %}
            {% endif %}
            {% if ajax_dropdown is defined %}
               <span id="{{ ajax_dropdown['toupdate']['id'] }}">
                  {{ fields.dropdownField(ajax_dropdown['toupdate']['itemtype'], (ajax_dropdown['toupdate']['itemtype'])|itemtype_foreign_key, '', '', {
                     no_label: true,
                     field_class: '',
                     rand: rand
                  }|merge(ajax_dropdown['toupdate']['params'])) }}
               </span>
            {% endif %}
         </div>
      {% endif %}
         <div class="col-auto">
            <button class="btn btn-primary ms-1" type="submit" name="add">
               <i class="{{ add_button_icon ?? 'ti ti-link' }}"></i>
               <span>{{ add_button_label ?? _x('button', 'Add') }}</span>
            </button>
         </div>
         {% if create_link ?? false %}
         <div class="col-auto ms-4">
               {% set target_form_path = (target_itemtype|itemtype_form_path ~ "?_" ~ source_itemtype|itemtype_foreign_key ~ "=" ~ source_items_id) %}
               {% set create_url = create_link['url'] is defined ? create_link['url'] : target_form_path %}
               <a href="{{ create_url }}" class="btn btn-primary">
                  <i class="ti ti-plus"></i>
                  <span>{{ button_label }}</span>
               </a>
         </div>
         {% endif %}
      </div>
      {{ fields.csrfField() }}
   </form>
</div>
