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

class Admin::MOdrRemisesController < ApplicationController
  layout "admin"
  before_action :auth_admin

  before_action :admin_space
  
  def admin_space
    @admin_space = "payments"
  end
  
  def export_sepa
    @m_odr_remise = MOdrRemise.find(params[:id])
    
    @m_odr = @m_odr_remise.m_odr
    
    @m_odr_virements = @m_odr_remise.m_odr_virements.where(:refused => false)
    

    @prelevement_date = Time.now 
    @iban = @m_odr.iban.to_s
    @bic = @m_odr.bic.to_s
    @name = @m_odr.bank_name.to_s

    
    
    
    params[:inline] = false
    file_path = Rails.root.join("pdf", "sepa", "remise_#{@m_odr_remise.id}.xml")
    if true or !File.exist?(file_path)
      @data_to_send = render_to_string(:layout => "admin/m_odr_remises/sepa.html.erb", :layout => false)
      
      #@data_to_send = "d"
      
      File.open(file_path, "w+") do |f|
        f.write(@data_to_send)
      end
    end

    send_file file_path, :filename =>"export-#{@m_odr_remise.id}-#{Date.today.strftime("%Y-%m-%d")}.xml" , :type => 'text/xml',:disposition => (params[:inline] ? 'inline' : "attachment")
    
  end
  
  
  def confirm
    
    @m_odr_remise = MOdrRemise.find(params[:id])
    @m_odr_remise.m_odr_virements.where(:refused => false).each do |m_odr_virement|
      
      m_odr_virement.sended = true
      m_odr_virement.m_odr_rep.change_state("Virement envoyé")
      m_odr_virement.save
      
      m_odr_virement.m_odr_rep.send_mail_type("succes", m_odr_virement)
      
    end
    @m_odr_remise.confirmed = true
    @m_odr_remise.save
    
    redirect_to [:admin, @m_odr_remise]

  end
  
  
  def generate_all
    
    
    redirect_to admin_m_odr_remises_path
    
    
    @m_odr_virements = MOdrVirement.where(:enabled => true, :m_odr_remise_id => nil)
    
    @m_odr_virements.group(:m_odr_id).each do |m_odr|
      @m_odr_remise = MOdrRemise.new(:m_odr_id => m_odr.m_odr_id)
      m_odr_virements = @m_odr_virements.where(:m_odr_id => m_odr.m_odr_id)
      @m_odr_remise.m_odr_virements << m_odr_virements
      
      @m_odr_remise.save
      
      
      
    end
    
  end
  
  
  def index
    @m_odr_remises = MOdrRemise.includes(:m_odr)
    
    @m_odr_remises = sort_by_sorting(@m_odr_remises, "created_at DESC")
    
    params[:search][:per_page] = params[:search][:per_page] || 50
		per_page = params[:search][:per_page]
		page = (params[:page] and params[:page] != "") ? params[:page] : 1 
		@m_odr_remises = @m_odr_remises.page(page).per(per_page)
    

  end

  def show
    @m_odr_remise = MOdrRemise.find(params[:id])

  end

  def new
    @m_odr_remise = MOdrRemise.new
  
  end

  def edit
    @m_odr_remise = MOdrRemise.find(params[:id])
    
  end

  def create
    @m_odr_remise = MOdrRemise.new(params.require(:m_odr_remise).permit!)

    if @m_odr_remise.save
      
    else
      render action: "new"
 
    end

  end


  def update
    @m_odr_remise = MOdrRemise.find(params[:id])


    if @m_odr_remise.update_attributes(params.require(:m_odr_remise).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @m_odr_remise = MOdrRemise.find(params[:id])
    @m_odr_remise.destroy
    
  end
end
