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

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

  before_action :admin_space
  
  def admin_space
    @admin_space = "preferences"
  end
  
  def index
    @import_csvs = ImportCsv.all

    @import_csvs = sort_by_sorting(@import_csvs, "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 
        @import_csvs = @import_csvs.page(page).per(per_page)
      
      }
    end
  end

  def show
    @import_csv = ImportCsv.find(params[:id])

  end

  def charge
    @import_csv = ImportCsv.find(params[:id])
    @import_csv.load

    redirect_to :action => :index
  end

  def new
    @import_csv = ImportCsv.new(:parent_id => params[:parent_id])
    if @import_csv.parent
      @import_csv.name = @import_csv.parent.name 
      @import_csv.table_name = @import_csv.parent.table_name 

    end
  
  end

  def edit
    @import_csv = ImportCsv.find(params[:id])
    
  end

  def create
    @import_csv = ImportCsv.new(params.require(:import_csv).permit!)

    if @import_csv.save
      redirect_to :action => :index
    else
      render action: "new"
 
    end

  end


  def update
    @import_csv = ImportCsv.find(params[:id])


    if @import_csv.update_attributes(params.require(:import_csv).permit!)
      redirect_to :action => :index
    else
     render action: "edit" 
      
    end
      
  end


  def destroy
    @import_csv = ImportCsv.find(params[:id])
    @import_csv.destroy
    
  end
end
