class Public::ContactsController < ApplicationController  
  layout :get_public_layout
  
  def index
    
  end
  
  def new
    @contact = Contact.new(:raison_id => params[:raison_id], :survey_set_id => params[:id], :provenance_id => 3)
    
    session[:product_qtes] = session[:product_qtes] || {}
    
    
    
    if @contact.raison_id == 4 and session[:product_ids] and session[:product_ids].size > 0
      session[:product_ids].each do |token|
        
        
        
        @contact.contact_products << ContactProduct.new(:d_product => DProduct.find(token), :qte => session[:product_qtes][token])
        
      end
    end
    
    
  end
	def create
		@contact = Contact.new(params.require(:contact).permit!)
    
    if @contact.raison_id == 4
      
      @contact.contact_products.each do |contact_product|
      
        session[:product_qtes][contact_product.d_product_id.to_s] = contact_product.qte

        
      end
    end
	
		if @contact.save
      if @contact.raison_id == 4
        @contact.notes = session[:product_ids].join(",")
        session[:product_ids] = []
      end
      @contact.save
			QuestionMailer.send_contact(@contact).deliver
      @contact.syncro
      render :action => :create
    else
      
      render :action => :new
		end

	end

end
