table class="data" id="buyer_accounts"
  thead
    tr
      th class="select" = bulk_select_all
      th = sortable('org_name', 'Group/Org.')
      th Admin
      th = sortable('created_at', 'Signup Date')

      - if @account_plans.size > 1
        th = sortable('plans.name', 'Plan')
      - if can?(:see, :multiple_applications)
        th Apps

      th = sortable('state', 'State')

      th class="actions"
        - if can?(:create, Account)
          = link_to 'Create', new_admin_buyers_account_path,
                              title: 'Create new account',
                              class: 'action add'

    tr class="search"
      = search_form do |s|
        th colspan="2"
        th colspan="2"
          = s.text_field :query, :value => search_query_value,
                                 :size => 50, :class => :query, :placeholder => 'search for accounts, users, keys, etc.'
          = help_bubble 'query' do
            | You can search by following attributes: Group/Org., User login, First name, Last name, email, user_key, app_id or Application Name.

        - if @account_plans.size > 1
          th = s.collection_select :plan_id, @account_plans, :id, :name,
                                  { :include_blank => true },
                                  { :class => :account_plans }

        - if can? :see, :multiple_applications
          th


        th
          = s.select :state, account_states_for_select,
          { :include_blank => true, :selected => @search.state },
          { :class => :state }
        th
          = s.submit 'Search', :name => nil
          - if params[:search]
            = search_reset_button(s)

  tbody
    - accounts.each do |account|
      tr id=dom_id(account)
        td class="select" =  bulk_select_one account
        td = link_to (account.org_name), admin_buyers_account_path(account)
        td = account.admins.first.try(:display_name)
        td = account.created_at.strftime("%e %b, %Y")
        - if @account_plans.size > 1
          td class="plan" = h account.bought_account_plan.try(:name)

        - if can? :see, :multiple_applications
          td = link_to (account.bought_cinstances.count), admin_buyers_account_applications_path(account)

        td class="state" = account.state.capitalize
        td class="actions operations"
          ul.Actions
            - if account.first_admin.try!(:pending?)
              li.Actions-item = action_link_to 'activate', activate_admin_buyers_account_user_path(account, account.admins.first),
                                           :method => :post, :class => 'action'
            - elsif account.pending?
              li.Actions-item = action_link_to 'approve', approve_admin_buyers_account_path(account), :method => :post

            - elsif can? :impersonate, account
              li.Actions-item = link_to "Act as", admin_buyers_account_impersonation_path(account), :method => :post,
                :target => '_blank', :title => 'Impersonate 3scaleadmin user on this provider side',
                :class => 'action bolt'

    = no_search_results(can?(:see, :multiple_applications) ? 9 : 8) if accounts.empty?

= will_paginate accounts
= link_to_export_widget_for('Accounts')
