class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController
  def initialize
    super
    @app_title = 'announcement'
  end
  
  # GET /bulletins
  # GET /bulletins.xml
  
  def index
  
	@part = PagePart.find(params[:part_id])
  
	if @part.widget_data_count
		@page_num = @part.widget_data_count
	else
		@page_num = 4
	end
  
	if @part.widget_field
		@widget_fields = @part.widget_field
	else
		@widget_fields = []
	end
	
    @title = params[:part_title]
	
    @widget_style = @part.widget_style
	
	@category_id = @part.category
	
    date_now = Time.now
    if !@category_id.blank?
      @bulletins = Bulletin.can_display.where(:bulletin_category_id => @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] ).per(@page_num)
      @current_category = BulletinCategory.from_id(@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] ).per(@page_num)
    else
      @bulletins = Bulletin.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] ).per(@page_num)
    end

    get_categorys
    
  end
  
  def bulletins_list_and_pic
  
  	if params[:widget_data_count]
  		@page_num = params[:widget_data_count];
  	else
  		@page_num = 4;
  	end
	
    @title = params[:part_title]
    date_now = Time.now
    if !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]).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]).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]).per(@page_num )
    end

    get_categorys
    
  end
  
  def bulletins_and_web_links
    @tags = AnnouncementTag.any_in(key: ['students', 'alumni', 'employee', 'guest']).asc(:order)
    @selected_tag = AnnouncementTag.find(params[:id]) rescue @tags[0]
    @bulletins = @selected_tag.get_visible_bulletins(:postdate).available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links(:created_at).available_for_lang(I18n.locale).page(params[:page]).per(5) rescue nil
  end
  
  def reload_bulletins
    @selected_tag = AnnouncementTag.find(params[:tag_id])
    @bulletins = @selected_tag.get_visible_bulletins(:postdate).available_for_lang(I18n.locale).can_display.page(params[:page]).per(5) rescue nil
  end
  
  def reload_web_links
    @selected_tag = AnnouncementTag.find(params[:tag_id])
    @web_links = WebResourceTag.first(:conditions => {:en => @selected_tag[:en]}).get_visible_links(:created_at).available_for_lang(I18n.locale).page(params[:page]).per(5) rescue nil
  end

  def bulletins_side_bar
    @tags_and_class = AnnouncementTag.sorted_for_cloud
    get_categorys
  end
  
  def bulletins_search_block
    
  end
  
  protected
  
  def get_categorys
    @bulletin_categorys = BulletinCategory.all
  end
  
end
