class Settings::ZonesController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_settings_zone, only: [:show, :edit, :update, :destroy]

  # GET /settings/zones
  # GET /settings/zones.json
  def index
    @settings_zones = Zone.all
  end

  # GET /settings/zones/1
  # GET /settings/zones/1.json
  def show
    @settings_tables = @settings_zone.tables
    @settings_rooms = @settings_zone.rooms
  end

  # GET /settings/zones/new
  def new
    @settings_zone = Zone.new
  end

  # GET /settings/zones/1/edit
  def edit
  end

  # POST /settings/zones
  # POST /settings/zones.json
  def create
    @settings_zone = Zone.new(settings_zone_params)
    @settings_zone.created_by = current_login_employee.name
    respond_to do |format|
      if @settings_zone.save
        format.html { redirect_to settings_zone_path(@settings_zone), notice: 'Zone was successfully created.' }
        format.json { render :show, status: :created, location: @settings_zone }
      else
        format.html { render :new }
        format.json { render json: @settings_zone.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /settings/zones/1
  # PATCH/PUT /settings/zones/1.json
  def update
    respond_to do |format|
      @settings_zone.created_by = current_login_employee.name
      if @settings_zone.update(settings_zone_params)
        format.html { redirect_to settings_zone_path(@settings_zone), notice: 'Zone was successfully updated.' }
        format.json { render :show, status: :ok, location: @settings_zone }
      else
        format.html { render :edit }
        format.json { render json: @settings_zone.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /settings/zones/1
  # DELETE /settings/zones/1.json
  def destroy
    @settings_zone.rooms.destroy
    @settings_zone.tables.destroy
    @settings_zone.destroy
    respond_to do |format|
      format.html { redirect_to settings_zones_path, notice: 'Zone was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_settings_zone
      @settings_zone = Zone.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def settings_zone_params
      params.require(:zone).permit(:name, :is_active, :created_by)
    end
end
