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

  skip_before_action :verify_authenticity_token, only: [:create]
  
  def index
    if params[:album_id]
      params[:album_id] = params[:album_id]
    elsif session[:album_id]
      params[:album_id] = session[:album_id]
    else
      params[:album_id] = 1
    end

    @album = Album.find(params[:album_id])
    session[:album_id] = @album.id
    @albums = Album.all.order(:name)
    @image_files = ImageFile.where(:album_id => @album.id).order("created_at DESC")
    @image_file = ImageFile.new(:album_id => @album.id)
    if request.xhr?
    render :layout => false
    end
    
  end
  
  def new
  end

  def edit
    @image_file = ImageFile.find(params[:id])
  end

  def create
    @image_file_create = true
    @image_file = ImageFile.new(:album_id => params[:album_id], :file =>params[:file])
    
    if @image_file.save

    else

    end


  end

  def update
    @image_file = ImageFile.find(params[:id])
    if @image_file.update_attributes(params.require(:image_file).permit!)

    else
  
    end
  end

  def destroy
    @image_file = ImageFile.find(params[:id])
    @image_file.destroy

    flash[:notice] = "L'image à bien été supprimée."
  end

  def show
    @image_file = ImageFile.find(params[:id])

  end
  
  def rotate
    deg = params[:direction] == "right" ? -90 : 90
    
    @image_file = ImageFile.find(params[:id])
    @image_file.rotate(deg)
  end
end
