require 'jam_ruby/recurly_client'
ActiveAdmin.register  JamRuby::JamTrackRight, :as => 'JamTrackRights' do

  menu :label => 'Purchased JamTracks', :parent => 'Purchases'

  config.sort_order = 'created_at DESC'
  config.batch_actions = false

  #form :partial => 'form'

  filter :user_id

  filter :user_id,
         :label => "USER ID", :required => false,
         :wrapper_html => { :style => "list-style: none" }


  filter :jam_track

  index do
    actions

    #column "Order" do |right|
      #link_to("Place", order_admin_jam_track_right_path(right)) + " | " +
    #  link_to("Refund", refund_admin_jam_track_right_path(right))
    #end

    column "Last Name" do |right|
      right.user.last_name      
    end
    column "First Name" do |right|
      right.user.first_name      
    end
    column "Jam Track" do |right|
      link_to(right.jam_track.name, admin_jam_track_path(right.jam_track))
      # right.jam_track
    end
    column "Plan Code" do |right|
      right.jam_track.plan_code
    end
    column "Redeemed" do |right|
      right.redeemed ? 'Y' : 'N'
    end

    
  end

  form do |f|
    f.inputs 'New Jam Track Right' do
      f.input :jam_track, :required=>true, collection: JamTrack.all, include_blank: false
      f.input :user, :required=>true, collection: User.all, include_blank: false
      f.input :can_download, :required => true, as: :boolean
    end
    f.actions
  end

=begin


  member_action :order, :method => :get do 
    right = JamTrackRight.where("id=?",params[:id]).first
    user = right.user
    jam_track = right.jam_track
    client = RecurlyClient.new    
    billing_info = {
      first_name: user.first_name,
      last_name: user.last_name,
      address1: 'Test Address 1',
      address2: 'Test Address 2',
      city: user.city,
      state: user.state,
      country: user.country,
      zip: '12345',
      number: '4111-1111-1111-1111',
      month: '08',
      year: '2017',
      verification_value: '111'
    }

    begin
      client.find_or_create_account(user, billing_info)      
      client.place_order(user, jam_track, nil, nil)
    rescue RecurlyClientError=>x
      redirect_to admin_jam_track_rights_path, notice: "Could not order #{jam_track} for #{user.to_s}: #{x.errors.inspect}"
    else
      redirect_to admin_jam_track_rights_path, notice: "Placed order of #{jam_track} for #{user.to_s}."      
    end
  end
    
  member_action :refund, :method => :get do
    right = JamTrackRight.where("id=?",params[:id]).first
    client = RecurlyClient.new    

    begin
      client.refund_user_subscription(right.user, right.jam_track)    
    rescue RecurlyClientError=>x
      redirect_to admin_jam_track_rights_path, notice: "Could not issue refund on #{right.jam_track} for #{right.user.to_s}: #{x.errors.inspect}"
    else
      redirect_to admin_jam_track_rights_path, notice: "Issued full refund on #{right.jam_track} for #{right.user.to_s}"      
    end
  end
=end
end