class Public::VirementsController < ApplicationController
  layout "public"
  
  before_filter :auth_product_customer
  

  
  def index


    
    @virements = current_product_customer.virements.all
    
    
  end


  def show
    @virement = current_product_customer.virements.find_by_token(params[:id])
    
      @temp_file = "#{Rails.root}/pdf/virements/#{@virement.token}_temp.pdf"
      @final_file = "#{Rails.root}/pdf/virements/#{@virement.token}.pdf"
    
    
      view = ActionView::Base.new(Rails.root.join('app/views'))
      view.class.include ApplicationHelper
      view.class.include Rails.application.routes.url_helpers

      pdf = view.render( 
              :pdf => "#{@virement.token}",
              :template => "public/virements/show.html.haml",  
              
              :locals => {:@virement => @virement, :current_product_customer => current_product_customer})
                                          
      # then save to a file
      pdf = WickedPdf.new.pdf_from_string(pdf, :margin =>  {   top:               0,                     # default 10 (mm)
                                          bottom:            0,
                                          left:              0,
                                          right:             0 })

      save_path = @temp_file
      File.open(save_path, 'wb') do |file|
      file << pdf
      end
      
      
      
      require 'posix/spawn'
     
      ::POSIX::Spawn::Child.new 'pdftk', @temp_file, 'background', Rails.root.join('pdf_stamp', 'orders.pdf').to_s, 'output',  @final_file
    
      #pdftk /Users/nico/Dev/sideplace_app.old/pdf/order_bills/SP20150700003.pdf background fond.pdf output sortie.pdf
      
      
      
      
      @data_to_send = File.open( @final_file).read
      
      send_data @data_to_send, :filename =>"note-commission.pdf" , :type => 'application/pdf',:disposition => (params[:inline] ? 'inline' : "attachment")
  
  
  end


  def new
    @no_search = true
    @devise = Devise.first
    @virement = current_product_customer.virements.new(:amount_ht => current_product_customer.max_virement, :devise => @devise)
    
    
  end

  def edit
    @virement = current_virement_account.virements.find(params[:id])
  
  end



  def create

    
  	@virement = current_product_customer.virements.new(params.require(:virement).permit!)

    
    
    
    if @virement.valid?
      if params[:confirm]
        token = @virement.generate_token
        @commission = current_product_customer.commissions.new(:commission_type_id => 4, :amount => ((@virement.amount_ht+5)*(-1)), :devise => @virement.devise)
        @virement.commission = @commission
        if @virement.save
          
          redirect_to public_commissions_path
        else
          render :inline => "test"
        end
      else
        render :action => "confirm" 
      end
      
      
      

          
  	else
  		render :action => "new" 
  	end
  end

  
  def update
    @virement = current_product_customer.virements.find(params[:id])
   
    

    
    respond_to do |format|
      if @virement.update_attributes(params.require(:virement).permit!)



        format.html { 
          redirect_to public_my_account_path
          }
        format.js
        
      else
        
        format.html { render :action => "edit" }
        format.js { "" }
      end
    end
    
    
      
  end
  		


  def destroy

    	 @virement = current_virement_account.virements.find(params[:id])
  	   @virement.enabled = nil
       @virement.save
       
       redirect_to public_my_account_path, :notice => "Votre virement a bien été désactivée"

    

  end



end
