class Settings::PromotionsController < ApplicationController
  load_and_authorize_resource except: [:create]
  before_action :set_promotion, only: [:show, :edit, :update, :destroy]
  before_action :check_user

  # GET /promotions
  # GET /promotions.json
  def index
    @promotions = Promotion.all.order("created_at asc")
  end

  # GET /promotions/1
  # GET /promotions/1.json
  def show
  end

  # GET /promotions/new
  def new
    @promotion = Promotion.new
    @promotion.promo_start_date = DateTime.now
    @promotion.promo_end_date = DateTime.now
  end

  # GET /promotions/1/edit
  def edit
  end

  # POST /promotions
  # POST /promotions.json
  def create
    @promotion = Promotion.new(promotion_params)
    @promotion.created_by = current_login_employee.id
    if !@promotion.promo_start_hour.nil?
      @promotion.promo_start_hour = @promotion.promo_start_hour.to_datetime.advance(hours: +6, minutes: +30)
    end
    if !@promotion.promo_end_hour.nil?
      @promotion.promo_end_hour = @promotion.promo_end_hour.to_datetime.advance(hours: +6, minutes: +30)
    end
    respond_to do |format|
      if @promotion.save
        promo_pros = @promotion.promotion_products
        promo_pros.each do |promo_pro|
          promo_pro.save
        end
        format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully created.' }
        format.json { render :show, status: :created, location: @promotion }
      else
        format.html { render :new }
        format.json { render json: @promotion.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /promotions/1
  # PATCH/PUT /promotions/1.json
  def update
    respond_to do |format|
      if @promotion.update(promotion_params)
        @promotion.promo_start_hour = @promotion.promo_start_hour.to_datetime.advance(hours: +6, minutes: +30)
        @promotion.promo_end_hour = @promotion.promo_end_hour.to_datetime.advance(hours: +6, minutes: +30)
        @promotion.save
        promo_pros = @promotion.promotion_products
        promo_pros.each do |promo_pro|
          promo_pro.save
        end
        format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully updated.' }
        format.json { render :show, status: :ok, location: @promotion }
      else
        format.html { render :edit }
        format.json { render json: @promotion.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /promotions/1
  # DELETE /promotions/1.json
  def destroy
    @promotion.promotion_products.each do |pp|
      pp.destroy
    end
    @promotion.destroy
    flash[:notice] = 'Promotion was successfully destroyed.'
    render :json => {:status=> "Success", :url => settings_promotions_path }.to_json
    # respond_to do |format|
    #   format.html { redirect_to settings_promotions_path, notice: 'Promotion was successfully destroyed.' }
    #   format.json { head :no_content }
    # end
  end

  def find_item_instance
    item = MenuItem.find_by_item_code(params[:item_code])
    if item.nil?
      product = Product.where("item_code = ?",params[:item_code]).pluck(:name,:item_code)
      render json: product
    else
      menu_instance = MenuItemInstance.where("menu_item_id = ?",item.id).pluck(:item_instance_name,:item_instance_code)
      render json: menu_instance
    end
  end

  def find_parent_item
    res = []
    arr_inst = []
    item_inst = MenuItemInstance.find_by_item_instance_code(params[:item_instance_code])
    if item_inst.nil?
      product = Product.where("item_code = ?",params[:item_instance_code])
      product.each do |pdt|
        res.push({pdt.item_code => pdt.name})
      end
    else
      # menu_item = item_inst.menu_item.pluck(:name,:item_code)
      # res.push(item_inst.item_instance_name)
      # res.push(item_inst.menu_item.item_code)
      item_inst.menu_item.menu_item_instances.each do |inst|
        arr_inst.push({inst.item_instance_code => inst.item_instance_name})
      end
      res.push({item_inst.menu_item.item_code => arr_inst})
    end
    puts "res"
    p res
    render json: res
  end

  def check_user
    if current_user.nil?
      redirect_to root_path
    end
  end
  
  private
    # Use callbacks to share common setup or constraints between actions.
    def set_promotion
      @promotion = Promotion.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def promotion_params
      params.require(:promotion).permit(:promo_code, :promo_start_date, :promo_end_date, :promo_start_hour,:promo_end_hour ,:promo_day, :promo_type,:original_product ,:min_qty ,:created_by,
                                        :promotion_products_attributes => [:id,:item_code, :min_qty, :net_off, :net_price, :percentage, :_destroy])
    end
end
