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

class Admin::VBdasController < ApplicationController
  layout "admin"
  before_action :auth_admin, :except => [:ba, :ba_pdf]

  before_action :admin_space
  
  def admin_space
    @admin_space = "cartes"
  end
  
  def index
    @v_bdas = VBda.all
    
    if params[:v_contact_id].to_s != ""
      @v_bdas = @v_bdas.where(:v_contact_id => params[:v_contact_id])
    end
    
    @v_bdas = @v_bdas.where(:societe_id => current_admin.societes.ids)
    
    @v_bdas = sort_by_sorting(@v_bdas, "id DESC")
    respond_to do |format|
      format.html{
      
        params[:search][:per_page] = params[:search][:per_page] || 100
        per_page = params[:search][:per_page]
        page = (params[:page] and params[:page] != "") ? params[:page] : 1 
        @v_bdas = @v_bdas.page(page).per(per_page)
      
      }
      format.csv { 
          @headers = []
          @columns = []
          @exclude = (!current_admin.super_admin ? [:enabled, :id, :used, :societe_id, :seuil, :email, :sended] : [])
          VBda.qi_table_order.each do |key, value|
          
            if value.instance_of? Hash
              name = value[:name]
            else
              name = value
            end
          
            if name != "Actions" and !@exclude.include?(key.to_sym)
              @headers << name.to_s
              @columns << key
            end
          
          end
    
    
    
          xlsx_package = Axlsx::Package.new
            wb = xlsx_package.workbook
            wb.add_worksheet(name: "import") do |sheet|
              sheet.add_row @headers
              
              @v_bdas.each do |v_bda|
                line = []
            
                @columns.each do |column|
              
                  if (v_bda.respond_to?("csv_"+column.to_s))
                    line << v_bda.send("csv_"+column.to_s)
                  elsif (v_bda.respond_to?(column))
                    if v_bda.send(column.to_s).class.to_s == "BigDecimal"
                      line << v_bda.send(column.to_s).to_s.gsub('.', ',')
                    else
                      line << v_bda.send(column.to_s)
                    end
                  else
                    line << column.to_s
                  end
              
                end
            
                
                sheet.add_row line, types: line.map{|t| self.cell_type_from_value(t)}
              end
              
              
            end
            
            @final_file = "#{Rails.root}/private_medias/export-remises-#{Time.now.to_s.to_slug}.xlsx"
      

            xlsx_package.serialize(@final_file)  
  
        
            send_file @final_file
        
  
  
        }
      
    end
      
    
  end

  def show
    @v_bda = VBda.find(params[:id])

  end

  def new
    @v_bda = VBda.new
  
  end

  def edit
    @v_bda = VBda.find(params[:id])
    
  end

  def create
    @v_bda = VBda.new(params.require(:v_bda).permit!)

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

  end


  def update
    @v_bda = VBda.find(params[:id])


    if @v_bda.update_attributes(params.require(:v_bda).permit!)
      
    else
     render action: "edit" 
      
    end
      
  end
  
  def send_mail
    @v_bda = VBda.find(params[:id])
    
    @v_bda.send_mail_type("envoi_bda")
    
    render action: "update" 
  end


  def destroy
    @v_bda = VBda.find(params[:id])
    @v_bda.destroy
    
  end
  
  def ba
    @v_bda = VBda.find(params[:id])
    #@v_bda.generate_ba if !params[:print]
    render :layout => false
  end
  
  def ba_pdf
    @v_bda = VBda.find(params[:id])
    
    @v_bda.generate_pdf if !params[:print]
    
    #@v_bda.generate_ba if !params[:print]
    render :layout => false
  end
end
