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

class Admin::NewslettersController < ApplicationController
  layout "admin"
  

  before_filter :auth_admin
  
#  require_permission 'newsletter.show'
#  require_permission 'newsletter.add', :only => [:newsletter_new, :newsletter_create]
#  require_permission 'newsletter.edit', :only => [:newsletter_edit, :newsletter_update]
#  require_permission 'newsletter.delete', :only => [:newsletter_del]
#  require_permission 'newsletter.send', :only => [:send_test, :send_newsletter, :select_recipients]
  

def history_detail
  @archive_newsletter = ArchiveNewsletter.find(params[:id])
  @newsletter = @archive_newsletter.newsletter
end


def history
  @newsletter = Newsletter.find(params[:id])
end
  
  def index
    @h2 = "Liste des newsletters "
   
   
   
	per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 15
	page = (params[:page] and params[:page] != "") ? params[:page] : 1 
	
   
   @newsletter= Newsletter.order("created_at DESC").page(page).per(per_page)
  end
  
  def new
    @h2 = "Creer une newsletter"
    @newsletter = Newsletter.new
    render :layout => false
  end

  def create
    @h2 = "Creer une newsletter"
    
    @newsletter = Newsletter.new(params.require(:newsletter).permit!)
  
    if @newsletter.save
    	per_page = (params[:per_page] and params[:per_page] != "") ? params[:per_page] : 15
    	page = (params[:page] and params[:page] != "") ? params[:page] : 1 
	
   
       @newsletters= Newsletter.order("created_at DESC").page(page).per(per_page)
      #redirect_to :action => :index
    else
      render :action => :new
    end
  end

  def edit
    @h2 = "Editer une newsletter"
    @newsletter = Newsletter.find(params[:id])
  end

  def update
    @h2 = "Editer une newsletter"
    @newsletter = Newsletter.find(params[:id])
    if @newsletter.update_attributes(params.require(:newsletter).permit!)
      flash[:notice]= "La newsletter à été mise à jour"
      
    else
      render :action => :edit
    end
  end

  def show
    @newsletter = Newsletter.find(params[:id])
    @options = {}
    render :layout => "mail"
  end
  
  def destroy
    @newsletter = Newsletter.find(params[:id])
    @newsletter.destroy
    flash[:notice]= "La newsletter à été supprimé avec succès"
    redirect_to :action => :index
    
  end

  
  def send_test
    @errors = []
    @newsletter = Newsletter.find(params[:id])
    #NewsletterMails.newsletter(@newsletter, params[:email]).deliver
    send_helper @newsletter, params[:email]
    
    
    flash[:notice]= "La newsletter a bien été envoyée à l'email de test"
    redirect_to :action => :index
  end
  
  
  def send_helper(newsletter, email, options = {})
    
    if email and email != ""
    
      begin
        @mail_archive = MailArchive.new
        @mail_archive.archive_newsletter_id =  @archive_newsletter.id if @archive_newsletter
        @mail_archive.email = email
        @mail_archive.newsletter_id = newsletter.id
        @mail_archive.sheet = options[:sheet]
        @mail_archive.person = options[:person]
        @mail_archive.save
        
        options = options.merge({:mail_archive => @mail_archive})
        
        
        NewsletterMails.newsletter(newsletter, email, options).deliver if email and email != "" 
        
      
      rescue
        @errors << "<span class'error'>#{email}</span>"
      end
    end
  end

  
  def send_newsletter
    @errors = []
    @newsletter = Newsletter.find(params[:id])
    
    @archive_newsletter = ArchiveNewsletter.create :newsletter_id => @newsletter.id, :title => @newsletter.title, :content => @newsletter.content, :email_from => Newsletter::EMAIL_TEXTES[@newsletter.email_from][0].to_s+ " <#{@newsletter.email_from}>"
    if params[:confirmed_groups]
      PeopleNewsgroup.all(:conditions => {:newsgroup_id => params[:confirmed_groups]}).each do |pn|
        if pn.person
          p =pn.person
          
          send_helper(@newsletter, p.email, :people_newsgroup => pn, :person => p, :sheet => p.sheet) if p.newsletter
        
        end
      end
    elsif params[:confirmed_sheets]
      
      

      @sheets = Sheet.find(:all, :conditions => {:id => params[:confirmed_sheets]})
      
      @persons = Person.find(:all, :conditions => {:sheet_id => params[:confirmed_sheets], :newsletter => true})
      
      @persons.each do |p|
        send_helper(@newsletter, p.email, :person => p, :sheet => p.sheet) 
      end
      
      @sheets.each do |s|
       send_helper(@newsletter, s.other_mail, :sheet => s) 
     end

      
    end
    
    
    flash[:notice]= "La newsletter a bien été envoyée à toutes les adresses mail séléctionnée"
    redirect_to :action => :index
    
  end
  
  def select_recipients
    @newsletter = Newsletter.find(params[:id])
    if params[:sheet_filter]
      @sheets =  Sheet.search_by_params(params[:sheet_filter])
    
    end
    
 
    
      
  end
  


end
