ActiveAdmin.register JamRuby::AffiliatePartner, :as => 'Affiliates' do

  menu :label => 'Partners', :parent => 'Affiliates'

  config.sort_order = 'referral_user_count DESC'
  config.batch_actions = false
  # config.clear_action_items!
  config.filters = false
  config.per_page = 50
  config.paginate = true

  #form :partial => 'form'

  scope("Active", default: true) { |scope| scope.where('partner_user_id IS NOT NULL').order('referral_user_count desc') }
  scope("Unpaid") { |partner| partner.unpaid }

  form do |f|
    f.inputs 'Fields' do
      f.input(:partner_name, :input_html => { :maxlength => 128 })
      f.input(:partner_user, as: :searchable_select, hint: 'This person is the owner of the affiliate. Has access to reporting info in account section of www.jamkazam.com')
      f.input(:entity_type, :as => :select, :collection => AffiliatePartner::ENTITY_TYPES)
      f.input(:rate)
    end
    f.actions
  end

  index do

    # actions # use this for all view/edit/delete links

    column 'User' do |oo|
      link_to(oo.partner_user.name, admin_user_path(oo.partner_user.id), { :title => oo.partner_user.name })
    end
    column 'Name' do |oo|
      oo.partner_name
    end
    column 'Type' do |oo|
      oo.entity_type
    end
    column 'Code' do |oo|
      oo.id
    end
    column 'Referral Count' do |oo|
      oo.referral_user_count
    end
    column 'Earnings' do |oo|
      sprintf("$%.2f", oo.cumulative_earnings_in_dollars)
    end
    column 'Amount Owed' do |oo|
      sprintf("$%.2f", oo.due_amount_in_cents.to_f / 100.to_f)
    end
    column 'Pay Actions' do |oo|
      link_to('Mark Paid', mark_paid_admin_affiliate_path(oo.id), :confirm => "Mark this affiliate as PAID?") if oo.unpaid
    end

    actions
  end

  action_item :only => [:show] do
    link_to("Mark Paid",
            mark_paid_admin_affiliate_path(resource.id),
            :confirm => "Mark this affiliate as PAID?") if resource.unpaid
  end

  member_action :mark_paid, :method => :get do
    resource.mark_paid
    redirect_to admin_affiliate_path(resource.id)
  end

  controller do

  end
end
