class Public::PriceLinesController < ApplicationController
  layout "public"



  def create

    if params[:configurateur]

      @price_line = @current_p_customer_sheet.price_line_block.price_lines.new(params.require(:price_line).permit!)



      if @price_line.save
        if !request.xhr?
          redirect_to cart_public_p_orders_path
        end

      end


    else


      if @p_product_ref = PProductRef.find(params[:price_line][:p_product_ref_id])

        if price_line = @current_p_customer_sheet.price_line_block.price_lines.where(:p_product_ref_id => @p_product_ref.id).first
         price_line.qte = price_line.qte + params[:price_line][:qte].to_f
        else
          price_line = @current_p_customer_sheet.price_line_block.price_lines.build(:qte => params[:price_line][:qte], :p_product_ref_id => @p_product_ref.id)
        end

        puts "ESSAI"
      puts @current_p_customer_sheet.id
      puts "ESSAI"

        puts price_line.price_line_block.errors

        puts price_line.save

        puts price_line.errors.messages

        #raise


        if !request.xhr?
          redirect_to public_p_product_path(@p_product_ref.p_product)
        end

      end

    end




  end

  def edit
    @price_line = @current_p_customer_sheet.price_line_block.price_lines.find(params[:id])

  end

  def update
    @price_line = @current_p_customer_sheet.price_line_block.price_lines.find(params[:id])

    if @price_line.update_attributes(params.require(:price_line).permit!)

      if !request.xhr?
        redirect_to cart_public_p_orders_path()
      end
    end
  end

  def destroy
    @price_line = @current_p_customer_sheet.price_line_block.price_lines.find(params[:id])

    if @price_line.destroy

      if !request.xhr?
        redirect_to cart_public_p_orders_path()
      end
    end
  end

end
