module PollsHelper

  #获取试卷的已答/未答人数
  def get_poll_answers(poll_users, status)
    if status == 1
      @poll_answers = 0
      @poll_unanswers = 0
    else
      @poll_answers = poll_users.commit_by_status(1).size   #表示已经提交了的用户
      course_all_members_count = poll_users.size
      @poll_unanswers = (course_all_members_count - @poll_answers)
    end
  end

  def poll_votes_count(votes,user_ids)
    votes.find_current_vote("user_id",user_ids.uniq).size
  end

  #公用tab页的相关信息
  def poll_common_header(is_teacher_or,poll)
    poll_url_status = []
    if is_teacher_or == 1  #当为老师的
      common_tabs = %w(1 2 3 4)
    else
      if poll.show_result #开启了公开统计
        common_tabs = %w(1 2)
      else
        common_tabs = %w(1)
      end
    end

    common_tabs.each do |c|
      if request.url.include?("polls_lists")
        active_status = 1
      elsif request.url.include?("commit_result")
        active_status = 1
      elsif request.url.include?("poll_setting")
        active_status = 1
      elsif request.url == poll_path(poll)
        active_status = 1
      else
        active_status = 0
      end
      common_tab = {
          :common_tab => c,
          :active_status => active_status
      }
      poll_url_status.push(common_tab)
    end
    poll_url_status
  end

  #poll的列表页的显示
  def poll_index_show(poll,course,is_teacher_or,user)
    # poll_all_users = poll.poll_users
    if course.is_end   #课堂停止后，试卷显示为已结束
      poll_status = 4
    elsif is_teacher_or == 1 || is_teacher_or == 0 || poll.unified_setting
      #当前为老师(非课堂成员、统一设置)的时候，显示的是老师身份的对应试卷的状态，因为该试卷，可能对应老师的多个分班
      poll_status = poll.polls_status
    else
      poll_status = poll.get_poll_status(user)  #当前用户查看的试卷的发布状态
    end

    if is_teacher_or == 1
      poll_users_list = poll.all_poll_users(user.id)  #当前老师所在班级的全部学生
      get_poll_answers(poll_users_list, poll_status)
      ex_pb_time = poll.get_poll_times(user.id,true)
      poll_publish_time = ex_pb_time[:publish_time]
      poll_end_time = ex_pb_time[:end_time]
      current_status = 3
      lock_icon = 0  #不显示锁图标
    elsif is_teacher_or == 2
      poll_users_list = poll.get_poll_exercise_users
      get_poll_answers(poll_users_list, poll_status)  # 未答和已答的
      # get_poll_answers(poll_all_users)
      ex_pb_time = poll.get_poll_times(user.id,false)
      poll_publish_time = ex_pb_time[:publish_time]
      poll_end_time = ex_pb_time[:end_time]
      current_status = poll.check_user_votes_status(user)
      lock_icon = 0  #不显示锁图标
    else
      poll_users_list = poll.get_poll_exercise_users
      get_poll_answers(poll_users_list, poll_status)  # 未答和已答的
      poll_publish_time = poll.publish_time
      poll_end_time = poll.end_time
      current_status = 4
      if poll.is_public
        lock_icon = 1  #非课堂成员，但是试卷为公开的，不加锁
      else
        lock_icon = 0  #显示锁图标
      end
    end
    if (course.is_public == 1) && poll.is_public
      lock_icon = 1
    end
    {
        "publish_time":poll_publish_time,
        "end_time":poll_end_time,
        "poll_answer":@poll_answers,
        "poll_unanswer":@poll_unanswers,
        "current_status":current_status,
        "lock_icon":lock_icon,
        "polls_status":poll_status
    }
  end

  #问卷列表页的用户信息显示
  def poll_user_info(poll_user,course_members,course)
    poll = poll_user.poll
    user = poll_user.user
    user_student_id = user.student_id

    if poll.un_anonymous  #是否开启实名认证
      poll_user_name = user.real_name
    else
      poll_user_name = user.nickname
    end
    course_member = course_members.find_by(user_id:user.id)
    course_group_id = course_member.present? ? course_member.course_group_id : nil
    if course_group_id == 0
      course_group_name = "未分班"
    else
      course_group = course.course_groups.find_by_id(course_group_id)
      course_group_id = course_group&.id
      course_group_name = course_group&.name
    end
    {
        "user_name":poll_user_name,
        "user_id": user.id,
        "student_id":user_student_id,
        "group_name":course_group_name,
        "group_id":course_group_id,
        "login":user.login
    }
  end

end
