class Settings::SimpleMenuItemsController < ApplicationController
  before_action :set_settings_menu_item, only: [:show, :edit, :update, :destroy ]
  before_action :set_settings_menu_category, only: [:index, :show, :edit, :new, :update ,:create]
  # GET /settings/menu_items
  # GET /settings/menu_items.json
  def index
    @settings_menu_items = @category.menu_items.page(params[:page]).per(10)
  end

  # GET /settings/menu_items/1
  # GET /settings/menu_items/1.json
  def show
    @sub_menu = MenuItem.where("menu_item_id=?",params[:id]).page(params[:page]).per(10)
    @menu_item_instance = MenuItemInstance.where("menu_item_id=?",params[:id]).page(params[:page]).per(10)
  end

  # GET /settings/menu_items/new
  def new
    @settings_menu_item = MenuItem.new
  end

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

  # POST /settings/menu_items
  # POST /settings/menu_items.json
  def create
    @settings_menu_item = MenuItem.new(settings_menu_item_params)
    if params[:simple_menu_item][:menu_item_id] == ''
      @settings_menu_item.menu_category_id = params[:menu_category_id]
    end
    @settings_menu_item.created_by = current_login_employee.name
    respond_to do |format|
      if @settings_menu_item.save
        format.html { redirect_to settings_menu_category_simple_menu_items_path, notice: 'Menu item was successfully created.' }
        format.json { render :show, status: :created, location: @settings_menu_item }
      else
        format.html { render :new }
        format.json { render json: @settings_menu_item.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /settings/menu_items/1
  # DELETE /settings/menu_items/1.json
  def destroy
    abc = MenuItem.deleteRecursive(@settings_menu_item)
    respond_to do |format|
      format.html { redirect_to settings_menu_category_simple_menu_items_path, notice: 'Menu item 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_item
      @settings_menu_item = MenuItem.find(params[:id])
    end

    def set_settings_menu_category
      @category = MenuCategory.find(params[:menu_category_id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def settings_menu_item_params
      params.require(:simple_menu_item).permit(:item_code, :name, :alt_name, :type, :menu_category_id,:account_id,:item_attributes, :menu_item_id, :min_qty, :min_selectable_item, :max_selectable_item, :created_by)
    end
end
