# -*- encoding : utf-8 -*-
class Admin::AlbumFoldersController < ApplicationController
   before_filter :auth_admin

  
  layout "admin"
  
  
  
  def find_album_folders

    
    params[:parent_id] = nil if !params[:parent_id] or params[:parent_id] == ""
      
    @album_folder_parent = AlbumFolder.find(params[:parent_id]) if params[:parent_id]
    
    
    @album_folders = AlbumFolder.where(:parent_id => params[:parent_id])
    

  
  end
  
  
  def reorder

    i = 1
    
    
    params[:album_folders_ids].each do |id|
      

      album_folder = AlbumFolder.find(id)
      album_folder.position = i

      album_folder.skip_index = true
      album_folder.save
      
      i = i + 1
      
      
    end
    
    if params[:menu_id] and params[:menu_id] != "" and @menu = Menu.find(params[:menu_id])
      
      find_album_folders
    end
    
    flash[:success] = ""
    
   
    
  end
  
  def index
  

     
      find_album_folders
      
      
    
  
       

   
    
    
  end

  def new

    
    
    @album_folder = AlbumFolder.new(:parent_id => params[:parent_id])
    
   
    
    
  end

  def create
    
    @album_folder= AlbumFolder.new(params.require(:album_folder).permit!)
   
    
    respond_to do |format|
      if @album_folder.save
        
        params[:parent_id] = @album_folder.parent_id
        find_album_folders
        
        
        flash[:notice] = "Le album_folder a été ajouté"
        format.html { 
          redirect_to(admin_album_folders_path(:menu_id => @album_folder.menu_id, :parent_id => @album_folder.parent_id)) 
          }
        format.js
        
      else
        
        format.html { render :action => "new" }
        format.js { render :action => "new" }
      end
    end
  
  end

  def edit

    @album_folder= AlbumFolder.find(params[:id])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end
  
  def edit_articles

    @album_folder= AlbumFolder.find(params[:id])
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    
    @admin = true
  end

  def update
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    @album_folder= AlbumFolder.find(params[:id])
  
    
    if request.xhr?
      @album_folder_parent = @album_folder.parent if @album_folder.parent
      @album_folders = AlbumFolder.where(:parent_id => @album_folder.parent_id)
    end
  
    if params[:album_folder][:parent_id] and params[:album_folder][:parent_id] == "no-menu-selected"
      @album_folder.parent_id = nil
      @album_folder.save
      params[:album_folder].delete(:parent_id)
      
    end
    
    @reorder = true if params[:reorder]
    
    
    
    
  
    
    
    respond_to do |format|
      if @album_folder.update_attributes(params.require(:album_folder).permit!)
        
        
      
        flash[:notice] = "Le album_folder à été modifié avec succès."
        format.html { redirect_to(admin_album_folders_path(:parent_id => @album_folder.parent_id)) }
        if @reorder
          
          
          format.js { 
            
            render :action => "update" }
        else
         
          format.js { 
             @album_folder = AlbumFolder.find(@album_folder.id)
            render :action => "show" }
        end
      else
        
        flash[:error] = "Ce album_folder n'a pas pu être déplacé."
        if @reorder
          format.js { render :action => "update_reorder_failled" }
        else
          format.html { render :action => "edit" }
          format.js { render :action => "edit" }
        end
        
        
      end
    end
    
  end

  def destroy
    
    params[:lang] = params[:lang] || "fr"
    
    @lang = LangSite.find_by_slug(params[:lang])
    
    @album_folder = AlbumFolder.find(params[:id])
    @album_folder.destroy
    flash[:notice] = "AlbumFolder supprimé"
    respond_to do |format|
    
        
        format.html { redirect_to(admin_album_folders_path(:menu_id => @album_folder.menu_id, :parent_id => @album_folder.parent_id)) }
        format.js
    
  
    end
  end

  def show
    @album_folder= AlbumFolder.find(params[:id])
  end
  
  
  
  

end
