class Contact < ApplicationRecord


  has_many :document_orders
  has_many :data_files, :through => :document_orders
  
  accepts_nested_attributes_for :document_orders

  validates :name, :presence => true
  #validates :firstname, :presence => true
  #validates :civilite, :presence => true
  
  #validates :address, :presence => true, :if => :postal_need
  #validates :cp, :presence => true, :if => :postal_need
  #validates :city, :presence => true, :if => :postal_need
  #validates :country, :presence => true, :if => :postal_need
  
  
  validates :email, :presence => true,  :format => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :if => :email_need
  validates :message, :presence => true, :if => :message_need
  
  def date_rgdp
    date_rgdp = Time.parse("2018-05-25 18:26:00")
    return date_rgdp
  end
  
  def valid_rgpd
    true if Time.now > self.date_rgdp
  end
  
  #validates :rgdp, :presence => {:if => :valid_rgpd}
  
  
  #raison_id 
  # 1 => contact classique
  
  
  def message_need
    true if raison_id != 3 and raison_id != 4 and raison_id != 2
  end
  
  def email_need
    if raison_id == 1
      true
    else
      false
    end
  end
  
  before_validation do
    if (raison_id == 2 or raison_id== 4) and !self.email? and !self.phone?
      errors.add(:email, "Vous devez indiquer votre numéro de téléphone et/ou votre email") 
      errors.add(:phone, "Vous devez indiquer votre numéro de téléphone et/ou votre email") 
      
    end


    
  end
  
  
  def postal_need
    if raison_id == 3 
      true
    else
      false
    end
  end
  
  
  def raison_text
    if self.raison_id == 1
      "Demande de contact générale"
    elsif self.raison_id == 2
      "Demande de contact pour legs, assurance, donation"
      
    elsif self.raison_id == 3
      "Demande de documents"
    
    elsif self.raison_id == 4
      'Demande provenant de la page "Militer"'
    else
      "Demande de contact générale"
    end
  end
  


end
