ActiveAdmin.register JamRuby::Teacher, :as => 'Teachers' do

  menu :label => 'Teachers', :parent => 'JamClass'

  config.sort_order = 'created_at desc'
  config.batch_actions = false
  config.per_page = 100
  config.paginate = true
  config.filters = true

  scope("All", default: true) { |scope| scope.order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, teachers.ready_for_session_at IS NULL DESC") }
  scope("All Sorted By Sign Up") { |scope| scope.order("teachers.created_at DESC, background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, teachers.ready_for_session_at IS NULL DESC") }
  scope("50% and Session Ready") { |scope| scope.where('profile_pct >= ?', 50.0).where('teachers.ready_for_session_at IS NOT NULL').order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, teachers.ready_for_session_at IS NULL DESC") }
  scope("50% and Not Session Ready") { |scope| scope.where('profile_pct > ?', 50.0).where('teachers.ready_for_session_at IS NULL').order("background_check_at > '#{(Date.today - 365).to_s}}' NULLS FIRST, teachers.ready_for_session_at IS NULL DESC") }

  controller do
    active_admin_config.includes.push :user
  end

  filter :teacher_full_name_or_user_email_cont, label: 'Name', as: :string
  filter :user_phantom, label: 'Phantom ', as: :boolean, selected: 'false'

         #filter :by_search_user_name, label: "Name", as: :string

  index do
    column "Name" do |teacher|
      span do
        link_to "#{teacher.user.name} (#{teacher.user.email})", "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.user.id}"
      end
    end
    column "Instruments & Genres" do |teacher|
      div do
        div do
          teacher.instruments.join(', ')
        end
        br
        div do
          teacher.genres.join(', ')
        end
      end
    end
    column "Location" do |teacher|
      teacher.user.location(country = true)
    end
    column "Profile %" do |teacher|
      div do
        span do
          "#{teacher.pct_complete[:pct]}%"
        end
        br
        span do
          link_to "Detail", admin_teacher_path(teacher.id)
        end
      end

    end

    column "Background Check" do |teacher|
      div do
        if teacher.background_check_at
          span do
            teacher.background_check_at.to_date
          end
          span do
            br
          end
          span do
            link_to(edit_admin_teacher_background_check_path(teacher.id)) do
              "update background check"
            end
          end

        else
          span do
            'NO'
          end
          span do
            br
          end
          span do
            link_to("update background check", edit_admin_teacher_background_check_path(teacher.id))
          end
        end
      end
    end

    column "Session Ready" do |teacher|
      div do
        if teacher.ready_for_session_at
          span do
            'YES'
          end
          span do
            br
          end
          span do
            link_to("mark not checked", mark_not_session_ready_admin_teacher_path(teacher.id), {confirm: "Mark not ready for session??"})
          end
        else
          span do
            'NO'
          end
          span do
            br
          end
          span do
            link_to("mark as checked", mark_session_ready_admin_teacher_path(teacher.id), {confirm: "Mark as ready for session?"})
          end

        end
      end
    end
    column "Top Teacher" do |teacher|
      div do
        if teacher.top_rated
          span do
            'YES'
          end
          span do
            br
          end
          span do
            link_to("mark not top", mark_not_top_admin_teacher_path(teacher.id), {confirm: "Mark as not top rated?"})
          end
        else
          span do
            'NO'
          end
          span do
            br
          end
          span do
            link_to("mark as top", mark_top_admin_teacher_path(teacher.id), {confirm: "Mark as top rated?"})
          end
        end
      end

    end
    column "Signed Up" do |teacher|
      teacher.created_at.to_date
    end
    column "School" do |teacher|
      if teacher.school
        teacher.school.name
      end
    end
  end

  show do
    attributes_table do
      row "Name" do |teacher|
        link_to teacher.user.name, "#{Rails.application.config.external_root_url}/client#/profile/teacher/#{teacher.user.id}"
      end
      row "Email" do |teacher|
        teacher.user.email
      end
      row "Location" do |teacher|
        teacher.user.location(country = true)
      end
      row "Profile %" do |teacher|
        div do
          span do
            "#{teacher.pct_complete[:pct]}%"
          end
          br
          br
          div do
            h5 do
              "Completed Sections"
            end
            teacher.pct_complete.each do |k, v|
              if k != :pct && v
                div do
                  k
                end
              end
            end
            br
            br
            h5 do
              "Uncompleted Sections"
            end
            teacher.pct_complete.each do |k, v|
              if k != :pct && !v
                div do
                  k
                end
              end
            end
          end
        end

      end
=begin
      row "Background Check" do |teacher|
        div do
          if teacher.background_check_at
            span do
              teacher.background_check_at.to_date
            end
            span do
              br
            end
            span do
              link_to(mark_background_check_admin_teacher_path(teacher.id), {confirm: "Mark as background checked?"}) do
                "mark as checked"
              end
            end

          else
            span do
              'NOT DONE'
            end
            span do
              br
            end
            span do
              link_to("mark as checked", mark_background_check_admin_teacher_path(teacher.id), {confirm: "Mark as background checked?"})
            end
          end


        end
      end
=end

      row "Session Ready" do |teacher|
        div do
          if teacher.ready_for_session_at
            span do
              'YES'
            end
            span do
              br
            end
            span do
              link_to("mark not checked", mark_not_session_ready_admin_teacher_path(teacher.id), {confirm: "Mark not ready for session??"})
            end
          else
            span do
              'NO'
            end
            span do
              br
            end
            span do
              link_to("mark as checked", mark_session_ready_admin_teacher_path(teacher.id), {confirm: "Mark as ready for session?"})
            end

          end
        end
      end
      row "Top Teacher" do |teacher|
        div do
          if teacher.top_rated
            span do
              'YES'
            end
            span do
              br
            end
            span do
              link_to("mark not top", mark_not_top_admin_teacher_path(teacher.id), {confirm: "Mark as not top rated?"})
            end
          else
            span do
              'NO'
            end
            span do
              br
            end
            span do
              link_to("mark as top", mark_top_admin_teacher_path(teacher.id), {confirm: "Mark as top rated?"})
            end
          end
        end

      end

      row "Signed Up" do |teacher|
        teacher.created_at.to_date
      end

      row "School" do |teacher|
        if teacher.school
          teacher.school.name
        end
      end

      row "Booking Prices" do |teacher|
        div do
          table = teacher.booking_price_table
          if table[:single].length == 0 and table[:monthly].length == 0
            'No Pricing'
          else
            if table[:single].length > 0
              div do
                span do
                  'SINGLE'
                end
                span do
                  br
                end
                table[:single].each do |single|
                  span do
                    "#{single[:minutes]} min = $#{'%.2f' % (single[:price]/100.0)}"
                  end
                  span do
                    br
                  end
                end
              end
            end
            if table[:monthly].length > 0
              div do
                span do
                  'MONTHLY'
                end
                span do
                  br
                end
                table[:monthly].each do |monthly|
                  span do
                    "#{monthly[:minutes]} min = $#{'%.2f' % (monthly[:price]/100.0)}"
                  end
                  span do
                    br
                  end
                end
              end
            end
          end

        end
      end
    end
  end


  member_action :mark_background_check, :method => :get do
    resource.mark_background_checked
    redirect_to :back
  end

  member_action :mark_session_ready, :method => :get do
    resource.mark_session_ready
    redirect_to :back
  end

  member_action :mark_not_session_ready, :method => :get do
    resource.mark_not_session_ready
    redirect_to :back
  end

  member_action :mark_top, :method => :get do
    resource.mark_top_rated
    redirect_to :back
  end

  member_action :mark_not_top, :method => :get do
    resource.mark_not_top_rated
    redirect_to :back
  end
end