class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  
  def index
    if  !params[:search_query].blank?
      search_cond =  {:is_checked=>true,:is_hidden=>false}
      search_cond.merge!({:bulletin_category_id => "#{params[:category_id]}" }) if  !params[:category_id].blank?
      search = Redis::Search.query("Bulletin", params[:search_query], :conditions =>search_cond)
      search_result = search.collect{|t| t["id"]}
      @news_bulletins = Bulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(10)
    else
      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_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.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_main]).per(10)
      end
    end
	  
  end
  
  def show
    if params[:preview] == "true"
      preview_content
    else
      @bulletin = Bulletin.all.can_display.where(_id: params[:id]).first
      unless @bulletin.disable?
        impressionist(@bulletin)
        get_categorys  
      else
         render :nothing => true, :status => 403
      end
    end

  end
  
  def preview_content
    @bulletin = Bulletin.find params[:id]
    get_categorys
  end
  
  protected
  
  def get_categorys
    @bulletin_categorys = BulletinCategory.all
  end
  
end
