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 = true
  config.per_page = 100
  config.paginate = true

  #form :partial => 'form'


  #filter :partner_user
  filter :partner_name
  filter :id
  filter :current_quarter_in_cents
  filter :cumulative_earnings_in_cents
  filter :jamtracks_sold
  filter :subscriptions_count
  filter :referral_user_count

  scope("Sorted By Current Quarter",  default: true) { |scope| scope.where('partner_user_id IS NOT NULL').order('current_quarter_in_cents desc') }
  scope("Sorted By Jamtracks Sold",  default: false) { |scope| scope.where('partner_user_id IS NOT NULL').order('jamtracks_sold desc') }
  scope("Sorted By Subs",  default: false) { |scope| scope.where('partner_user_id IS NOT NULL').order('subscriptions_count desc') }
  scope("Sorted By Signups",  default: false) { |scope| scope.where('partner_user_id IS NOT NULL').order('referral_user_count desc') }
  scope("Sorted By Newest First") { |scope| scope.where('partner_user_id IS NOT NULL').order('id desc') }
  scope("Any") { |scope| scope.where('partner_user_id IS NOT NULL').order('referral_user_count desc') }
  scope("Unpaid") { |partner| partner.unpaid }

  controller do
    helper 'active_admin/subscription'
  end


  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)
      f.input(:paypal_id)
    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 'Signups' do |oo|
      oo.referral_user_count
    end
    column 'JamTracks' do |oo|
      oo.jamtracks_sold
    end
    column 'Subs' do |oo|
      oo.subscriptions_count
    end
    column 'Cum Earnings' do |oo|
      div do
          sprintf("Tot $%.2f", oo.cumulative_earnings_in_dollars)
        end
      div do
          sprintf("Jam $%.2f", oo.jamtrack_cumulative_earnings_in_dollars)
        end
      div do
          sprintf("Sub $%.2f", oo.subscriptions_cumulative_earnings_in_dollars)
        end
    end
    column 'Current Quarter' do |oo|
      div do
        sprintf("Tot $%.2f", oo.current_quarter_in_dollars)
      end
      div do
        sprintf("Jam $%.2f", oo.jamtrack_current_quarter_in_dollars)
      end
      div do
        sprintf("Sub $%.2f", oo.subscriptions_current_quarter_in_dollars)
      end
    end
    column 'Amount Owed' do |oo|
      div do
        sprintf("Tot $%.2f", oo.due_amount_in_cents.to_f / 100.to_f)
      end
      div do
        sprintf("Jam $%.2f", oo.jamtrack_due_amount_in_cents.to_f / 100.to_f)
      end
      div do
        sprintf("Sub $%.2f", oo.subscription_due_amount_in_cents.to_f / 100.to_f)
      end
    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

  show do |affiliate_partner|
  
    attributes_table do
      row :id
      row :partner_name
      row :entity_type
      row :rate
      row :address
      row :tax_identifier
      row :paypal_id
      row :venmo_user_id
      row :jamtracks_sold
      row :subscriptions_count
      row :cumulative_earnings_in_dollars
      row :jamtrack_cumulative_earnings_in_dollars
      row :subscriptions_cumulative_earnings_in_dollars
      row :current_quarter_in_dollars
      row :jamtrack_current_quarter_in_dollars
      row :subscriptions_current_quarter_in_dollars
    end
    
   
    render 'earnings', { affiliate_partner: affiliate_partner }
   
  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
