class Admin::AdminsController < ApplicationController
  layout "admin"
  
  before_action :auth_admin
  
  before_action :is_super_admin
  
  before_action :admin_space
  
  def admin_space
    @admin_space = "preferences"
  end
  
  
  def index
    
    @admins = Admin.all
  end


  def show
    @admin = Admin.find(params[:id])


    date_regex = /^(0[1-9]|[12][0-9]|3[01])[\/](0[1-9]|1[012])[\/](19|20)\d\d$/i 
    
    if params[:start] and params[:start] =~ date_regex
  		@start = Date.parse(params[:start]).beginning_of_day 
  		params[:start]= @start.strftime('%d/%m/%Y')
		else
  		params[:start] = ""
		end
    
		if params[:stop] and params[:stop] =~ date_regex
			@stop = Date.parse(params[:stop]).end_of_day 
			params[:stop]= @stop.strftime('%d/%m/%Y')
	  else
		 	params[:stop] = ""
		end
    @timer_watchers = @admin.timer_watchers
    @timer_watchers = @timer_watchers.after(@start) if @start
    @timer_watchers = @timer_watchers.before(@stop) if @stop
    
    

  end


  def new
    @admin = Admin.new
  
  end

  def edit
    @admin = Admin.find(params[:id])
  
  end

  def create
    @admin = Admin.new(admin_params)
    
    if @admin.save
      @admins = Admin.all
          
    else
      render :action => "new" 
    end
  end

  def update
    @admin = Admin.find(params[:id])
    
    if @admin.update_attributes(admin_params)

          
    else
      render :action => "edit" 
    end
    
  end
      


  def destroy
    @admin = Admin.find(params[:id])
    
    @admin.destroy if @admin != @current_admin
  
  end
  
  private
      def admin_params
        params.require(:admin).permit!
      end



end
