# -*- encoding : utf-8 -*-
  class Admin::ImageActusController < ApplicationController
  before_action :auth_admin
  layout "admin"


  def index
    if params[:image_actu_album_id]
      params[:image_actu_album_id] = params[:image_actu_album_id]
    elsif session[:image_actu_album_id] and ImageActuAlbum.where(:id => session[:image_actu_album_id]).first
      params[:image_actu_album_id] = session[:image_actu_album_id]
    else
      params[:image_actu_album_id] = 1
    end

    @image_actu_album = ImageActuAlbum.find(params[:image_actu_album_id])
    session[:image_actu_album_id] = @image_actu_album.id
    @image_actu_albums = ImageActuAlbum.all.order(:name)
    @image_actus = ImageActu.where(:image_actu_album_id => @image_actu_album.id).order("created_at DESC")
    @image_actu = ImageActu.new(:image_actu_album_id => @image_actu_album.id)
    if request.xhr?
    render :layout => false
    end
  end

  def new
  end

  def edit
    @image_actu = ImageActu.find(params[:id])
  end

  def create
    @image_actu_create = true
    @image_file = ImageFile.new(:album_id => 40, :file =>params[:files])
    
    
    
    
    if @image_file.save
      @image_actu = ImageActu.new(:image_actu_album_id => params[:image_actu_album_id], :image_file_id => @image_file.id, :published_at => Time.now)
    
      LangSite.all.each do |ls|
        @image_actu.image_actu_langs << ImageActuLang.new(:lang_site_id => ls.id)
      end
      if @image_actu.save
        
      else
        
      end
    
    else

    end


  end

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

    else
  
    end
  end

  def destroy
    @image_actu = ImageActu.find(params[:id])
    @image_actu.destroy

    flash[:notice] = "L'image à bien été supprimée."
    
    respond_to do |format|
      format.html{
        redirect_to :back
      }
      format.js{
        
        
      } 
    end
  end

  def show
    @image_actu = ImageActu.find(params[:id])

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

end
