class Panel::WebResource::Widget::WebLinksController < OrbitWidgetController

  def initialize
    super
    @app_title = 'web_resource'
  end
  
  # GET /web_links
  # GET /web_links.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

    @widget_style = @part.widget_style

    @category_id = @part.category

    date_now = Time.now

    if !params[:category_id].blank?
      @web_link_categorys = @module_app.categories.enabled.any_in(:_id => params[: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 ).page( params[:page_main] ).per(@page_num)
      elsif !params[:category_id].blank?
        @web_links = WebLink.all.can_display.where(:category_id.in => [wlcg.id]).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num)
      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 ).page( params[:page_main] ).per(@page_num)
      else
        @web_links = WebLink.all.can_display.where(:category_id => wlcg.id).desc( :is_top, :created_at ).page( params[:page_main] ).per(@page_num)
      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 get_categorys
    @web_link_categorys = WebLinkCategory.excludes('disable' => true)
  end
  
end
