#coding=utf-8
module CompetitionsHelper

  def user_json_data users
    users.map do |user|
      user_name = user.show_name
      user_path = user_path(user)
      user_id = user.user_extentions.student_id ? user.user_extentions.student_id : "--"
      user_school = user.school_name
      user.attributes.dup.except("hashed_password", "firstname", "lastname", "mail", "admin", "status", "last_login_on",
                                 "identity_url", "salt", "gid", "phone", "ID_number", "authentication").merge({
                                                                                                                  user_name: user_name,
                                                                                                                  user_path: user_path,
                                                                                                                  user_id: user_id,
                                                                                                                  user_school: user_school
                                                                                                              })
    end
  end

  def max_min_stage_time competition
    CompetitionStageSection.find_by_sql("SELECT MAX(end_time) as max_end_time, MIN(start_time) as min_start_time, competition_stage_id FROM competition_stage_sections WHERE competition_id = #{competition.id} GROUP BY competition_stage_id order by competition_stage_id")
  end

  def com_end_date time
    if time.present?
      if time.strftime("%H:%M:%S") == "00:00:00"
        (time - 1.day).strftime("%Y-%m-%d")
      else
        time.strftime("%Y-%m-%d")
      end
    else
      "--"
    end
  end

  def com_end_time time
    if time.present?
      if time.strftime("%H:%M:%S") == "00:00:00"
        (time - 1.day).strftime("%Y-%m-%d").to_s + " 24:00"
      else
        time.strftime("%Y-%m-%d %H:%M")
      end
    else
      "--"
    end
  end

  def chart_school_str members
    chart_school_name = ""
    school_ids = UserExtensions.where(:user_id => members.pluck(:user_id)).pluck(:school_id).uniq
    school_ids.each do |school_id|
      school = School.where(:id => school_id).first
      if school.present?
        chart_school_name = chart_school_name == "" ? school.name : (chart_school_name + "、" + school.name)
      end
    end
    chart_school_name
  end
end