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]
  before_action :set_settings_item_attribute, only: [:index, :show, :edit, :new, :update ,:create]
  before_action :set_settings_item_option, 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     
    @item_sets = ItemSet.all 
  end

  # GET /settings/menu_items/1/edit
  def edit 
    @item_sets = ItemSet.all 
  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

    # Remove "" default first 
    params[:simple_menu_item][:item_attributes].delete_at(0)
    params[:simple_menu_item][:item_options].delete_at(0)

    # attr_format = []
    # param_count = params[:simple_menu_item][:item_attributes].count
    # # Format for attributes json    
    # params[:simple_menu_item][:item_attributes].each do|attr_id|      
    #   menu_attr = MenuItemAttribute.find(attr_id)
    #   if attr_format.count == 0
    #     attr_format.push({ type: menu_attr.attribute_type, value: [] })
    #   end

    #   attr_format.each do |af|
    #     if menu_attr.attribute_type == af[:type]
    #       af[:value].push(menu_attr.value)
    #     else 
    #       new_attr = {type: menu_attr.attribute_type, value: [ menu_attr.value ] }
    #       attr_format.push(new_attr) 
    #     end
        
    #     break if attr_format.count > param_count
    #   end

    #   param_count -= 1           
    # end
    # @settings_menu_item.item_attributes = attr_format

    # Bind for Item Sets
    if params[:simple_menu_item][:item_sets].count > 1
      params[:simple_menu_item][:item_sets].delete_at(0)

      sets = ItemSet.find(params[:simple_menu_item][:item_sets])

      if sets.count > 0
        @settings_menu_item.item_sets = sets
      end
    end
    
    respond_to do |format|
      if @settings_menu_item.save
        menu_item = MenuItem.find(@settings_menu_item.id)
        menu_item.update_attributes(item_attributes: params[:simple_menu_item][:item_attributes], item_options: params[:simple_menu_item][:item_options])

        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
    # Remove "" default first 
    params[:simple_menu_item][:item_attributes].delete_at(0)
    params[:simple_menu_item][:item_options].delete_at(0)

    # Bind for Item Sets
    if params[:simple_menu_item][:item_sets].count > 0
      params[:simple_menu_item][:item_sets].delete_at(0)

      sets = ItemSet.find(params[:simple_menu_item][:item_sets])

      if sets.count >= 0
        # destroy all item sets for edit 
        @settings_menu_item.item_sets.destroy
        @settings_menu_item.item_sets = sets
      end
    end

    @settings_menu_item.menu_category_id = params[:menu_category_id]

    respond_to do |format|
      if @settings_menu_item.update(settings_menu_item_params)
        @settings_menu_item.update_attributes(item_attributes: params[:simple_menu_item][:item_attributes], item_options: params[:simple_menu_item][:item_options])
        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    
    path_to_file = @settings_menu_item.image_path.to_s        
    File.delete(path_to_file) if File.exist?(path_to_file)
    abc = MenuItem.deleteRecursive(@settings_menu_item)
    render :json => {:status=> "Success", :url => settings_menu_category_simple_menu_items_path }.to_json
    # 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

    def set_settings_item_attribute
      @item_attributes = MenuItemAttribute.all.order("id asc")   #.map{|a| [a.name, a.value]}     
    end

    def set_settings_item_option
      @item_options = MenuItemOption.all
    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, :image_path, :menu_category_id, :account_id, :item_attributes, :item_options, :min_qty, :is_sub_item, :is_available, :created_by, :item_sets, :unit, :taxable)
    end
end
