class Panel::WebResource::FrontEnd::WebLinksController < OrbitWidgetController
  
  def initialize
    super
    @app_title = 'web_resource'
  end
  
  # GET /web_links
  # GET /web_links.xml
 
  def index
    
    @item = Page.find(params[:page_id])

    @title = @item.title
  
    if @item.frontend_data_count
      @page_num = @item.frontend_data_count
    else
      @page_num = 4
    end
  
    date_now = Time.now

    # @web_link_categorys = get_categories_for_index
    if !params[:filter_category_id].blank?
      @web_link_categorys = @module_app.categories.enabled.any_in(:_id => params[:filter_category_id])
    else
      @web_link_categorys = @module_app.categories.enabled
    end

    @web_link_datas = Array.new

    @web_link_categorys.each do |wlcg|

      if !params[:category_id].blank? && !params[:tag_id].blank?
        @web_links = WebLink.all.can_display.where(:category_id.in => [wlcg.id], :tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at )
      elsif !params[:category_id].blank?
        @web_links = WebLink.all.can_display.where(:category_id.in => [wlcg.id]).desc( :is_top, :created_at )
      elsif !params[:tag_id].blank?
        @web_links = WebLink.all.can_display.where(:category_id => wlcg.id, :tagged_ids.in => params[:tag_id]).desc( :is_top, :created_at )
      else
        @web_links = WebLink.all.can_display.where(:category_id => wlcg.id).desc( :is_top, :created_at )
      end

      web_link_datas = Array.new

      @web_links.each do |wl|
        web_link_datas << { 
            "title" => wl.title,
            "is_hot" => (!wl.is_hot.blank? ? '1' : ''), 
            "context" => wl.context,
            "url" => wl.url
        } if !wl.blank?
      end

      @web_link_datas << { "title" => wlcg.title, "web_links" => web_link_datas } if !@web_links.blank?

    end

  end

  def show
    @item = Page.find(params[:page_id])
    @title = @item.title
    @web_link = WebLink.find(params[:id])
    get_categorys
  end
  
  protected

  def get_categorys
    @web_link_categorys = WebLinkCategory.excludes('disabled' => true)
  end
  
end
