<%
  content_name = f.object.class.content_name

  field_id = "#{content_name}_#{field[:id]}"
  value ||= if f.object.respond_to?(field[:id].to_sym)
    value = f.object.send(field[:id].to_sym)
  else
    value = field[:value]
  end
%>
<div class="content-field">
  <div class="input-field">
    <% unless defined?(show_label) && !show_label %>
      <%= f.label field do %>
        <%= field[:label].present? ? field[:label] : ' ' %>
        <% if defined?(autocomplete) && autocomplete %>
          <i class="material-icons grey-text lighten-2 tooltipped"
             style="font-size: 100%"
             data-tooltip="This field may suggest some ideas for you when you start typing."
             data-position="right"
          >
            offline_bolt
          </i>
        <% end %>
      <% end %>
    <% end %>

    <%
      placeholder = I18n.translate "attributes.#{content_name}.#{field[:label].downcase.gsub(/\s/, '_')}",
        scope: :serendipitous_questions,
        name: f.object.send('name') || "this #{content_name}",
        default: 'Write as little or as much as you want!'
    %>

    <%= hidden_field_tag "#{content_name}[custom_attribute_values][][name]", field[:id] %>
    <%=
      text_area_tag "#{content_name}[custom_attribute_values][][value]",
                    value,
                    class: "js-can-mention-pages materialize-textarea #{defined?(autocomplete) && autocomplete ? ('autocomplete ' + 'js-autocomplete-' + field[:id].to_s) : ''}",
                    placeholder: placeholder
    %>

    <p class="grey-text show-when-focused">
      Tip: You can press @ to insert a link to your other pages. 
    </p>
  </div>
</div>

<% if defined?(autocomplete) && autocomplete %>
  <%= content_for :javascript do %>
    $(function() {
      $('.js-autocomplete-<%= field[:id].to_s %>').autocomplete({
        limit: 5,
        data: {
          <% autocomplete.each do |autocomplete_option| %>
            "<%= autocomplete_option %>": null,
          <% end %>
        }
      });
    });
  <% end %>
<% end %>
