class Settings::PromotionProductsController < ApplicationController
  before_action :set_promotion, only: [:show, :edit, :update, :destroy,:new]
  before_action :set_promotion_product, only: [:show, :edit, :update, :destroy]

  # GET /promotion_products
  # GET /promotion_products.json
  def index
    @promotion_products = PromotionProduct.all
  end

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

  # GET /promotion_products/new
  def new
    @promotion_product = PromotionProduct.new
  end

  # GET /promotion_products/1/edit
  def edit
  end

  # POST /promotion_products
  # POST /promotion_products.json
  def create
    @promotion_product = PromotionProduct.new(promotion_params)
    respond_to do |format|
      if @promotion_product.save
        format.html { redirect_to edit_settings_promotion_path(@promotion), notice: 'PromotionProduct was successfully created.' }
        format.json { render :show, status: :created, location: @promotion_product }
      else
        format.html { render :new }
        format.json { render json: @promotion_product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /promotion_products/1
  # PATCH/PUT /promotion_products/1.json
  def update
    respond_to do |format|
      if @promotion_product.update(promotion_params)
        format.html { redirect_to edit_settings_promotion_path(@promotion), notice: 'PromotionProduct was successfully updated.' }
        format.json { render :show, status: :ok, location: @promotion_product }
      else
        format.html { render :edit }
        format.json { render json: @promotion_product.errors, status: :unprocessable_entity }
      end
    end
  end

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

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_promotion
      @promotion = Promotion.find(params[:promotion_id])
    end
    def set_promotion_product
      @promotion_product = PromotionProduct.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def promotion_params
      params.require(:promotion_product).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)
    end
end
