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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "cartes"
  end
  
  def index
    @v_contacts = VContact.all
    
    params[:search][:enabled] = "Oui" if !params[:search][:enabled]
    
    if params[:search][:enabled].to_s == "Oui"
      @v_contacts = @v_contacts.where(:enabled => true)
    elsif params[:search][:enabled].to_s == "Non"
      @v_contacts = @v_contacts.where(:enabled => false)
    end
    
    
    
    if params[:code].to_s != ""
      @v_contacts = @v_contacts.where("codemanaginn LIKE ?","%#{params[:code]}%")
    end

    if params[:name].to_s != ""
      @v_contacts = @v_contacts.where("nom LIKE ?","%#{params[:name]}%")
    end
    
    if params[:firstname].to_s != ""
      @v_contacts = @v_contacts.where("prenom LIKE ?","%#{params[:firstname]}%")
    end

    if params[:city].to_s != ""
      @v_contacts = @v_contacts.where("ville LIKE ?", "%#{params[:city]}%")
    end
    
    if params[:cp].to_s != ""
      @v_contacts = @v_contacts.where("codepostal LIKE ?", "%#{params[:cp]}%")
    end
    
    
    if !current_admin.super_admin
      @v_contacts = @v_contacts.where(:codeproprietaire => current_admin.societes.pluck(:socmanaginn))
    end
    
    if params[:search][:codeproprietaire].to_s != ""
      if params[:search][:codeproprietaire].to_s == "null"
        @v_contacts = @v_contacts.where(:codeproprietaire => nil)
      else
        @v_contacts = @v_contacts.where(:codeproprietaire => params[:search][:codeproprietaire])
      end
    end
    

    @v_contacts = sort_by_sorting(@v_contacts, "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_contacts = @v_contacts.page(page).per(per_page)
      
      }
      format.csv { 
          @headers = []
          @columns = []
          @exclude = (!current_admin.super_admin ? [:enabled, :id, :codeproprietaire, :corbeille, :datecreation,:v_societe] : [] )
          VContact.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_contacts.each do |v_contact|
                line = []
            
                @columns.each do |column|
              
                  if (v_contact.respond_to?("csv_"+column.to_s))
                    line << v_contact.send("csv_"+column.to_s)
                  elsif (v_contact.respond_to?(column))
                    if v_contact.send(column.to_s).class.to_s == "BigDecimal"
                      line << v_contact.send(column.to_s).to_s.gsub('.', ',')
                    else
                      line << v_contact.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-cartes-#{Time.now.to_s.to_slug}.xlsx"
      

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

  def show
    @v_contact = VContact.find(params[:id])

  end
  
  def generate_ba
    @v_contact = VContact.find(params[:id])
    @v_contact.generate_ba
    
    redirect_back :fallback_location => "/"
  end

  def new
    @v_contact = VContact.new(:codeproprietaire => (params[:codesociete] ? params[:codesociete] : (current_admin.societe ? current_admin.societe.socmanaginn : "")))
  
  end

  def edit
    @v_contact = VContact.find(params[:id])
    
  end

  def create
    @v_contact = VContact.new(params.require(:v_contact).permit!)

    if @v_contact.save
      @societe = @v_contact.societe
    else
      render action: "new"
 
    end

  end


  def update
    @v_contact = VContact.find(params[:id])


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


  def destroy
    @v_contact = VContact.find(params[:id])
    @v_contact.destroy
    
  end
  
  def autocomplete
    @v_contacts = VContact
    
    result = []
  
    if !current_admin.super_admin
      @v_contacts = @v_contacts.where(:codeproprietaire => current_admin.societes.pluck(:socmanaginn))
    end
    
    @v_contacts = @v_contacts.where("codemanaginn LIKE ? or nom LIKE ?","%#{params[:search]}%", "%#{params[:search]}%")

  
    
    @v_contacts.all.each do |v_contact|
      result << {:member_label => v_contact.member_label, :id => v_contact.id }
    end
    
    respond_to do |format|
        format.json { render json: result}
    end
  end
end
