class DashboardController < ApplicationController
  before_action :user_is_employee? #,  only: :index

  layout "admin"

  def index
    @today = Time.zone.now.to_date
    @trainer = params[:trainer_id] ? User.find(params[:trainer_id]) : current_user
    if current_user.trainer?
      @users = User.where(coach_id: @trainer.id)
      @plans = Plan.where(trainer_id: @trainer.id).where("finish >= ?", @today - 1.week)
    else
      @plans = Plan.where("finish >= ?", Time.zone.now.to_date)
      @users = User.where.not(last_plan: nil).where("last_plan <= ? ", @today + 2.weeks)
    end
    @plans = @plans.order(finish: :asc).paginate(:page => params[:page])
    @users = @users.order(last_plan: :desc).paginate(:page => params[:page])
    redirect_to "/recepcion" if current_user.role == "reception"
  end

  def birthdays
    @birthdays = User.where.not(last_plan: nil).where.not(dob: nil).sort_by(&:birth_month).group_by(&:birth_month)
  end

end