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

  def reorder
    i = 0
    params[:order].each do |p_product_image_id|
      i += 1
      p_product_image = PProductImage.find(p_product_image_id)
      p_product_image.position = i
      p_product_image.save
    end
    
  end

  def create
    
    @product = PProduct.find(params[:p_product_id])
    @p_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)
        p_product_image = PProductImage.create(:image_file_id => image_file.id, :name => image_file.name, :description  => image_file.description, :p_product_id => @product.id )
        
        @p_product_images << p_product_image
      end
    end
    
  

    respond_to do |format|
    
        format.js
    
    end
  end
  
  

  def edit
    @p_product_image = PProductImage.find(params[:id])

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



  def show


  end

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

    end
  end

  def destroy
    
    @p_product_image = PProductImage.find(params[:id])
    @p_product_image.destroy
    
  
  end

 
end
