<% content_for :js do %>
  <%= javascript_include_tag 'views/final_recovery_diary_records/form' %>
<% end %>

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

  <%= f.simple_fields_for :recovery_diary_record, @final_recovery_diary_record.recovery_diary_record.localized do |recovery_diary_record| %>
    <%= f.hidden_field :school_calendar_id %>
    <%= recovery_diary_record.hidden_field :id %>

    <fieldset>
      <div class="row">
        <div class="col col-sm-4">
          <%= recovery_diary_record.association :unity, as: :select2_unity, user: current_user %>
        </div>

        <div class="col col-sm-4">
          <% if current_user.current_role_is_admin_or_employee? %>
            <%= recovery_diary_record.association :classroom, as: :select2_classroom, user: current_user %>
          <% else %>
            <%= recovery_diary_record.association :classroom, as: :select2, elements: @classrooms, user: current_user %>
          <% end %>
        </div>

        <div class="col col-sm-4">
          <% if current_user.current_role_is_admin_or_employee? %>
            <%= recovery_diary_record.association :discipline, as: :select2_discipline, user: current_user,
              classroom_id: @final_recovery_diary_record.recovery_diary_record.classroom_id %>
          <% else %>
            <%= recovery_diary_record.association :discipline, as: :select2, elements: @disciplines, user: current_user,
              classroom_id: @final_recovery_diary_record.recovery_diary_record.classroom_id %>
          <% end %>
        </div>
      </div>

      <div class="row">
        <div class="col col-sm-4">
          <%= f.input :year, disabled: true %>
        </div>

        <div class="col col-sm-4">
          <%= recovery_diary_record.input :recorded_at, as: :date %>
        </div>
      </div>
    </fieldset>

    <fieldset>
      <legend><%= t('.students') %></legend>

      <% if recovery_diary_record.object.errors.added? :students, :at_least_one_assigned_student %>
        <div class="alert alert-danger fade in">
          <i class="fa-fw fa fa-times"></i>
          <%= recovery_diary_record.object.errors[:students].first %>
        </div>
      <% end %>

      <table class="table table-striped table-bordered table-condensed records">
        <thead>
          <tr>
            <th><%= RecoveryDiaryRecordStudent.human_attribute_name :student %></th>
            <th width="20%"><%= t('.needed_score') %></th>
            <th width="20%"><%= RecoveryDiaryRecordStudent.human_attribute_name :score %></th>
          </tr>
        </thead>

        <tbody id="recovery-diary-record-students" data-scale="<%= @number_of_decimal_places %>">
          <tr>
            <td class="no_item_found" colspan="3" style="<%= recovery_diary_record.object.students.reject(&:marked_for_destruction?).empty? ? 'display: table-cell;' : 'display: none;' %>" ><%= t('.no_item_found') %></td>
          </tr>

          <%= recovery_diary_record.association :students, collection: recovery_diary_record.object.students.sort_by { |student| student.student.name.upcase } do |student| %>
            <%= render 'student_fields', f: student %>
          <% end %>
        </tbody>
      </table>
    </fieldset>
  <% end %>

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

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

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