<%= form_tag({}, :data => {:cm_url => context_menu_deals_path}) do -%>
  <%= hidden_field_tag 'back_url', url_for(params.respond_to?(:to_unsafe_hash) ? params.to_unsafe_hash : params) %>
  <%= hidden_field_tag 'project_id', @project.id if @project %>
  <div class="autoscroll">
    <table class="contacts deals index">
    <tbody>
      <% previous_group = false %>
      <% @deals.each do |deal| %>
        <% if @query.grouped? && (group = @query.group_by_column.value(deal)) != previous_group %>
          <% reset_cycle %>
          <tr class="group open">
            <td colspan="<%= @query.columns.size + 2 %>">
              <span class="expander" onclick="toggleRowGroup(this);">&nbsp;</span>
              <%= group.blank? ? 'None' : column_content(@query.group_by_column, deal) %> <span class="count">(<%= @deal_count_by_group[group] %>)</span>
              <%= link_to_function("#{l(:button_collapse_all)}/#{l(:button_expand_all)}", "toggleAllRowGroups(this)", :class => 'toggle-all') %>
            </td>
          </tr>
          <% previous_group = group %>
        <% end %>

      <tr class="hascontextmenu <%= deal.status_id %>  <%= cycle('odd', 'even') %> ">
        <td class="checkbox">
          <%= check_box_tag "ids[]", deal.id, false, :onclick => "toggleContact(event, this);" %>
        </td>
        <td class="avatar">
            <%= link_to avatar_to(deal, :size => "32"), {:controller => 'deals', :action => 'show', :id => deal.id}, :id => "avatar" %>
        </td>
        <td class="name">
            <h1 class="deal_name"><%= link_to deal.name, :controller => 'deals', :action => 'show', :id => deal.id %></h1>
            <p>
              <%= link_to_source(deal.contact) if deal.contact %>
            </p>
        </td>

        <td class="info">
          <div class="deal-sum"><strong><%= deal.price_to_s %></strong>
            <%= content_tag(:span, " (#{deal.probability}%)" ) if deal.probability  %>
            <% if deal.status && deal.project.deal_statuses.any? %>
            <%= deal_status_tag(deal.status) %>
            <% end %>
          </div>
          <div class="description" >
            <%= h deal.category %><%= " (#{format_date(deal.due_date)})" if deal.due_date %>
          </div>
        </td>
      </tr>
      <% end %>
    </tbody>
    </table>

    <table class="contacts deals index total">
      <tbody>
      <tr class="total">
        <% if @deal_weighted_amount.map{|k, v| v.to_i > 0}.any? %>
        <th class="title"><%= l(:label_crm_expected_revenue) %>:</th>
        <th class="sum deals-sum">
          <%= prices_collection_by_currency(@deal_weighted_amount, :hide_zeros => true).join('<br/>').html_safe %>
        </th>
        <% end %>
        <th class="title"><%= "#{l(:label_total)} (#{@deals_count}):" %></th>
        <th class="sum deals-sum">
          <%= prices_collection_by_currency(@deal_amount, :hide_zeros => true).join('<br/>').html_safe %>
        </th>
      </tr>

      </tbody>
    </table>

  </div>

<% end %>
