class Public::MessagesController < ApplicationController
  layout "public"
  before_filter :auth_customer


  def index
    need = Need.find(params[:need_id])
  end

  def create

   @need = Need.find(params[:need_id])

   @message = @need.messages.create(comment_params)
   @message.customer = current_customer

   if(@message.save)
     flash[:notice] = "Commentaire envoyé."
     admins = Admin.where.not(email: nil)
     admins.each do |admin|
       AdminMailer.customer_post_comment(admin, @need, @message).deliver
     end

     @need.customers.each do |customer|
       CustomerMailer.need_commented(customer, @need, @message).deliver
     end
   else
     flash[:error] = "Votre commentaire n'a pas pu être envoyé."
   end
   redirect_to :back


  end

  def comment_params
     params.require(:message).permit(:content)
  end

end
