# encoding: utf-8
module ManagementsHelper

  def user_phone_addr phone
    addr = "--"
    unless phone.nil?
      result = MobInfo.lookup(phone[0,7])
      if result.present?
        addr = "#{result[:province]}-#{result[:city]}"
      end
    end
    return addr
  end

  def user_ip_addr ip
    addr = "--"
    unless ip.nil?
      result = SM.find_by_ip(ip)
      if result.present?
        addr = "#{result[:province]}-#{result[:city]}"
      end
    end
    return addr
  end

  def trial_authorization_status status
    case status
      when "0", nil
        0
      when "1"
        1
      when "2"
        2
      else
        [1,2]
    end
  end

  def mirror_status_show status
    case status
      when 0
        "<i class=\"fa fa-check-circle font-16 color-grey\" data-tip-right=\"未发布\"></i>".html_safe
      when 1
        "<i class=\"fa fa-check-circle font-16 color-green\" data-tip-right=\"已发布\"></i>".html_safe
      when 2, 3
        "<i class=\"fa fa-exclamation-circle color-red font-16\" data-tip-right=\"被修改\"></i>".html_safe
      when 4
        "<i class=\"fa fa-times-circle font-18 color-red mt5\" data-tip-right=\"被删除\"></i>".html_safe
      when 5
        "<i class=\"fa fa-exclamation-circle color-orange03 font-16\" data-tip-right=\"子节点异常\"></i>".html_safe
    end
  end

  def mirror_script_show mirror
    case mirror.main_type.to_i
      when 1
        "<a href=\"#{modify_script_users_path(:mirror_id => mirror.id)}\" target=\"_blank\"><i class=\"fa fa fa-file-text font-16 #{mirror.mirror_scripts.empty? ? "color-red" : "color-green"}\" aria-hidden=\"true\" data-tip-right=\"脚本模板\"></i></a>".html_safe
    end
  end

  def template_name type
    case type
      when "taskPass"
        "过关任务:"
      when "script"
        "通用模板:"
      when "courseGuide"
        "新课导语:"
      else
        ""
    end
  end

  def select_mirror_type type=nil
    case type
      when "1", nil, ""
        [["主类别", 1],["小类别", 0]]
      when "0"
        [["小类别", 0], ["主类别", 1]]
    end
  end

  def show_mirror_type type
    case type
      when "1"
        '<i class="fa fa-star color-green font-16" aria-hidden="true" data-tip-right="主类别"></i>'.html_safe
      when "0"
        '<i class="fa fa-star color-grey font-16" aria-hidden="true" data-tip-right="子类别"></i>'.html_safe
      when nil
        ""
    end
  end

  def show_mirror_tip type
    case type
      when "1"
        "主类别"
      when "0"
        "子类别"
      when nil
        "未分类"
    end
  end


  def query_user_status_num status
    user = (status == 0) ? user = User.all : User.where(:status => status)
    return user.count
  end

  def course_list_major_time id
    Major.where(:id => id).first.try(:name)
  end

  # params[:id]: course_list_id
  def get_course_list_major id
    Major.where(:id => MajorCourse.where(:course_list_id => id).map(&:major_id))
  end

  def course_list_count id
    Major.where(:id => id).first.try(:name)
  end

  def delete_mirror_http del_uri
    del_uri = URI.parse(URI.encode(del_uri.strip))
    del_data = {}
    del_http = Net::HTTP.new del_uri.host, del_uri.port
    del_req = Net::HTTP::Delete.new(del_uri.request_uri)
    del_req.form_data = del_data
    del_res = del_http.start { |http| http.request del_req }
  end

  def get_shixun_task id
    HomeworkCommon.where(:course_id => id,:homework_type => 4).order("IF(ISNULL(publish_time),1,0),publish_time asc,created_at asc")
  end

  def get_sub_repertoires_first id
    SubRepertoire.where(:repertoire_id => id).order("created_at desc")[0]
  end

  def get_sub_repertoires id
    SubRepertoire.where(:repertoire_id => id).order("created_at desc")
  end

  def get_mirror_repository id, sub_id
    if sub_id.present?
      MirrorRepository.where(:repertoire_id => id, :sub_repertoire_id => sub_id)
    else
      MirrorRepository.where(:repertoire_id => id)
    end
  end

  def sort_shixun index, shixuns, sx_order
    if index == '6'
      shixuns.each do |shixun|
        shixun[:usercount] = Myshixun.where(:shixun_id => shixun.id).count
      end
      sx_order == "desc" ?  shixuns.sort{|x,y| y[:usercount] <=> x[:usercount]} : @shixuns = @shixuns.sort{|x,y| x[:usercount] <=> y[:usercount]}
    else
      shixuns = shixuns.order("publish_time #{@sx_order}")
    end
  end
  # 实训作业列表的已提交作品数、有效作品数、通关数
  def shixun_task_work_count homework_common
    statistics_count = {}
    statistics_count[:commit_count] = 0
    statistics_count[:eff_count] = 0
    statistics_count[:tog_count] = 0
    student_works = homework_common.student_works.where("work_status !=?", 0)
    statistics_count[:commit_count] = student_works.size
    statistics_count[:eff_count] = StudentWork.find_by_sql("SELECT COUNT(*) size FROM (SELECT sw.`id` FROM student_works sw LEFT JOIN games g ON
                                 sw.`myshixun_id`=g.`myshixun_id` WHERE sw.`work_status`!= 0 AND sw.`is_delete`=0 AND g.`status` = 2 AND
                                 sw.`homework_common_id`=#{homework_common.id} GROUP BY sw.`id`) sg;").first.try(:size).to_i
    statistics_count[:tog_count] = StudentWork.find_by_sql("SELECT COUNT(*) size FROM (SELECT ms.`id` sw_id, COUNT(g.`id`) g_id FROM
                                 student_works sw, games g, myshixuns ms WHERE sw.`myshixun_id`=ms.`id` AND g.`myshixun_id`=ms.id AND
                                 sw.`homework_common_id`=#{homework_common.id} AND sw.`work_status`!= 0 AND sw.`is_delete`=0 AND g.`status` = 2 GROUP BY
                                 ms.`id`) sg WHERE sg.g_id = (SELECT COUNT(*) FROM challenges c, homework_commons_shixuns hcs WHERE
                                 c.`shixun_id`=hcs.`shixun_id` AND hcs.`homework_common_id`=#{homework_common.id});").first.try(:size).to_i

    statistics_count
  end
end