class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'news'
  end
  
  # GET /news_bulletins
  # GET /news_bulletins.xml
  
  def index_news_bulletins_by_unit
    @page = Page.find(params[:page_id]) rescue nil
    @news_bulletins=NewsBulletin.all.available_for_lang(I18n.locale).can_display.where("unit_list_for_anc_id"=>params[:name]).page( params[:page_main]).per(15).page( params[:page_main]).per(10)
    render :index
  end

  def index
    @page = Page.find(params[:page_id]) rescue nil
    if !params[:search_query].blank?
      # search_cond =  {:is_checked=>true,:is_hidden=>false,:is_pending=>false,:is_pending=>false}
      # search_cond.merge!({:news_bulletin_category_id => "#{params[:category_id]}" }) if  !params[:category_id].blank?
      @search = NewsBulletin.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}
      @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).can_display.any_in(_id:search_result).desc(:postdate).page( params[:page_main]).per(10)
    else
      date_now = Time.now
      if !params[:category_id].blank? && !params[:tag_id].blank?
        @tag = NewsTag.find(params[:tag_id]) rescue nil
        @tag = NewsTag.where(key: params[:tag_id])[0] unless @tag
        @news_bulletins = @tag.news_bulletins.available_for_lang(I18n.locale).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(15)
        @current_category = NewsBulletinCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:category_id].blank?
        @news_bulletins = NewsBulletin.all.available_for_lang(I18n.locale).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(15)
        @current_category = NewsBulletinCategory.from_id(params[:category_id]) rescue nil
      elsif !params[:tag_id].blank?
        @tag = NewsTag.find(params[:tag_id]) rescue nil
        @tag = NewsTag.where(key: params[:tag_id])[0] unless @tag
        @news_bulletins = @tag.news_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(15)
      else
        @news_bulletins = NewsBulletin.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(15)
      end
      impressionist(@tag) if @tag
    end
  end
  
  def show
    if params[:preview] == "true"
      preview_content
    else
      ''
      @news_bulletin = NewsBulletin.all.can_display.where(_id: params[:id]).first
      if !@news_bulletin.disable? and !@news_bulletin.is_rejected
        if @news_bulletin.enabled_for_lang(I18n.locale.to_s)
          impressionist(@news_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
    @news_bulletin = NewsBulletin.find params[:id] rescue nil
    @news_bulletin = Preview.find(params[:id]).get_virtual_object if @news_bulletin.nil?
    get_categorys
  end
  
  protected
  
  def get_categorys
    @news_bulletin_categorys = NewsBulletinCategory.all
  end
  
end
