module ContestsHelper

  # 获取动态列表名称
  def get_acts_list_type type
    case type
      when "work"
        l(:label_work_acts)
      when "news"
        l(:label_news_acts)
      when "attachment"
        l(:label_attachment_acts)
      when "message"
        l(:label_message_acts)
      when "journalsForMessage"
        l(:label_journalsForMessage_acts)
      when "poll"
        l(:label_poll_acts)
      else
        l(:label_contest_cats)
    end
  end

  # 判断当前用户是否为竞赛管理员
  def is_contest_manager?(user_id, contest_id)
    @result = false
    mem = ContestMember.where("user_id = ? and contest_id = ?",user_id, contest_id)
    unless mem.blank?
      @result = mem.first.roles.to_s.include?("ContestManager") ? true : false
    end
    return @result
  end

  # 获取竞赛的管理人员
  def contest_managers contest
    contest.contest_members.select{|cm| cm.roles.to_s.include?("ContestManager")}
  end

  # 获取竞赛的评委人员
  def contest_judges contest
    contest.contest_members.select{|cm| cm.roles.to_s.include?("Judge")}
  end

  # 获取竞赛的参赛人员
  def contest_contestants contest
    contest.contest_members.select{|cm| cm.roles.to_s.include?("Contestant")}
  end

  def searchmember_by_name  members, name
    #searchPeopleByRoles(project, StudentRoles)
    mems = []
    if name != ""
      name = name.to_s.downcase
      members.each do |m|
        username = m.user[:lastname].to_s.downcase + m.user[:firstname].to_s.downcase
        if(m.user[:login].to_s.downcase.include?(name) ||  m.user.user_extensions[:student_id].to_s.downcase.include?(name) || username.include?(name))
          mems << m
        end
      end
    else
      mems = members
    end
    mems
  end

  def zh_contest_role role
    if role == "ContestManager"
      result = l(:label_CM)
    elsif role == "Judge"
      result = l(:label_judge)
    elsif role == "Contestant"
      result = l(:label_contestant)
    elsif role == "Manager"
      result = l(:field_admin)
    elsif role.include?("ContestManager") && role.include?("Judge")
      result = l(:label_CM) + "  " + l(:label_judge)
    end
    result
  end

  def search_member_by_name_login_school members, name
    mems = []
    if name != ""
      name = name.to_s.downcase
      school_ids = School.where("name like '%#{name}%'").map{|school| school.id}
      members.each do |m|
        username = m.user[:lastname].to_s.downcase + m.user[:firstname].to_s.downcase
        if(m.user[:login].to_s.downcase.include?(name) ||  school_ids.include?(m.user.user_extensions[:school_id]) || username.include?(name))
          mems << m
        end
      end
    else
      mems = members
    end
    mems
  end
end