class Contact < ActiveRecord::Base

  belongs_to :admin
  has_many :contact_actions
  validates :name, :presence => true
  validates :email, :presence => true,  :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  #validates :message, :presence => true
  validates :phone, :presence => true
  
  has_many :contact_files
  
  
  

  def syncro
    require 'net/https'
    require 'open-uri'
    @params = {
      :name => self.name,

      :firstname => self.firstname,

      :corporate => self.corporate,

      :email => self.email,

      :phone => self.phone,

      :message => self.message,
      
      :provenance_id => self.provenance_id
    }
    
    api_url = "https://groupe-payre.fr/admin/contacts/api"
    #api_url ="http://localhost:3030/admin/contacts/api"

    @c = Curl::Easy.new(api_url) do |curl|      
      curl.verbose = true
    end


    @c.http_post(
    Curl::PostField.content(:name, @params[:name]),
    Curl::PostField.content(:firstname, @params[:firstname]),
    Curl::PostField.content(:corporate, @params[:corporate]),
    Curl::PostField.content(:email, @params[:email]),
    Curl::PostField.content(:phone, @params[:phone]),
    Curl::PostField.content(:message, @params[:message]),
    Curl::PostField.content(:provenance_id, @params[:provenance_id])
    
    )

    @debug = @c.body_str
    
     
    
  end

end
