# -*- encoding : utf-8 -*-

class Admin::CircuitsController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "stocks"
  end
  
  def index
    @circuits = Circuit.all
    @circuits = sort_by_sorting(@circuits, "name DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
    		per_page = params[:search][:per_page]
    		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
    		@circuits = @circuits.page(page).per(per_page)
      
      }
    end

  end

  def show
    @circuit = Circuit.find(params[:id])

  end

  def new
    @circuit = Circuit.new
  
  end

  def edit
    @circuit = Circuit.find(params[:id])
    
  end

  def create
    @circuit = Circuit.new(params.require(:circuit).permit!)

    if @circuit.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @circuit = Circuit.find(params[:id])


    if @circuit.update_attributes(params.require(:circuit).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @circuit = Circuit.find(params[:id])
    @circuit.destroy
    
  end
end
