class Settings::MenuCategoriesController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_settings_menu_category, only: [:show, :edit, :update, :destroy]
  before_action :set_settings_menu, only: [:new]

  # GET /settings/menu_categories
  # GET /settings/menu_categories.json
  def index
    @settings_menu_categories = MenuCategory.all.page(params[:page]).per(10)
  end

  # GET /settings/menu_categories/1
  # GET /settings/menu_categories/1.json
  def show
  end

  # GET /settings/menu_categories/new
  def new
    @settings_menu_category = MenuCategory.new
    @settings_menu_category.menu = @settings_menu
  end

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

  # POST /settings/menu_categories
  # POST /settings/menu_categories.json
  def create
    @settings_menu_category = MenuCategory.new(settings_menu_category_params)
    @settings_menu_category.created_by = current_login_employee.name
    #@settings_menu_category.menu = @settings_menu

    respond_to do |format|
      if @settings_menu_category.save
        format.html { redirect_to settings_menu_categories_path, notice: 'Menu category was successfully created.' }
        format.json { render :show, status: :created, location: @settings_menu_category }
      else
        if (@settings_menu_category.errors)
          @settings_menu_category.errors.each do |error|
            Rails.logger.debug error
          end
        end
        format.html { render :new }
        format.json { render json: @settings_menu_category.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /settings/menu_categories/1
  # PATCH/PUT /settings/menu_categories/1.json
  def update
    respond_to do |format|
      if @settings_menu_category.update(settings_menu_category_params)
        format.html { redirect_to settings_menu_category_path(@settings_menu_category), notice: 'Menu category was successfully updated.' }
        format.json { render :show, status: :ok, location: @settings_menu_category }
      else
        format.html { render :edit }
        format.json { render json: @settings_menu_category.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /settings/menu_categories/1
  # DELETE /settings/menu_categories/1.json
  def destroy
    # instances = MenuItemInstance.where("menu_item_id=?",@settings_menu_item.id)
    # instances.each do |instance|
    #   instance.destroy
    # end 
    # @settings_menu_item.destroy
    
    # @settings_menu_category.destroy
    abc = MenuCategory.destroyCategory(@settings_menu_category)
    respond_to do |format|
      format.html { redirect_to settings_menu_categories_path, notice: 'Menu category was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_settings_menu_category
      @settings_menu_category = MenuCategory.find(params[:id])
    end
    def set_settings_menu
      @settings_menu = Menu.find(params[:menu_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def settings_menu_category_params
      params.require(:menu_category).permit(:code, :menu_id, :name, :alt_name, :order_by, :menu_category_id)
    end
end
