class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'news'
  end
  
  # GET /news_bulletins
  # GET /news_bulletins.xml
  
  def index
    date_now = Time.now
    if !params[:category_id].blank?
      @news_bulletins = NewsBulletin.can_display.where(:news_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?
      @news_bulletins = NewsTag.find(params[:tag_id]).news_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
      @news_bulletins = NewsBulletin.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 = NewsBulletinCategory.from_id(params[:category_id]) rescue nil
  end
  
  def show
    if params[:preview] == "true"
      preview_content
    else
      @news_bulletin = NewsBulletin.can_display.where(_id: params[:id]).first
      get_categorys
    end
  end
  
  def preview_content
    @news_bulletins = NewsBulletin.find params[:id]
    get_categorys
    render :show
  end
  
  protected
  
  def get_categorys
    @news_bulletin_categorys = NewsBulletinCategory.excludes('disabled' => true)
  end
  
end
