class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  
  def index
  
    date_now = Time.now
    if !params[:category_id].blank?
      @bulletins = Bulletin.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)
    elsif !params[:tag_id].blank?
      @bulletins = AnnouncementTag.find(params[:tag_id]).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.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
	  
	  @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
  end
  
  def show
    if params[:preview] == "true"
      preview_content
    else
      @bulletin = Bulletin.can_display.where(_id: params[:id]).first
      get_categorys
    end

  end
  
  def preview_content
    @bulletin = Bulletin.find params[:id]
    get_categorys
    render :show
  end
  
  protected
  
  def get_categorys
    @bulletin_categorys = BulletinCategory.excludes('disabled' => true)
  end
  
end
