class MEvent < ApplicationRecord
  belongs_to :circuit
  belongs_to :organisateur

  acts_as_sorting :fields => {
    :id => {:name => "Id", :reorder => true},
 
    :m_event_type_title => {:name => "Type d'évenement", :reorder => true},
    :circuit => {:name => "Circuit", :reorder => false},
    :organisateur => {:name => "organisateur", :reorder => false},
    :start_at => {:name => "Date de début", :reorder => true},
    :end_at => {:name => "Date de fin", :reorder => true},
    :title => {:name => "Titre", :reorder => true},
    
    :description => {:name => "Description", :reorder => true},
    :link => {:name => "Lien réservation", :reorder => true},
    
    
    :actions => {:name => "Actions", :reorder => false}
  }

  belongs_to :image_file

  def member_label
    self.start_at.to_date.to_s+" - "+self.title.to_s+" - "+(self.organisateur.name if self.organisateur).to_s
  end


  acts_as_csv_import :fields => [:m_event_type_title, :start_at, :end_at, :title, :description, :link, :encadrant, :imp_circuit_name, :imp_circuit_organisateur ]
  
  
  before_validation do
    if self.imp_circuit_organisateur? and !self.organisateur
      self.organisateur = Organisateur.where(:name => self.imp_circuit_organisateur).first
      
    end
    
    if self.imp_circuit_name? and !self.circuit
      self.circuit = Circuit.where(:name => self.imp_circuit_name).first
      
    end
  end
end
