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

<%= simple_form_for @absence_justification, html: { class: "smart-form", id: 'absence_justification' } do |f| %>
  <%= f.error_notification %>
  <%= render 'base_errors', f: f %>

  <% readonly = @absence_justification.legacy || !['new', 'create'].include?(action_name) %>

  <% if @absence_justification.legacy %>
    <!-- #absence-justifications-refactor esta mensagem é informativa para justificativas anteriores a 2023  -->
    <div class="alert alert-warning alert-dismissable">
      Não é possível editar uma justificativa de falta anterior ao ano de 2023.
    </div>
  <% end %>

  <fieldset>
    <div class="row">
      <div class="col col-sm-4">
        <%= f.association :unity, as: :select2_unity, user: current_user %>
      </div>
      <div class="col col-sm-4">
        <% if current_user.current_role_is_admin_or_employee? %>
          <%= f.association :classroom, as: :select2_classroom, user: current_user, record: f.object %>
        <% else %>
          <%= f.association :classroom, as: :select2, elements: @classrooms,
                user: current_user, record: f.object, readonly: readonly
                %>
        <% end %>
      </div>
      <% if @absence_justification.legacy %>
      <!-- #absence-justifications-refactor esta mensagem é informativa para justificativas anteriores a 2023  -->
        <div data-discipline>
          <div class="col col-sm-4">
            <% if current_user.current_role_is_admin_or_employee? %>
              <%= f.association :disciplines, as: :select2_discipline,
                    multiple: true,
                    user: current_user,
                    classroom_id: @absence_justification.classroom_id,
                    admin_or_employee: current_user.current_role_is_admin_or_employee?,
                    required: true,
                    input_html: {
                      value: f.object.discipline_ids.join(','),
                      readonly: readonly,
                      data: { without_json_parser: true }
                    } %>
            <% else %>
              <%= f.association :disciplines, as: :select2, elements: @disciplines,
                    multiple: true,
                    user: current_user,
                    classroom_id: @absence_justification.classroom_id,
                    admin_or_employee: current_user.current_role_is_admin_or_employee?,
                    required: true,
                    input_html: {
                      value: f.object.discipline_ids.join(','),
                      readonly: readonly,
                      data: { without_json_parser: true }
                    }
                    %>
            <% end %>
          </div>
        </div>
      <% end %>

    </div>

    <div class="row">
      <div class="col col-sm-4">
        <%= f.input :absence_date, input_html: { readonly: readonly } %>
      </div>
      <div class="col col-sm-4">
        <%= f.input :absence_date_end, input_html: { readonly: readonly } %>
      </div>
      <% if @absence_justification.legacy %>
        <!-- #absence-justifications-refactor esta mensagem é informativa para justificativas anteriores a 2023  -->
        <div class="col col-sm-4" data-student>
          <%= f.association :students, as: :select2, elements: @students, multiple: true, required: true,
          input_html: { value: f.object.student_ids.join(','), readonly: readonly, data: { without_json_parser: true } } %>
        </div>
      <% end %>
    </div>

    <% unless @absence_justification.legacy %>
    <div class="row">
      <div class="col col-sm-4" id="period">
        <%= f.input :period, as: :select2, elements: Periods.to_select_specific_values(false, %w[1 2 3]).to_json,
                     readonly: @period != Periods::FULL %>
      </div>
      <div class="col col-sm-4" data-student>
        <%= f.association :students, as: :select2, elements: @students, multiple: true, required: true,
                          input_html: {
                            value: f.object.student_ids.join(','),
                            readonly: readonly,
                            data: { without_json_parser: true }
                          } %>
      </div>
      <% if @frequency_by_discipline %>
        <div class="col col-sm-4">
          <%= f.input :class_number, as: :select2, multiple: true,
                      hint: "O não preenchimento do campo 'Aula' vincula a justificativa de falta com todas as aulas do período selecionado.",
                      elements: number_of_classes_elements(@absence_justification.school_calendar.number_of_classes),

                      input_html: {
                        value: @absence_justification.class_number,
                        readonly: readonly,
                        data: { without_json_parser: true }
                      } %>
        </div>
      <% end %>
    </div>
    <% end %>

    <div class="row">
      <div class="col col-sm-8">
        <%= f.input :justification, input_html: { readonly: @absence_justification.legacy || action_name == 'show' }  %>
      </div>
    </div>

  <div class="row textarea">
    <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[:absence_justification_attachments].present? %>
        <div class="alert alert-danger alert-block ">
          <i class="fa-fw fa fa-times"></i>
          <span class="help-inline">
            <%= f.object.errors[:absence_justification_attachments].join "<br />" %>
          </span>
        </div>
      <% end %>
      <table class="table table-striped table-bordered table-condensed records">
        <thead>
          <tr>
            <th>Anexo</th>
            <th width="73px"></th>
          </tr>
        </thead>

        <tbody id="absence-justification-attachments">
          <%= f.simple_fields_for :absence_justification_attachments, f.object.absence_justification_attachments do |attachment| %>
            <% if attachment.object.persisted? %>
              <%= render 'absence_justification_attachment_show', f: attachment %>
            <% else %>
              <%= render 'absence_justification_attachment_fields', f: attachment %>
            <% end %>
          <% end %>
        </tbody>
        <tfoot class="links">
          <tr>
            <td colspan="2">
              <%= link_to_add_association t('.add_attachment'), f, :absence_justification_attachments,
                class: 'btn btn-success btn-sm',
                disabled: @absence_justification.legacy || action_name == 'show',
                :"data-association-insertion-method" => "append",
                :"data-association-insertion-node" => "#absence-justification-attachments" %>
            </td>
          </tr>
        </tfoot>
      </table>
    </div>
  </div>

  <% if action_name == 'show' %>
    <div class="row textarea">
      <div class="col col-sm-12">
        <legend>Faltas justificadas</legend>
        <table class="table table-striped table-bordered table-condensed records">
          <thead>
          <tr>
            <th>Aluno</th>
            <th>Dia</th>
            <th>Aula</th>
            <th>Disciplina</th>
          </tr>
          </thead>
          <tbody>
          <% if @absences_justified.empty? %>
            <tr class="nested-fields">
              <td colspan="4">Nenhuma falta foi justificada.</td>
            </tr>
          <% end %>
          <% @absences_justified.each do |absence_justified| %>
            <tr class="nested-fields">
              <td><%= absence_justified[:student_name] %></td>
              <td><%= absence_justified[:frequency_date] %></td>
              <td><%= absence_justified[:class_number] == 0 ? 'Todas' : absence_justified[:class_number] %></td>
              <td><%= absence_justified[:discipline_name] %></td>
            </tr>
          <% end %>
          </tbody>
        </table>
      </div>
    </div>
  <% end %>

  <%= hidden_field_tag :current_date, Time.now.strftime("%d/%m/%Y") %>
  <%= hidden_field_tag :teacher_id, current_teacher %>

  </fieldset>

  <footer>
    <%= link_to "Voltar", absence_justifications_path, class: "btn btn-default" %>

    <% if @absence_justification.persisted? %>
      <%= link_to 'Histórico', history_absence_justification_path(@absence_justification), class: 'btn btn-info' %>
    <% end %>

    <% unless @absence_justification.legacy || action_name == 'show' %>
      <%= f.submit 'Salvar', class: 'btn btn-primary' %>
    <% end %>

  </footer>
<% end %>
