<!-- _table.html.erb Start -->
<% if data.empty? %>
    Ihre Anfrage ergab keine Treffer
<% else %>

    <% content_for :head do %>
      <%= javascript_include_tag 'app_table' %>
    <% end %>

    <script type="text/javascript" language="javascript">
        function show_form_for(id, type) {
            window.location = '<%= controller.root_url %>' + type + '/edit/' + id
        }
    </script>
    <div id="control-form">
      <form name="pagination" id="pagination" action="<%= url_for {} %>">
        <% offset = (params['offset'].to_i > 0) ? params['offset'].to_i : 0 %>
        <% amount = @user.rows_per_page %>
        <%= hidden_field_tag 'offset', offset %>
        <div class="pager">
          <% if offset > 0 %>
              <a href="javascript:page_before();"><i class="fa fa-step-backward" aria-hidden="true"></i></a>
          <% end %>
          <span class="fw-600">Einträge: </span><span><%=offset+1%><span>bis</span><%=[offset+amount, data.rec_count.to_i].min%><span>von</span><%=data.rec_count.to_s%></span>
          <% if offset+amount < data.rec_count.to_i %>
              <a href="javascript:page_after();"><i class="fa fa-step-forward" aria-hidden="true"></i></a>
          <% end %>
        </div>
        <div>
          <span class="fw-600">Einträge pro Seite:</span> <%= text_field_tag 'amount', amount, :size => 3 %>
          <button type="submit" id="eintraegeproseite" style="vertical-align: middle">
            <i class="fa fa-check" aria-hidden="true"></i>
        </button>
      </div>
      </form>
      <% if @order.first.is_a? Sorting %>
        <div class="sorter">
          <span class="fw-600">Sortiert nach:</span>
          <span id="sorting_order">
            <% @order.each do |o| %>
              <span id="sorting_order_<%= o.priority %>" class="order_el">
                <span>
                  <%= link_to({:class => 'order_drag', :title => 'Sortierreihenfolge ändern'}) do%>
                      <i class="fa fa-arrows" aria-hidden="true"></i>
                  <% end %>
                </span>

                <%= o.human_name %>

                <%= link_to({:controller => @datensatz, :action => 'sorting_toggle', :id => o.id},{:title => 'Auf-/Abwärts sortieren'}) do %>
                  <% if o.desc %>
                    <i class="fa fa-caret-down" aria-hidden="true"></i>
                  <% else %>
                      <i class="fa fa-caret-up" aria-hidden="true"></i>
                  <% end %>
                <% end %>

                <span>
                  <%= link_to({:controller => @datensatz, :action => 'sorting_del', :id => o.id}, {:title => 'Nicht mehr nach diesem Feld sortieren' }) do %>
                  <i class="fa fa-times" aria-hidden="true"></i>
                  <% end %>
                </span>
              </span>
            <% end %>
          </span>
        </div>
      <% end %>
    </div>

    <script type="text/javascript" language="javascript">
	$(function() {
 		$('#sorting_order').sortable();
		$('#sorting_order').on('sortupdate', function(event, ui) {
			console.log("post "+$('#sorting_order').sortable().sortable('serialize'));
			$.ajax({
				method: 'POST',
				url: '<%= url_for :controller => @datensatz, :action => 'change_order' %>',
				dataType: 'html' ,
				data: $('#sorting_order').sortable().sortable('serialize')+"&authenticity_token="+encodeURIComponent('<%= form_authenticity_token %>'),
			}).done(function (msg) {
				window.location.reload();
			});
		});
	});
    </script>

    <% show_edit = (theUser(session).can_write_on?(data.first) && @datensatz != 'federal_state_info') %>

    <table style="white-space: nowrap" class='dtable row-highlight' id="data-table">
      <thead id="flt_head">
        <tr>
          <th>Bearbeiten</th>
          <% if show_edit %>
              <th>Löschen</th>
          <% end %>
          <% if @match %>
              <th></th>
          <% end %>
          <% data.first.columns.each do |col| %>
              <th title="<%= col['description'] %>">
                 <%= link_to(col['human_name_table'],
                        { :controller => @datensatz, :action => 'sorting_add', :datensatz => col['table_name'], :col_name => col['col_name'], :human_name => col['human_name'] },
                        { }) %>
              </th>
          <% end %>
        </tr>
      </thead>
      <tbody>
        <% data.each do |row| %>
            <% zeile %>
            <tr>
              <td class="zeile<%= $x %>">
                <%= link_to({:controller => @datensatz, :action => 'edit', :id => row['id']}, { :title => 'Bearbeiten' }) do %>
                  <i class="fa fa-pencil" aria-hidden="true"></i>
                <% end %>
                <!-- <%= link_to(image_tag('16-em-pencil.png', {:border => 0}), {:controller => @datensatz, :action => 'edit', :id => row['id']}, { :title => 'Bearbeiten' }) %> -->
              </td>
              <% if show_edit %>
                  <td class="zeile<%= $x %>">
                     <%= link_to({ controller: @datensatz, action: 'delete', id: row['id'], offset: @offset}, { title: 'Löschen', data: { confirm: "Sind Sie Sicher, dass Sie diesen Eintrag löschen möchten?" } }) do %>
                      <i class="fa fa-times" aria-hidden="true"></i>
                     <% end %>
                    <!-- <%= link_to(image_tag('16-em-cross.png', {:border => 0}), { controller: @datensatz, action: 'delete', id: row['id'], offset: @offset}, { title: 'Löschen', data: { confirm: "Sind Sie Sicher, dass Sie diesen Eintrag löschen möchten?" } }) %> -->
                  </td>
              <% end %>
              <% if @match %>
                  <td class="zeile<%= $x %>">
                    <%= link_to({:action => 'matching', :id => row['id'], :datensatz => @datensatz}, { :title => 'Abgleichen' }) do %>
                    <i class="fa fa-search" aria-hidden="true"></i>
                    <!-- <%= link_to(image_tag('16-lupe.png', {:border => 0}), {:action => 'matching', :id => row['id'], :datensatz => @datensatz}, { :title => 'Abgleichen' }) %> -->
                    <% end %>

                  </td>
              <% end %>

              <% row.columns.each do |col|
                errno=get_errno(col, row)
              %>
                  <td title="<%= error_text(errno) %>" class="zeile<%= $x %> <%= error_class(errno) %>">

                    <%= value_for(col, row).to_s.html_safe %>
                  </td>
              <% end %>
            </tr>
        <% end %>
      </tbody>
    </table>
<% end %>

<!-- table.html.erb Ende -->
