class CatalogueController < WithOrderController

  def index
    @categories = CatalogueCategory.readable_hash_tree
  end

  def index_by_manufacturer
    @manufacturers = Manufacturer.visible_to_user.select([:id, :title, :logotype_uri])
  end

  def info
    @category = CatalogueCategory.select([:id, :title, :description, :body, :parent_id]).find(params[:id])
    @catalogue_category_path = @category.self_and_ancestors.select([:id, :title]).reverse.drop(1)
    @category_parent = @catalogue_category_path[0]
    @tree = CatalogueCategory.hash_tree.shift[1][@category_parent]
  end

  def info_by_manufacturer
    @manufacturer = Manufacturer.visible_to_user.select([:id, :title, :body, :description]).find(params[:id])
  end

  def show
    @category = CatalogueCategory.select([:id, :title, :description, :body, :parent_id]).find(params[:id])
    @catalogue_category_path = @category.self_and_ancestors.select([:id, :title]).reverse.drop(1)
    @category_parent = @catalogue_category_path[0]
    @tree = CatalogueCategory.hash_tree.shift[1][@category_parent]
    @products = Product.visible_to_user.where(:catalogue_category_id => @category.self_and_descendants.collect(&:id)).page(params[:page]).per(30)
  end

  def show_by_manufacturer
    @manufacturer = Manufacturer.visible_to_user.select([:id, :title, :body, :description]).find(params[:id])
    @products = @manufacturer.products.visible_to_user.select(['products.id', 'products.title', 'products.main_image_uri']).page(params[:page]).per(30)
  end

end
