class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  def index_bulletins_by_unit
    @page = 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 index
    @item = Page.find(params[:page_id])
  
  	if @item.frontend_data_count
  		@page_num = @item.frontend_data_count
  	else
  		@page_num = 15
  	end
    @frontend_style = @item.frontend_style

    @page = Page.find(params[:page_id]) rescue nil
    if  !params[:search_query].blank?
      # search_cond =  {:is_checked=>true,:is_hidden=>false,:is_pending => false}
      # search_cond.merge!({:bulletin_category_id => "#{params[:category_id]}" }) if  !params[:category_id].blank?
      @search = Bulletin.solr_search do 
        fulltext params[:search_query]
        with(:frontend_search,true)
        with(:available_lang).any_of([I18n.locale.to_s])
      end
      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)
    else
      date_now = Time.now
      if !params[:category_id].blank? && !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.available_for_lang(I18n.locale).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(@page_num)
        @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(: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(@page_num)
        @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.available_for_lang(I18n.locale).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(@page_num)
      else
        @bulletins = Bulletin.all.available_for_lang(I18n.locale).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(@page_num)
      end
      impressionist(@tag) if @tag
    end
	  
  end
  
  def show
    @page = 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.disable? and !@bulletin.is_rejected
        if @bulletin.enabled_for_lang(I18n.locale.to_s)
          impressionist(@bulletin)
          get_categorys 
        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?
    get_categorys
  end
  
  protected
  
  def get_categorys
    @bulletin_categorys = BulletinCategory.all
  end
  
end
