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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "stocks"
  end
  
  def index
    @organisateurs = Organisateur.all

    @organisateurs = sort_by_sorting(@organisateurs, "name ASC")
    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 
        @organisateurs = @organisateurs.page(page).per(per_page)
      
      }
      
      
      format.csv { 
          @headers = []
          @columns = []
          Organisateur.qi_table_order.each do |key, value|
          
            if value.instance_of? Hash
              name = value[:name]
            else
              name = value
            end
          
            if name != "Actions"
              @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
              
              @organisateurs.each do |p_customer|
                line = []
            
                @columns.each do |column|
              
                  if (p_customer.respond_to?("csv_"+column.to_s))
                    line << p_customer.send("csv_"+column.to_s)
                  elsif (p_customer.respond_to?(column))
                    if p_customer.send(column.to_s).class.to_s == "BigDecimal"
                      line << p_customer.send(column.to_s).to_s.gsub('.', ',')
                    else
                      line << p_customer.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-organisateurs-#{Time.now.to_s.to_slug}.xlsx"
      

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

  def show
    @organisateur = Organisateur.find(params[:id])

  end

  def new
    @organisateur = Organisateur.new
  
  end

  def edit
    @organisateur = Organisateur.find(params[:id])
    
  end

  def create
    @organisateur = Organisateur.new(params.require(:organisateur).permit!)

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

  end


  def update
    @organisateur = Organisateur.find(params[:id])


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


  def destroy
    @organisateur = Organisateur.find(params[:id])
    @organisateur.destroy
    
  end

  def autocomplete
    
    search = params[:search]
    @organisateurs = Organisateur.where("name LIKE ? or city LIKE ?", "%#{search}%", "%#{search}%")
    
    @organisateurs = @organisateurs.distinct.limit(50)
    
    
    
    
    respond_to do |format|
        format.json { render json: @organisateurs.map{|a| {:id => a.id, :member_label => a.member_label}} }
    end
  end
end
