<h2>Service Subscriptions</h2>

<div id="bulk-operations" style="display: none;">
  <h3>Bulk operations</h3>
  <p>You have selected <span class="count"></span> service subscriptions and you can make following operations with them:</p>
  <dl>
    <%= bulk_action 'Send email', new_admin_buyers_service_contracts_bulk_send_email_path,
                    'Send email to selected subscribers' %>
    <%= bulk_action 'Change service plan', new_admin_buyers_service_contracts_bulk_change_plan_path,
                    'Transfer these subscriptions to different service plan' %>
    <%= bulk_action 'Change state', new_admin_buyers_service_contracts_bulk_change_state_path,
                    'Accept, suspend or resume selected subscriptions' %>
    <%#= bulk_action 'Delete', new_admin_buyers_service_contracts_bulk_delete_path,
                    'Delete selected applications' %>
  </dl>

  <% content_for :javascripts do %>
    <%= javascript_include_tag 'bulk_operations.js' %>
    <%= javascript_include_tag 'vendor/jquery.tipsy.js' %>
  <% end %>
</div>



<table class="data">
  <thead>
    <tr>
      <th class="select"><%= bulk_select_all %></th>
      <th><%= sortable('cinstances.state', 'State') %></th>
      <th><%= sortable('accounts.org_name', 'Account') %></th>
      <% if current_account.multiservice? %>
        <th>
          <label for="search_service_id">Service</label>
        </th>
      <% end %>
      <th><%= sortable('plans.name', 'Plan') %></th>
      <th>
          Paid?
          <%= help_bubble 'free_or_paid' do %>
            'Free' subscriptions are those, whose service plan has no billing rules associated. 'Paid' are all the others.
          <% end %>
      </th>
      <th><%= sortable('cinstances.created_at', 'Created On') %></th>
    </tr>

    <tr class="search">
      <%= form_for @search, :as => :search, :url => search_path, :html => { :method => :get, :class => :search } do |s| %>
        <th></th>
        <th><%= s.select :state, @states, :include_blank => true, :selected => @search.state %></th>
        <th><%= s.text_field :account_query, :size => 15, :disabled => @account.present?, :value => @account.try(:org_name) %></th>

        <% if current_account.multiservice? %>
          <th><%= s.collection_select :service_id, @services, :id, :name,
                                      { :include_blank => true },
                                      { :disabled => @service } -%>
          </th>
        <% end %>

        <th><%= s.grouped_collection_select :plan_id, @services,
                                            :service_plans, :name, :id, :name,
                                            { :include_blank => true },
                                            { :disabled => @plan } -%>
        </th>
        <th><%= s.select :plan_type, [ ['free'], ['paid'] ],
                         {:include_blank => true},
                         { :disabled => @plan } %>
        </th>
        <th>
          <%= s.submit 'Search', :name => nil %> <%= s.submit 'Reset', :name => :search, :id => :search_reset %>
        </th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% @service_contracts.each do |contract| %>
      <tr id="<%= dom_id(contract) %>" >
        <td class="select"><%=  bulk_select_one contract %></td>
        <td class="state"><%= contract.state %></td>
        <td><%= link_to_buyer_or_deleted contract.account %></td>
        <% if current_account.multiservice? %>
          <td><%= contract.service.name %></td>
        <% end %>
        <td class="plan"><%= contract.plan.name %></td>
        <td class="paid_or_free"><%= plan_free_or_paid contract.plan %></td>
        <td><%= time_tag_with_title contract.created_at %></td>
      </tr>
    <%-end-%>
    <%= no_search_results(current_account.multiservice? ? 7 : 6) if @service_contracts.empty? %>
  </tbody>
</table>
<%= will_paginate @service_contracts %>
