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

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

  def index
    @petitions = Petition.all

    @petitions.all.each do |p|
      if !p.slug?
        p.slug = p.name.to_slug
        p.save
      end
    end
  end
  
  def chine
    
  end
  
  def show
    @petition = Petition.find(params[:id])

  end

  def new
    
    
		@petition = Petition.new
    
    
    LangSite.all.each do |ls|
      @petition.petition_langs << PetitionLang.new(:lang_site_id => ls.id)
    end

  end

  def reconfirm
    @signator = PetitionSignator.find(params[:id])
    PetitionMails.confirmation(@signator).deliver
    redirect_to admin_petition_url(@signator.petition), :notice => "mail de confirmation envoyé de nouveau."
  end

  def edit
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
  
    
    @petition = Petition.find(params[:id])
  end

  def create
    @petition = Petition.new(params.require(:petition).permit!)


    if @petition.save
     redirect_to admin_petitions_url, notice: 'La pétition a été crée.' 
      
    else
      render action: "new"
 
    end

  end


  def update
    @petition = Petition.find(params[:id])


    if @petition.update_attributes(params.require(:petition).permit!)
      redirect_to admin_petitions_url, notice: 'Les infos sur la pétition ont été mise à jour.' 
     
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @petition = Petition.find(params[:id])
    @petition.destroy
    
    redirect_to admin_petitions_url

  end
end