class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController

  # GET /bulletins
  # GET /bulletins.xml
  def index_bulletins_by_unit
    @item = Page.find(params[:page_id]) rescue nil
    @bulletins=Bulletin.all.available_for_lang(I18n.locale).can_display.where("cache_dept.#{I18n.locale.to_s.downcase}"=>params[:name]).page( params[:page_main]).per(15)
    render :index
  end

  def search_result
    if params[:search_query] == ""
      @bulletins = get_bulletins_for_index
      
    else
      @search = Bulletin.solr_search do 
        fulltext params[:search_query]
        with(:frontend_search,true)
        with(:available_lang).any_of([I18n.locale.to_s])
      end
      # @bulletins = Bulletin.where(:category_id.in => params[:category_id]).and(:is_rejected=>false).and(:is_pending=>false)
      search_result =  @search.results.collect{|result| result.id}

      @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).page( params[:page_main]).per(@page_num)
    end
  end

  def index
    @bulletins = get_bulletins_for_index
   end

   def get_bulletins_for_index
    
      @item = Page.find(params[:page_id]) rescue nil
      
    if @item
      if @item.frontend_data_count
        @page_num = @item.frontend_data_count
      else
        @page_num = 15
      end
      @frontend_style = @item.frontend_style
    end
       
      date_now = Time.now
      if !params[:category_id].blank? && !params[:tag_id].blank?
        @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:category_id.in => params[:category_id], :tagged_ids.in => params[:tag_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
        # @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:category_id.in=>params[:category_id],:tagged_ids.in=>params[:tag_id])
        @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:category_id].blank?
        @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.where(:category_id.in => params[:category_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
        @current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:tag_id].blank?
        @bulletins = Bulletin.available_for_lang(I18n.locale).can_display.where(:tagged_ids.in => params[:tag_id]).desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
      else
        @bulletins = Bulletin.all.available_for_lang(I18n.locale).can_display.desc( :is_top, :postdate).page( params[:page_main]).per(@page_num)
      end
      # delayed_impressionist(@tag) if @tag
      @bulletins
   end 
  
  def show
    @item = Page.find(params[:page_id]) rescue nil
    if params[:preview] == "true"
      preview_content
    else
      @bulletin = Bulletin.all.can_display.where(_id: params[:id]).first
      if @bulletin and !@bulletin.disable? and !@bulletin.is_rejected
        if @bulletin.enabled_for_lang(I18n.locale.to_s)
          # delayed_impressionist(@bulletin)
          impressionist(@bulletin)
        else
          render :text => "<div class='alert alert-error'>#{t('sys.can_not_display_due_to_no_context')}</div>".html_safe 
        end
      else
        render :nothing => true, :status => 403
      end
    end

  end

  def preview_content
    @bulletin = Bulletin.find params[:id] rescue nil
    @bulletin = Preview.find(params[:id]).get_virtual_object if @bulletin.nil?
  end

end
