class WorkoutsController < ApplicationController
  before_action :user_is_editor?
  before_action :set_workout, only: [:edit, :update, :destroy]
  layout "admin"

  def index
    @workouts = Workout.all #.group_by(&:category)
  end

  def show
    redirect_to workouts_url
  end

  def new
    @workout = Workout.new
  end

  def edit
  end

  def create
    @workout = Workout.new(workout_params)
    @workout.id = Workout.all.order(id: :asc).last.id + 1
    if @workout.save
      redirect_to workouts_url
    else
      render :new
    end
  end

  def update
    if @workout.update(workout_params)
      redirect_to workouts_url
    else
      render :edit
    end
  end

  def destroy
    @workout.routine_workouts.delete_all
    @workout.destroy
    redirect_to workouts_url
  end

  private

    def set_workout
      @workout = Workout.find(params[:id])
    end

    def workout_params
      params.require(:workout).permit(:name, :video_man, :video_woman, :description, :image, :group, :sub_group, :category)
    end

end