class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  
  def index
    @title = params[:part_title]
    date_now = Time.now
    if !params[:category_id].blank?
      @bulletins = Bulletin.all.can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10)
      @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
    elsif !params[:tag_id].blank?
      @tag = AnnouncementTag.find(params[:tag_id]) rescue nil
      @tag = AnnouncementTag.where(key: params[:tag_id])[0] unless @tag
      @bulletins = @tag.bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10)
    else
      @bulletins = Bulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page]).per(10)
    end

    get_categorys
    
  end
  
  def bulletins_and_web_links
    @tags = AnnouncementTag.any_in(key: ['students', 'alumni', 'employee', 'guest']).asc(:order)
    @selected_tag = AnnouncementTag.find(params[:id]) rescue @tags[0]
    @bulletins = @selected_tag.get_visible_bulletins.can_display.page(params[:page]).per(5) rescue nil
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links.page(params[:page]).per(5) rescue nil
  end
  
  def reload_bulletins
    @selected_tag = AnnouncementTag.find(params[:tag_id])
    @bulletins = @selected_tag.get_visible_bulletins.can_display.page(params[:page]).per(5) rescue nil
  end
  
  def reload_web_links
    @selected_tag = AnnouncementTag.find(params[:tag_id])
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links.page(params[:page]).per(5) rescue nil
  end

  def bulletins_side_bar
    @tags = AnnouncementTag.all
    get_categorys
  end
  
  def bulletins_search_block
    
  end
  
  protected
  
  def get_categorys
    @bulletin_categorys = BulletinCategory.all
  end
  
end
