# -*- encoding : utf-8 -*-

class Public::PetitionSignatorsController < ApplicationController
  layout "public"


  def create
    @signator = PetitionSignator.new(params.require(:petition_signator).permit!)

    if params[:lang]
      
      @lang = LangSite.find_by_slug(params[:lang])
      
    end
    @petition_lang = PetitionLang.where(:lang_site_id => @lang.id).find_by_slug(params[:slug])
		@petition = @petition_lang.petition
    @signator.lang_site = @lang
    if @signator.save
      PetitionMails.confirmation(@signator).deliver
      #redirect_to thank_public_petition_signator_url(:id => @signator.token)
    else
      render action: "new"
 
    end

  end

  def thank
    
    @signator = PetitionSignator.find_by_token(params[:id])
      
    @lang = @signator.lang_site

    @petition = @signator.petition
    @petition_lang = @petition.petition_langs.where(:lang_site_id => @lang.id).first
		
    
  end

  def confirm
    @signator = PetitionSignator.find_by_token(params[:id])
    @petition = @signator.petition
    #@title = "Petition - "+@petition.name
    if !@signator.enabled
      @signator.enabled = true

      @signator.save(:validate => false)
    
      PetitionMails.thanks(@signator).deliver
      
    end
      
  end


end
