class Public::HipayVirementsController < ApplicationController
  layout "public"
  
  before_filter :auth_annonce_account
  

  
  def index


    
    @hipay_virements = current_annonce_account.hipay_virements.all
    
    
  end


  def show
    @hipay_virement = current_annonce_account.hipay_virements.find_by_token(params[:id])
    
      @temp_file = "#{Rails.root}/pdf/hipay_virements/#{@hipay_virement.token}_temp.pdf"
      @final_file = "#{Rails.root}/pdf/hipay_virements/#{@hipay_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 => "#{@hipay_virement.token}",
              :template => "public/hipay_virements/show.html.haml",  
              
              :locals => {:@hipay_virement => @hipay_virement, :current_annonce_account => current_annonce_account})
                                          
      # 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
    params[:devise_id] = params[:devise_id] || 1
    @devise = Devise.find(params[:devise_id])
    @hipay_virement = current_annonce_account.hipay_virements.new(:amount_ht => current_annonce_account.max_hipay_virement(@devise.id), :devise => @devise)
    
    
  end

  def edit
    @hipay_virement = current_hipay_virement_account.hipay_virements.find(params[:id])
  
  end



  def create

    @no_search = true
  	@hipay_virement = current_annonce_account.hipay_virements.new(params.require(:hipay_virement).permit!)

    
    params[:devise_id] = @hipay_virement.devise_id
    @devise = Devise.find(params[:devise_id])
    
    if @hipay_virement.valid?
      if params[:confirm]
        token = @hipay_virement.generate_token
        @commission = current_annonce_account.commissions.new(:commission_type_id => 14, :amount => ((@hipay_virement.amount_ht+5)*(-1)), :devise => @hipay_virement.devise)
        @hipay_virement.commission = @commission
        if @hipay_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
    @hipay_virement = current_annonce_account.hipay_virements.find(params[:id])
   
    

    
    respond_to do |format|
      if @hipay_virement.update_attributes(params.require(:hipay_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

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

    

  end



end
