# -*- encoding : utf-8 -*-
class Admin::PromotionsController < ApplicationController
  
  	before_filter :authenticate_admin!
	load_and_authorize_resource
	
	layout "admin"

	navigation :promotions

	def index
		@promotions = Promotion.order("month DESC, title ASC")

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

	def new

		@promotion = Promotion.new(:month => Date.today.beginning_of_month, :promotion_type_id => 1)
	end

	def create
		@promotion = Promotion.new(params[:promotion])
		@promotions = Promotion.order("month DESC, title ASC")
		
		if @promotion.save
			flash[:notice] = "La promotion à bien été créé."
			
			respond_to do |format|
				format.js
			end
			
		else
			
			respond_to do |format|
				format.js { render :action => :new}
			end
					
		end
		
	end

	def update
		
		@promotion = Promotion.find(params[:id])
		@promotions = Promotion.order("month DESC, title ASC")
		if @promotion.update_attributes(params[:promotion])
			flash[:notice] = "La promo à bien été modifiée."

			respond_to do |format|
				format.js
			end

		else
			respond_to do |format|
				format.js { render :action => :edit}
			end		
		end
		
	end

	def edit
		@promotion = Promotion.find(params[:id])
	end

	def show
		@promotion = Promotion.find(params[:id])
	end

	def destroy
		@promotion = Promotion.find(params[:id])
		@promotion.destroy


		flash[:notice] = "La promo à bien été supprimée."
	end

end
