<% content_for :js do %>
  <%= javascript_include_tag 'list-contents' %>
  <%= javascript_include_tag 'views/discipline_content_records/form' %>
<% end %>

<%= simple_form_for @discipline_content_record, html: { class: 'smart-form' } do |f| %>
  <%= f.error_notification %>

  <%= f.simple_fields_for :content_record, @discipline_content_record.content_record.localized do |content_record| %>
    <%= content_record.hidden_field :id %>

    <fieldset>
      <div class="row">
        <div class="col col-sm-4">
          <%= content_record.input :unity_id, as: :select2_unity, user: current_user %>
        </div>


        <div class="col col-sm-4">
          <% if current_user.current_role_is_admin_or_employee? %>
            <%= content_record.association :classroom, as: :select2_classroom, user: current_user,
                record: f.object %>
          <% else %>
            <%= content_record.association :classroom, as: :select2, elements: @classrooms,
                user: current_user, record: f.object, readonly: ['show', 'edit'].include?(action_name) %>
          <% end %>
        </div>

        <div class="col col-sm-4">
          <% if current_user.current_role_is_admin_or_employee? %>
            <%= f.association :discipline, as: :select2_discipline, user: current_user, record: f.object,
                classroom_id: @discipline_content_record.content_record.classroom_id %>
          <% else %>
            <%= f.association :discipline, as: :select2, elements: @disciplines, user: current_user,
                  record: f.object, classroom_id: @discipline_content_record.content_record.classroom_id,
                  readonly: ['show', 'edit'].include?(action_name)  %>
          <% end %>
        </div>
      </div>

      <div class="row">
        <div class="col col-sm-4">
          <%= content_record.input :record_date, as: :date %>
        </div>

        <% if @allow_class_number %>
          <div class="col col-sm-4">
            <% show_classes = action_name == ('new' || 'create') ? false : true %>
            <%= f.input :class_number, as: :select2, multiple: true,
                                     elements: number_of_classes_elements(@number_of_classes),
                                     placeholder: 'Selecione uma ou mais aulas',
                                     disabled: show_classes %>
          </div>
        <% end %>
      </div>

      <div class="row">
        <div class="col col-sm-12">
          <%= content_record.input :daily_activities_record, as: :text,
                 required: f.object.decorator.daily_activities_required? %>
        </div>
      </div>

    </fieldset>
    <fieldset>
      <legend><%= @discipline_content_record.class.human_attribute_name(:contents) %></legend>
      <% if content_record.object.errors[:contents].any? %>
        <div class="alert alert-danger fade in">
          <i class="fa-fw fa fa-times"></i>
          <%= content_record.object.errors[:contents].first %>
        </div>
      <% end %>
        <div class="well" style="max-height: 300px;overflow-y: auto;">
          <ul class="list-group checked-list-box" id="contents-list">

            <% contents.each do |content| %>
              <li class="list-group-item manual" style="display: flex;" id="content_record_<%= content.id %>">
                <%= check_box_tag("discipline_content_record[content_record_attributes][content_ids][]",
                    content.id, content.id.in?(@discipline_content_record.contents.collect(&:id)),
                    hidden: true, 'data-content_description': content.to_s ) %>
                <span style="margin-left: 5px;display: block;width: calc(100% - 85px);"><%= content %></span>
                <% if content.is_editable %>
                  <div style="width: 80px;">
                    <a class="btn btn-success" onclick="editContent('content_record_<%= content.id %>')">
                      <i class="fa fa-pencil"></i>
                    </a>
                    <a class="btn btn-danger" onclick="removeContent('content_record_<%= content.id %>')">
                      <i class="fa fa-trash"></i>
                    </a>
                  </div>
                <% end %>
              </li>
            <% end %>
          </ul>
        </div>
        <div class="row textarea contents-select2-container">
          <div class="col col-sm-12">
            <% translation = Translator.t('activerecord.attributes.discipline_content_record.contents') %>
            <%= content_record.input :contents_tags,
                as: :string,
                label: "Adicionar #{translation}",
                input_html: {
                  class: 'select2-content-tags-ajax',
                  value: '',
                  data: {
                    url: contents_path,
                    data: content_record.object.contents_tags,
                    :'filter-by-description' => 1
                  }
                }
            %>
          </div>
        </div>
    </fieldset>

  <% end %>

  <footer>
    <%= link_to t('views.form.back'), discipline_content_records_path, class: 'btn btn-default' %>

    <% if @discipline_content_record.persisted? %>
      <%= link_to t('views.form.history'),
            history_discipline_content_record_path(@discipline_content_record),
            class: 'btn btn-info' %>
    <% end %>

    <%= f.submit t('views.form.save'), class: 'btn btn-primary', data: { disable_with: 'Salvando...'} %>
  </footer>
<% end %>
