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


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

  <%= f.hidden_field :school_calendar_id %>
  <%= f.hidden_field :teacher_id %>

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

      <div class="col col-sm-4">
        <%= f.association :classroom, as: :select2_classroom, user: current_user, record: f.object %>
      </div>

      <div data-discipline>
        <div class="col col-sm-4">
          <%= f.association :discipline, as: :select2_discipline,
                            user: current_user,
                            classroom_id: @observation_diary_record.classroom_id,
                            admin_or_employee: @allow_discipline_edit,
                            required: true,
                            input_html: { value: f.object.discipline_id,
                                          data: { without_json_parser: true } } %>
        </div>
      </div>
    </div>

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

  <fieldset>
    <legend><%= ObservationDiaryRecord.human_attribute_name :notes %></legend>

    <div>
      <% if f.object.errors[:notes].any? %>
        <div class="alert alert-danger fade in">
          <i class="fa-fw fa fa-times"></i>

          <% f.object.errors.full_messages_for(:notes).each do |error| %>
            <%= error %><br/>
          <% end %>
        </div>
      <% end %>
    </div>

    <table class="table table-striped table-bordered table-condensed records">
      <thead>
        <tr>
          <th scope="col" style="width: 45%"><%= ObservationDiaryRecordNote.human_attribute_name :students %></th>
          <th scope="col" style="width: 46%"><%= ObservationDiaryRecordNote.human_attribute_name :description %></th>
          <th scope="col"></th>
        </tr>
      </thead>

      <tbody id="observation-diary-record-notes">
        <tr>
          <td class="no_item_found" colspan="3" style="<%= f.object.notes.reject(&:marked_for_destruction?).empty? ? 'display: table-cell;' : 'display: none;' %>" ><%= t('.no_item_found') %></td>
        </tr>

        <%= f.association :notes do |note_form| %>
          <%= render 'note_fields', f: note_form %>
        <% end %>
      </tbody>

      <tfoot class="links">
        <tr>
          <td colspan="3">
            <%= link_to_add_association(
                  t('.add_note'),
                  f,
                  :notes,
                  'data-association-insertion-method': 'append',
                  'data-association-insertion-node': '#observation-diary-record-notes',
                  class: 'btn btn-success btn-sm'
                ) %>
          </td>
        </tr>
      </tfoot>
    </table>
  </fieldset>

  <fieldset>
    <div class="row">
      <div class="col col-sm-12">
        <legend>Anexos</legend>
        <div class="alert alert-info fade in">
          <i class="fa-fw fa fa-info"></i>
          Somente arquivos nos formatos jpeg, jpg, png, gif, pdf, odt, doc, docx, ods, xls, xlsx, odp, ppt, pptx, odg, xml, csv. Tamanho máximo por arquivo: 3MB
        </div>
        <br>
        <% if f.object.errors[:observation_diary_record_attachments].present? %>
          <div class="alert alert-danger alert-block ">
            <i class="fa-fw fa fa-times"></i>
            <span class="help-inline">
              <%= f.object.errors[:observation_diary_record_attachments].join "<br />" %>
            </span>
          </div>
        <% end %>
        <table class="table table-striped table-bordered table-condensed records">
          <thead>
            <tr>
              <th scope="col">Anexo</th>
              <% if action_name != 'show' %>
                <th scope="col" style="width: 73px"></th>
              <% end %>
            </tr>
          </thead>

          <tbody id="observation-diary-record-attachments">
            <% if action_name == 'show' && f.object.observation_diary_record_attachments.blank? %>
              <tr class="nested-fields">
                <td><span>Nenhum documento anexado</span></td>
              </tr>
            <% else %>
              <%= f.simple_fields_for :observation_diary_record_attachments, f.object.observation_diary_record_attachments do |attachment| %>
                <% if attachment.object.persisted? %>
                  <%= render 'observation_diary_record_attachment_show', f: attachment %>
                <% else %>
                  <%= render 'observation_diary_record_attachment_fields', f: attachment %>
                <% end %>
              <% end %>
            <% end %>
          </tbody>
          <% if action_name != 'show'%>
            <tfoot class="links">
              <tr>
                <td colspan="2">
                  <%= link_to_add_association t('.add_attachment'), f, :observation_diary_record_attachments,
                    class: 'btn btn-success btn-sm',
                    :"data-association-insertion-method" => "append",
                    :"data-association-insertion-node" => "#observation-diary-record-attachments" %>
                </td>
              </tr>
            </tfoot>
          <% end %>
        </table>
      </div>
    </div>
  </fieldset>
  <footer>
    <%= link_to t('views.form.back'), observation_diary_records_path, class: 'btn btn-default' %>

    <% if f.object.persisted? %>
      <%= link_to(
            t('views.form.history'),
            history_observation_diary_record_path(f.object),
            class: 'btn btn-info'
          ) %>
    <% end %>

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