#enconding:utf-8
#  fq
module BidsHelper
  
  def render_notes(bid, journal, options={})
    content = ''
    removable = User.current == journal.user || User.current == bid.author
    links = []
    if !journal.notes.blank?
      links << link_to(image_tag('comment.png'),
                       {:controller => 'bids', :action => 'new', :id => bid, :journal_id => journal},
                       :remote => true,
                       :method => 'post',
                       :title => l(:button_quote)) if options[:reply_links]
      if removable
        url = {:controller => 'bids',
          :action => 'destroy',
          :object_id => journal,
          :id => bid}
        links << ' '
        links << link_to(image_tag('delete.png'), url,
                     :remote => true, :method => 'delete', :class => "delete", :title => l(:button_delete))
      end                                                     
    end
    content << content_tag('div', links.join(' ').html_safe, :class => 'contextual') unless links.empty?
    content << textilizable(journal.notes)
    css_classes = "wiki"
    content_tag('div', content.html_safe, :id => "journal-#{journal.id}-notes", :class => css_classes)
  end
  
  def link_to_in_place_notes_editor(text, field_id, url, options={})
    onclick = "$.ajax({url: '#{url_for(url)}', type: 'get'}); return false;"
    link_to text, '#', options.merge(:onclick => onclick)
  end
  
  # this method is used to get all projects that tagged one tag
  # added by william
  def get_bids_by_tag(tag_name)
    Bid.tagged_with(tag_name).order('updated_on desc')
  end
  
  #added by huang
  def sort_bid_enterprise(state, project_type)
    content = ''.html_safe
    case state
    when 0
      content << content_tag('li', link_to(l(:label_sort_by_active), calls_path(:bid_sort_type => '1', :project_type => project_type)))
      content << content_tag('li', link_to(l(:label_sort_by_time), calls_path(:bid_sort_type => '0', :project_type => project_type), :class=>"selected"), :class=>"selected")
      
    when 1
      content << content_tag('li', link_to(l(:label_sort_by_active), calls_path(:bid_sort_type => '1', :project_type => project_type), :class=>"selected"), :class=>"selected")
      content << content_tag('li', link_to(l(:label_sort_by_time), calls_path(:bid_sort_type => '0', :project_type => project_type)))
    end
    content = content_tag('ul', content)
    content_tag('div', content, :class => "tabs_enterprise")
  end
  #end
  
  def sort_bid(state, project_type)
    content = ''.html_safe
    case state
    when 0
      content << content_tag('li', link_to(l(:label_sort_by_active), calls_path(:bid_sort_type => '1', :project_type => project_type)))
      content << content_tag('li', link_to(l(:label_sort_by_time), calls_path(:bid_sort_type => '0', :project_type => project_type), :class=>"selected"), :class=>"selected")
      
    when 1
      content << content_tag('li', link_to(l(:label_sort_by_active), calls_path(:bid_sort_type => '1', :project_type => project_type), :class=>"selected"), :class=>"selected")
      content << content_tag('li', link_to(l(:label_sort_by_time), calls_path(:bid_sort_type => '0', :project_type => project_type)))
    end
    content = content_tag('ul', content)
    content_tag('div', content, :class => "tabs")
  end
  
  #huang
  def sort_contest(state)
    content = ''.html_safe
    case state
    when 0
      content << content_tag('li', link_to(l(:label_sort_by_active), contest_path(:contest_sort_type => '1')))
      content << content_tag('li', link_to(l(:label_sort_by_time), contest_path(:contest_sort_type => '0'), :class=>"selected"), :class=>"selected")
      
    when 1
      content << content_tag('li', link_to(l(:label_sort_by_active), contest_path(:contest_sort_type => '1'), :class=>"selected"), :class=>"selected")
      content << content_tag('li', link_to(l(:label_sort_by_time), contest_path(:contest_sort_type => '0')))
    end
    content = content_tag('ul', content)
    content_tag('div', content, :class => "tabs")
  end
  #end
  
  def course_options_for_select(courses)
    # <option value = '0'>#{l(:label_choose_reward)}</option>
    html = ''
    courses.each do |course|
      html << "<option value = #{course.id}>"
      html << course.name
      html << "</option>"
    end
    html.html_safe
  end
  

  # used to get the reward and handle the value which can be used to display in views
  # added by william
  def get_prize(b_project)
     b_project.get_reward 
  end
  
  def count_bid_project
    bids = Bid.where('parent_id = ?', @bid.id)
    @projects = []
    for bid in bids
      @projects += bid.biding_projects
    end
    @projects.count
  end
  
  def count_bid_user
    bids = Bid.where('parent_id = ?', @bid.id)
    @users = []
    for bid in bids
      for project in bid.projects
        @users += project.users
      end
    end
    @users.count
  end

  # 查看学号
  def im_watching_student_id? bid
    people = []
    people << bid.author
    case bid.reward_type # 天煞的bid分了三用途,里面各种hasmany还不定能用!
    when Bid::Enterprise
    when Bid::Contest
      bid.join_in_contests.each do |jic|
        people << jic.user
      end
    when Bid::Homework
      # people += bid.courses.first.users.to_a
      people += searchTeacherAndAssistant(bid.courses.first).to_a
    else
      raise 'bids_helper: unknow bid type' # 出了错看这里!不知道的抛异常,省的找不到出错的地方!
    end
    people.include?(User.current)
  end

  # 当前用户是否加入了此课程(包括教师)
  def is_cur_course_user? bid
    people = []
    #people << bid.author
    course = bid.courses.first
    course.members.each do |member|
      people <<  member.user
    end
    people.include?(User.current)
  end

  # def select_option_helper option
    # tmp = Hash.new
    # option.each do |project|
      # tmp[project.name] = project.identifier
    # end
    # tmp
  # end

  def can_delete_project_homework bind_project,current_user
    current_user.id == bind_project.user.id || current_user.admin
  end

end