class Admin::ContactMessagesController < ApplicationController
  layout "admin"
  before_filter :auth_admin


  def create

    @contact_message = ContactMessage.new
    @contact_message.assign_attributes(message_params)
    @contact_message.admin = current_admin
    @contact_message.read_by_admin = true

    if @contact_message.save
      CustomerMailer.contact_message_received(@contact_message.contact, @contact_message).deliver
      flash[:notice] = "Message envoyé."
      return redirect_to :back
    else
      flash[:error] = "Votre message n'a pas pu être envoyé."
      return render 'index'
    end

  end


  def destroy
     @message = ContactMessage.find(params['id'])
     if @message.destroy
       flash[:notice] = "message supprimé"
     else
       flash[:error] = "Impossible de supprimer ce message"
     end

     redirect_to :back
  end

  def message_params
     params.require(:contact_message).permit(:content, :contact_id)
  end

end
