<%= stylesheet_link_tag "resources/frequency_in_batch" %>

<% content_for :js do %>
  <%= javascript_include_tag 'views/daily_frequencies_in_batchs/create_or_update_multiple' %>
<% end %>

<div class="widget-body no-padding">
  <table class="table table-bordered table-only-inner-bordered">
    <thead>
    <th><%= DailyFrequency.human_attribute_name :unity %></th>
    <th><%= DailyFrequency.human_attribute_name :classroom %></th>
    <th><%= DailyFrequency.human_attribute_name :discipline %></th>
    <th><%= DailyFrequency.human_attribute_name :frequency_date %></th>
    </thead>
    <tbody style="border-bottom: 1px solid #ccc;">
    <td><%= @classroom.unity.name %></td>
    <td><%= @classroom.description %></td>
    <td><%= @frequency_type == FrequencyTypes::BY_DISCIPLINE ? @discipline.description : 'Todas' %></td>
    <% start_date = params[:start_date] || params[:frequency_in_batch_form][:start_date] %>
    <% end_date = params[:end_date] || params[:frequency_in_batch_form][:end_date] %>
    <% start_date = format_date(start_date) %>
    <% end_date = format_date(end_date) %>
    <% if start_date == end_date %>
      <td><%= l(start_date, format: :default) %></td>
    <% else %>
      <td><%= l(start_date, format: :default) + ' a ' + l(end_date, format: :default) %></td>
    <% end %>
    </tbody>
    <div id="flash-message">
      <p class="alert alert-info" style="margin-bottom: 0;" id="substitution-calculation-type-info">
        <i class="fa-fw fa fa-info"></i>
        <%= t('activemodel.attributes.frequency_in_batch_form.days_limit') %>
      </p>
    </div>
  </table>

  <%= simple_form_for @frequency_form, url: create_or_update_multiple_daily_frequencies_in_batchs_path, method: :post,
                      html: { class: 'smart-form daily_frequency', id: 'frequency-batch-form' } do |f| %>

    <div class="frequency-legend">
      <strong>Legenda:</strong>

      <div class="frequency-legend-item">
        <label class="checkbox checkbox-frequency">
          <input type="checkbox" checked disabled>
          <i></i>
          <span class="hide">Presente</span>
        </label>
        Presente
      </div>

      <div class="frequency-legend-item">
        <label title="" class="checkbox checkbox-frequency partial-absence">
          <input type="checkbox" checked disabled>
          <i></i>
          <span class="hide">Falta parcial</span>
        </label>
        Falta parcial
      </div>

      <div class="frequency-legend-item">
        <label class="checkbox checkbox-frequency">
          <input type="checkbox" disabled>
          <i></i>
          <span class="hide">Ausente</span>
        </label>
        Ausente
      </div>

      <div class="frequency-legend-item">
        <label title="" class="checkbox checkbox-frequency justified">
          <input type="checkbox" disabled>
          <i></i>
          <span class="hide">Falta justificada</span>
        </label>
        Falta justificada
      </div>

      <a href="#" class="btn btn-primary frequency-legend-button no-confirm" data-toggle="modal" data-target="#absence-justification-modal">
        Inserir falta justificada por período
      </a>
    </div>

    <table class="table table-bordered table-only-inner-bordered table-striped table-hover">
      <thead>
      <th style="width: 20px; text-align: center;">Seq.</th>
      <th><%= DailyFrequencyStudent.human_attribute_name :student %></th>
      <% @daily_frequencies = [] %>
      <% if @general_configuration.type_of_teaching == true %>
        <th>Modalidade de ensino</th>
      <% end %>
      <% params[:dates].each do |date| %>
        <% date[:daily_frequencies].each do |daily_frequency| %>
          <% @daily_frequencies << daily_frequency %>
          <% if daily_frequency.new_record? %>
            <% @new_record = daily_frequency.new_record? %>
          <% end %>
        <% end %>
        <th style="max-width: 180px; text-align: center; vertical-align: top" class="<%= @frequency_type == FrequencyTypes::GENERAL ? '' : 'date-collapse' %>"
            data-count="<%= date[:lesson_numbers].nil? ? 0 : date[:lesson_numbers].count %>" >
          <%= date[:date].to_date.strftime("%d").to_s + '/' + date[:date].to_date.strftime("%m").to_s %>&nbsp
          <i id="icon-remove" class="fa fa-minus-square hidden" aria-hidden="true"></i>
          <i id="icon-add" class="fa fa-plus-square <%= @frequency_type == FrequencyTypes::GENERAL ||
                                                        date[:lesson_numbers].count < 2 ? 'hidden' : '' %>"
             aria-hidden="true"></i>
        </th>
      <% end %>
      <th style="text-align: center; vertical-align: middle;" title="Total de faltas no período"><%= 'TF' %></th>
      </thead>
      <tbody>
      <input type="hidden" name="unity_id" value="<%= @classroom.unity.id %>">

      <input type="hidden" name="classroom_id" value="<%= @classroom.id %>">

      <input type="hidden" name="discipline_id" value="<%= @frequency_type == FrequencyTypes::GENERAL ? nil : @discipline.id %>">

      <input type="hidden" name="frequency_type" value="<%= @frequency_type %>">

      <input type="hidden" name="period" value="<%= @period %>">

      <input type="hidden" name="start_date" value="<%= start_date %>">

      <input type="hidden" name="end_date" value="<%= end_date %>">

      <input type="hidden" value="<%= @new_record %>" id="new_record">

      <% @students.each_with_index do |student, index| %>
        <%= render 'student_fields', student: student, index: index, form: f %>
      <% end %>

      <% if @additional_data.any? %>
        <% dependence = false %>
        <% @additional_data.each do |additional_data| %>
          <% dependence = true if additional_data[:tooltip] == 'Dependência' %>
        <% end %>
        <% if dependence %>
          <tr>
            <td colspan="30">
              <span class="daily-note-student-legend">
                <%= t('daily_frequencies.edit_multiple.dependence_students_legend') %>
              </span>
            </td>
          </tr>
        <% end %>
      <% end %>

      <%= image_tag "beta.png", class: 'hidden', id: 'image-beta' %>
      </tbody>
    </table>
    <footer>
      <div class="pull-left">
        <%= link_to 'Voltar', new_daily_frequencies_in_batch_path, class: 'btn btn-default pull-left' %>
        <% daily_frequency_ids = @daily_frequencies.map(&:id).compact %>
        <% if daily_frequency_ids.present? && daily_frequency_ids.length == @daily_frequencies.map(&:id).length %>
          <%= link_to t('.destroy'), destroy_multiple_daily_frequencies_in_batchs_path(
            daily_frequencies_ids: daily_frequency_ids), method: :delete,
                      data: { confirm: t('.confirm') }, class: 'btn new-delete-style pull-left'
          %>
          <%= link_to t('views.form.history'), history_multiple_daily_frequencies_in_batchs_url(
            daily_frequencies_ids: daily_frequency_ids), class: 'btn btn-primary pull-left'
          %>
          <div class="btn-group pull-left dropup" style="margin-left: 5px;">
            <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
              <%= t('.print') %> <span class="caret"></span>
            </button>
            <ul class="dropdown-menu">
              <li>
                <%= link_to(t('.by_month'), attendance_record_report_path(
                  attendance_record_report_form: params_for_print_month(@daily_frequencies)), method: :post,
                            target: '_blank')
                %>
              </li>
              <li>
                <%= link_to(t('.by_step'), attendance_record_report_path(
                  attendance_record_report_form: params_for_print_step(@daily_frequencies)), method: :post,
                            target: '_blank')
                %>
              </li>
            </ul>
          </div>
        <% end %>
      </div>
      <div class="pull-right">
        <%= f.submit t('.save'), class: 'btn new-save-style', id: 'save-frequencies-btn', data: { disable_with: 'Salvando...' } %>
      </div>
      <div class="pull-right" style="margin-right: 10px;">
        <%= f.input :receive_email_confirmation, as: :boolean, label: false, inline_label: t('activerecord.attributes.daily_frequency.receive_email_confirmation') %>
      </div>
    </footer>
  <% end %>

  <%= render 'absence_justification' %>

</div>
