# -*- encoding : utf-8 -*-
class Admin::PrescriptionsController < ApplicationController
  
	before_filter :authenticate_admin!
	load_and_authorize_resource
	

	before_filter :find_prescriptions
	
	layout "admin"
	navigation :prescriptions
	


	def index
		@prescription = @prescriptions[0]
		
	end

	def show
		@prescription = Prescription.find(params[:id])
		@prescription.read = true
		@prescription.save
	end
	
	def print
		
		@prescription = Prescription.find(params[:id])
		render :layout => false
	end

	def delete
	end


	def update
		@prescription = Prescription.find(params[:id])
		
		if params[:prescription][:done_by] and params[:prescription][:done_by] != ""
			params[:prescription][:done_at] =  Time.now
			params[:prescription][:done] =  true 
			
		else
			params[:prescription][:done_by] = nil
		end
		
		
		
		if @prescription.update_attributes(params[:prescription])
			
		else
			
		end
	end

	def done
		@prescription = Prescription.find(params[:id])
		
		if params[:prescription][:done_by] and params[:prescription][:done_by] != ""
			params[:prescription][:done_at] =  Time.now
			params[:prescription][:done] =  true 
			@prescription.update_attributes(params[:prescription])
		end
	end

	def undone
		@prescription = Prescription.find(params[:id])
		params[:prescription] = {}
		params[:prescription][:done_by]= ""
		params[:prescription][:done_at] =  Time.now
		params[:prescription][:done] =  false 
		@prescription.update_attributes(params[:prescription])

	end
	
	def file
		@prescription = Prescription.find(params[:id])
		@file = @prescription.file.path
		
		send_file @file, :disposition => "inline"
	end
	
	def find_prescriptions
		@prescriptions = Prescription.order("'read ASC', 'done ASC', 'created_at DESC'")
		@last = Prescription.order("created_at DESC").first
	end

	def destroy
		@prescription = Prescription.find(params[:id])
		@prescription.destroy
		redirect_to admin_prescriptions_path, :notice => "L'ordonnance à bien été supprimée"
	end

end
