class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'news'
  end
  
  # GET /news_bulletins
  # GET /news_bulletins.xml
  
  def index
    if !params[:search_query].blank?
      search_cond =  {:is_checked=>true,:is_hidden=>false}
      search_cond.merge!({:news_bulletin_category_id => "#{params[:category_id]}" }) if  !params[:category_id].blank?
      search = Redis::Search.query("NewsBulletin", params[:search_query], :conditions =>search_cond)
      search_result = search.collect{|t| t["id"]}
      @news_bulletins = NewsBulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(10)
    else
        date_now = Time.now
      if !params[:category_id].blank?
       @news_bulletins = NewsBulletin.all.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_main]).per(10)
      elsif !params[:tag_id].blank?
        tmp = NewsTag.find(params[:tag_id]) rescue nil
        tmp = NewsTag.where(key: params[:tag_id])[0] unless tmp
       @news_bulletins = tmp.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_main]).per(10) rescue nil
      else
        @news_bulletins = NewsBulletin.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
	  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.all.can_display.where(_id: params[:id]).first
      unless @news_bulletin.disable?
        # impressionist(@news_bulletin)
        get_categorys
      else
        render :nothing => true, :status => 403 
      end

    end
  end
  
  def preview_content
    @news_bulletin = NewsBulletin.find params[:id]
    get_categorys
  end
  
  protected
  
  def get_categorys
    @news_bulletin_categorys = NewsBulletinCategory.all
  end
  
end
