# -*- encoding : utf-8 -*-
class Admin::ProductImagesController < ApplicationController
  layout "admin"
  

  def reorder
    i = 0
    params[:order].each do |product_image_id|
      i += 1
      product_image = ProductImage.find(product_image_id)
      product_image.position = i
      product_image.save
    end
    
  end

  def create
    
    @product = Product.find(params[:product_id])
    @product_images = []
    if params[:image_file_ids].kind_of?(Array)
      params[:image_file_ids].each do |image_file_id|
        image_file = ImageFile.find(image_file_id)
        product_image = ProductImage.create(:image_file_id => image_file.id, :title => image_file.name, :description  => image_file.description, :product_id => @product.id )
        
        @product_images << product_image
      end
    end
    
  

    respond_to do |format|
    
        format.js
    
    end
  end
  
  

  def edit
    @product_image = ProductImage.find(params[:id])

    if request.xhr?
      render :layout => false
    end
    
  end
  
  



  def show


  end

  def update
    @product_image = ProductImage.find(params[:id])
    respond_to do |format|
          if @product_image.update_attributes(params.require(:product_image).permit!)
        
            format.js 
          else
            format.html { render :action => "edit", :layout => false} 
        format.js { render :action => "edit" }
          end

    end
  end

  def destroy
    
    @product_image = ProductImage.find(params[:id])
    @product_image.destroy
    
  
  end

 
end
