
class CyclesController < ApplicationController
  before_action :user_is_admin?
  before_action :set_cycle, only: %i[ show edit update destroy ]
  layout "admin"

  def index
    @cycles = Cycle.where(template: true)
  end

  def new
    @cycle = Cycle.new(template: true)
  end

  def create
    @cycle = Cycle.new(cycle_params)
    if @cycle.save
      redirect_to edit_cycle_url(@cycle), notice: "Ciclo creado."
    else
      render :new, status: :unprocessable_entity
    end
  end

  def update
    if @cycle.update(cycle_params)
      redirect_to cycles_url, notice: "Ciclo actualizado."
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @cycle.destroy
    redirect_to cycles_url, notice: "Ciclo eliminado."
  end

  private

    def set_cycle
      @cycle = Cycle.find(params[:id])
    end

    def cycle_params
      params.require(:cycle).permit(:name, :number, :template, :instructions, :init, :duration, cycle_steroids_attributes: [:id, :_destroy, :quantity, :unit, :steroid_id, :intake, :time_unit, :notes])
    end

end