class Admin::CustomersController < ApplicationController
  layout "admin"



  def index

    per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 50

    page = (params[:page] and params[:page] != "") ? params[:page] : 1


    @customers = Customer.all

    if(params[:q] != '')
      @customers = @customers.search(params[:q])
    end

    @customers = @customers.page(page).per(per_page).order("created_at DESC").all
  end


  def connected_customers

    @customers_5m = Customer.where("last_activity > ?", DateTime.now - 5.minutes)
    @customers_1h = Customer.where("last_activity > ?", DateTime.now -  1.hour)
    @customers_24h = Customer.where("last_activity > ?", DateTime.now -  24.hours)


  end


  def show
    @customer = Customer.find(params[:id])
  end

  def validate
    @customer = Customer.find(params[:id])
    @customer.account_validated = true
    @customer.account_validated_at = Time.now
    @customer.save
    CustomerMailer.validate_account(@customer).deliver

    redirect_to :back
  end


  def edit
        @customer = Customer.find(params[:id])

  end
  def update
    @customer = Customer.find(params[:id])

    if @customer.update_attributes(params.require(:customer).permit!)
      redirect_to admin_customers_path
    else
      render :action => "edit"
    end

  end


  def destroy

    	@customer = Customer.find(params[:id])
  	  @customer.destroy



  end



end
