# encoding: utf-8
module OrganizationsHelper
  include ApplicationHelper
  include FilesHelper


  def find_user_not_in_current_org_by_name org
      if params[:q] && params[:q].lstrip.rstrip != ""
        scope = Principal.active.sorted.not_member_of_org(org).like(params[:q])
      else
        scope = []
      end
      principals = paginateHelper scope,10
      s = content_tag('ul', project_member_check_box_tags_ex('membership[user_ids][]', principals), :class => 'mb5', :id => 'principals')
      links = pagination_links_full(@obj_pages, @obj_count, :per_page_links => false, :remote => false, :flag => true){|text, parameters, options|
        link_to text, org_member_autocomplete_org_member_index_path( parameters.merge(:q => params[:q],:flag => true,:org=>org, :format => 'js')), :remote => true
      }
      s + content_tag('ul', links,:class => 'wlist',:style=>'float:left !important', :id => "org_member_pagination_links" )
  end

  def get_default_name field
    case field.name
      when 'activity' then
        return '动态'
      when 'course' then
        return '课程'
      when 'project' then
        return '项目'
    end
  end

  def get_message_org(org_id, field_id)
    OrgDocumentComment.find_by_sql("SELECT * FROM org_document_comments where organization_id = #{org_id} and org_subfield_id = #{field_id} and parent_id is null order by updated_at desc limit 8;")
  end

  def get_message_reply_org(org_id, ids)
    # OrgDocumentComment.find_by_sql("SELECT * FROM org_document_comments where organization_id = #{org_id} and parent_id is not null order by updated_at desc limit 1;")
    OrgDocumentComment.find_by_sql("SELECT * FROM org_document_comments where organization_id = #{org_id} and parent_id in (#{ids}) order by updated_at desc limit 1;")
  end

  def get_attach_org(field_id)
    Attachment.find_by_sql("SELECT * FROM `attachments` where container_id =#{field_id} and container_type = 'OrgSubfield' and is_public =1 order by created_on limit 6;")
  end

  def get_attach_org2(field)
    org_attachments = field.attachments
    attachments = User.current.admin? ? org_attachments : visable_attachemnts(org_attachments)
    return attachments.sort_by{|x| x.created_on}.reverse.first(6)
    # Attachment.find_by_sql("SELECT * FROM `attachments` where container_id =#{field_id} and container_type = 'OrgSubfield' and is_public =1 order by created_on limit 6;")
  end

  # 从内容中获取路径
  def get_image_path_from_content content
    r = Regexp.new(/src="\/files\/uploads\/image(.+?)"/)
    if r.match(content).nil?
      image_path = nil
    else
      image_path = r.match(content)[1]
    end
  end

  #排列下拉框
  def subfield_status_option
    type = []
    option1 = []
    option1 << "左一"
    option1 << "1"
    type << option1
    # option2 = []
    # option2 << "中一"
    # option2 << "2"
    # type << option2
    option3 = []
    option3 << "左二"
    option3 << "3"
    type << option3
    option4 = []
    option4 << "左三"
    option4 << "4"
    type << option4
    option5 = []
    option5 << "左四"
    option5 << "5"
    type << option5
    option6 = []
    option6 << "右一"
    option6 << "6"
    type << option6
    option7 = []
    option7 << "右二"
    option7 << "7"
    type << option7
    option8 = []
    option8 << "右三"
    option8 << "8"
    type << option8
    type
  end

  # 课程和项目不参与某些模块的排列
  def subfield_status_option_default
    type = []
    option1 = []
    option1 << "左一"
    option1 << "1"
    type << option1
    option3 = []
    option3 << "左二"
    option3 << "3"
    type << option3
    option4 = []
    option4 << "左三"
    option4 << "4"
    type << option4
    option6 = []
    option6 << "右一"
    option6 << "6"
    type << option6
    type
  end

  def subfield_list_type subfield
    case subfield.to_i
      when 1
        resulet = "左一"
      when 2
        resulet = "左二"
      when 3
        resulet = "左二"
      when 4
        resulet = "左三"
      when 5
        resulet = "左四"
      when 6
        resulet = "右一"
      when 7
        resulet = "右二"
      when 8
        resulet = "右三"
    end
  end


  def get_subfield_acts field
    org_subfield = OrgSubfield.find(field.id)
    org_subfield_ids = org_subfield.org_document_comments.map(&:id) << 0
    org_acts = OrgActivity.where("(org_act_type='OrgDocumentComment'and org_act_id in (#{org_subfield_ids.join(",")})) || (container_type='OrgSubfield' and container_id=#{org_subfield.id})").order('updated_at desc')
    org_acts
  end

  def org_subfield_had_created?(org, type)
    sub_field = org.org_subfields.select{|subfield| subfield.field_type == type}
    result = sub_field.length > 0 ? true : false
    result
  end

  def org_subfield_type(field)
    case field.field_type
      when "Post"
        result = "帖子"
      when "Resource"
        result = "资源"
      when "Compstu"
        result = "学生"
      when "Comptec"
        result = "教师"
      when "Complex"
        result = "综合"
    end
  end

  def org_teacher_resource_count user
    results = Attachment.find_by_sql("SELECT * FROM attachments where author_id = #{user.id};").count
  end

  def org_teacher_course_count user
    results = Course.find_by_sql("select * from courses where courses.tea_id = #{user.id}").count
  end

  def org_student_course_count user
    course_ids = user.courses.map { |c| c.is_delete == 0 && c.id}
    results = Member.find_by_sql("select id from courses where courses.tea_id = #{user.id}").count
  end

  def excellent_teachers
    User.find_by_sql("select u.*, ue.technical_title, ue.school_id,(select count(*) from courses where courses.tea_id = u.id) as course_count
                                   from users u, user_extensions ue where u.id = ue.user_id and ue.identity=0 and u.excellent_teacher =1 order by course_count desc").first(5)
  end

end
