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_main]).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_main]).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_main]).per(10)
    end
	  
  end
  
  def show
    if params[:preview] == "true"
      preview_content
    else
      @bulletin = Bulletin.can_display.where(_id: params[:id]).first
      impressionist(@bulletin)
      get_categorys
    end

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