class Www::PagesController < WwwController

  def show
    @page = Page.find(params[:id])

    @q = nil
    @q = @page.attachments.order("created_at DESC")
    @q = @q.search(params[:q])
    @items = @q.result.paginate(page: params[:page])

    @side_menu = []
    @side_menu << (@page.page_id.nil? ? @page : Page.find(@page.page_id))
    @side_menu << Page.find(@page.page_id).pages.where(active: true).order("id ASC") rescue nil
    @side_menu = @side_menu.flatten
    if @page.restricted?
      authenticate_user!
      authorize! :show, @page
    end

    if request.path != www_page_path(@page)
      return redirect_to @page, :status => :moved_permanently
    end
    contact_page = Page.find(Config[:www][:contact_page_id].to_i) rescue Page.find(8)
    if @page == contact_page #Config[:www][:contact_page_id]
      @contact_message = ContactMessage.new
      @contact_message.textcaptcha
    end

    add_breadcrumb I18n.t('www.layout.home'), www_root_path
    add_breadcrumb @page.name, www_page_path(@page)

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @page }
    end
  end

  def attachment_category
    @page = Page.find(params[:id])
    @attachment_category = (params[:category].nil? ? AttachmentCategory.first : AttachmentCategory.find(params[:category].to_i) )
    @attachment = @page.attachments.where(attachment_category_id: params[:category].to_i)

    @q = @attachment.search(params[:q])
    @attachments = @q.result.paginate(:page => params[:page])

    @applications_category = Page.find 7 #Aplicaciones

    add_breadcrumb I18n.t('www.layout.home'), www_root_path
    add_breadcrumb @page.name, www_page_path(@page)
    add_breadcrumb @attachment_category.name, attachment_category_www_page_path(@page, :category => @attachment_category.id)
  end

end
