<%= form_with(model: [:admin, node], 
              class: 'form-plain has--key-ctrls', 
              id: 'node_form', 
              data: { 
                locale: form_locale, 
                controller: "i18n-form" 
              }) do |form| %>

  <div class="form-header__titled">

    <%= form.label :title, class: "title-box", data: { icon: t("nodes.icons.#{@node.category}") } do %>    
      <%= render partial: 'material/text_field_i18n_simple', locals: { f: form, attr: :title } %>
    <% end %>

    <div class="i18n__from-ctrls">      
      <%- available_locales.each do |locale| %>
        <%= label_tag do %>
          <%= radio_button_tag 'form_locale', locale, form_locale.to_s == locale, data: {action: 'change->i18n-form#setFormLocale' } %>
          <%= tag.span locale.split('_').map{|v| tag.em v}.join.html_safe %>
        <% end %>          
      <% end %>
    </div>
  </div>


  <div class="form-section" id="node-attachments">    
    <div class="field">
      <div>
        <label><%= t(:'activerecord.attributes.node.attachments') %></label>
        
        <div class="button-container">
          <%= link_to t('ui.append_text'),                       
                      url_for(controller: 'attachments', action: 'new', node_id: node.id), 
                      class: 'btn',
                      data: { turbo_stream: true } %>
          <%= link_to t('ui.append_asset'),                       
                      url_for(controller: 'assets', action: 'index', node_id: node.id), 
                      class: 'btn',
                      data: { 
                        controller: 'utils',
                        action: 'click->utils#disableScroll',
                        turbo_stream: true 
                      } %>                    
        </div>
      </div>

      <div id="attachments" data-controller="sort">
        <%= form.fields_for :attachments do |builder| %>
          <%= render partial: 'admin/attachments/attachment', locals: {f: builder} %>
        <% end%>
      </div>
    </div>
  </div>


  <div class="form-section">

    <%= render partial: 'material/select_field', 
              locals: { 
                f: form, 
                attr: :parent_id, 
                choices: (@node.root? ? [] : node_structure_for_select(@node.root, @node)), 
                selected: @node.parent_id, 
                include_blank: false
              } unless form.object.root? %>      
    
    <%= render partial: 'material/select_field', 
                locals: { 
                  f: form, 
                  attr: :template, 
                  include_blank: false, 
                  choices: @node.available_templates.map { |k| [t(k, scope: :'nodes.templates'), k] }.sort 
                } %>
    
    <%= render  partial: 'material/tom_select_field', 
                locals: { 
                  f: form, 
                  attr: :settings, 
                  choices: Node::SETTINGS.map{ |tag| [ t(tag, scope: :'nodes.settings') , tag.to_s] }.sort,
                  multiple: true
                } %>
    
    <%= render partial: 'material/text_field_i18n', locals: { f: form, attr: :href } unless form.object.root? %>
    
    <%= render  partial: 'material/tom_select_field_i18n', 
                locals: { 
                  f: form, 
                  attr: :tags, 
                  choices: Node.tags,
                  multiple: true,
                  tags: true
                } unless form.object.root? %>    
    
  </div>

  <div class="form-section">
    <%= render partial: 'material/text_field_i18n', locals: { f: form, attr: :page_title } %>
    <%= render partial: 'material/text_field_i18n', locals: { f: form, attr: :page_description } %>
    
    <%= tag.div class: 'field' do %>
      <div>
        <%= form.label :slug, for: nil, class: 'i18n__label' %>
      </div>
      
      <div>
        <%- I18n.available_locales.each do |locale| %>
          <%-
            i18n_key =  locale.to_s.downcase.sub('-', '_')
            i18n_attr = "slug_#{i18n_key}" 
          -%>
          
          <%= form.label i18n_attr, class: "input-box input-box-url i18n__input i18n__input-#{i18n_key}" do %>    
            
            <%= tag.span File.join(form.object.parent&.url(locale: locale) || '', ''), class: 'base__url' %>
            
            <%= form.text_field i18n_attr,
                            class: 'material__input',
                            disabled: form.object.root?,
                            placeholder: form.object.public_send(:slug, locale: I18n.default_locale) %>
          <% end %>

          <%- form.object.errors.full_messages_for(i18n_attr).uniq.each do |msg| -%>
            <%= content_tag :p, msg, role: 'alert' %>
          <% end %>
        <% end %>  
      </div>  
    <%- end -%>
    
  </div>



  
  
  <div class="form-section">
    <div class="field">
      <div>
        <%= form.label :status, for: nil %>
      </div>
      <div>
        <ul class="node__status">    
          <%- Node.statuses.each do |status| -%>
          <li><%= form.radio_button :status, status[0], id: status[1] %> <%= label_tag status[1], t(status[0], scope: :'nodes.statuses'), class: 'plain' %></li>
          <% end %>
        </ul>
      </div>
    </div>

    <div class="field">
      <div><%= form.label :published_at %></div>
      <div>
        <div class="datetime__select">
          <%= form.datetime_select :published_at, 
                                   {datetime_separator: '<span>@</span>', 
                                   time_separator: '<span>:</span>',
                                   use_short_month: true}, {class: 'material__input material__input-select'} %>
        </div>        
        <%- form.object.errors.full_messages_for(:published_at).uniq.each do |msg| -%>
          <%= content_tag :p, msg, role: 'alert' %>
        <% end %>                
      </div>
    </div>
    
    <div class="field">
      <div><%= form.label :expires_at %></div>
      <div>
        <div data-controller="utils" class="datetime__select with-toggles">
          <%= form.datetime_select :expires_at, 
                                   {datetime_separator: '<span>@</span>', 
                                   time_separator: '<span>:</span>', 
                                   use_short_month: true, 
                                   default: (1.month.from_now.at_midnight-1.minute), 
                                   disabled: (@node.expires_at.blank?)}, { class: 'material__input material__input-select' } %>
          
          <%= button_tag 'add_alarm', type: 'button', data: { action: 'click->utils#toggleDisabled' } %>
          <%= button_tag 'remove_circle_outline', type: 'button', data: { action: 'click->utils#toggleDisabled' } %>                    
        </div>
      
      <%- form.object.errors.full_messages_for(:expires_at).uniq.each do |msg| -%>
        <%= content_tag :p, msg, role: 'alert' %>
      <% end %>
      </div>
    </div>
        
    <%= render  partial: 'material/tom_select_field', 
                locals: { 
                  f: form, 
                  attr: :excluded_locales, 
                  choices: options_for_select(I18n.available_locales.map{ |v| [t(v, scope: 'language_names'), v.to_s] }, form.object.excluded_locales),
                  multiple: true
                } %>    
    
    <%= render partial: 'material/check_box_icon', locals: { f: form, attr: :is_allowlist} %>            
    
  </div>
    
  
    
  <div class="form-ctrls">      
    <%= link_to t(:'ui.cancel'), 
                url_for(action: 'tree', id: @node.parent&.id), 
                data: { 
                  turbo_stream: true, 
                  turbo_action: 'advance'
                } %>  
    <%= form.submit t(:'ui.save') %>
  </div>

<% end %>