class Filters	< ApplicationController		
		# Фильтр по сотрудникам
		def employees(employees)
			
			# работа с параметрами
			#if(params && params[:empl]) 
			#	if(params[:empl].to_i>0) then employee_selected = params[:empl] else employee_selected = '0' end
			#else
			#	employee_selected = '0'
			#end
			
			workParams = ParamsWork.new
			employee_selected = workParams.setParams("empl")
			
			
			# Сам фильтр
			filter = "<select name='empl'>"
			filter += "<option value='0' SELECTED>Все</option>"
			employees.each {|key, value|
					if(employee_selected.to_s == key.to_s) then selected ='SELECTED' else selected='' end
					filter += "<option value="+key.to_s+" "+selected+">"+value.to_s+"</option>"
			   }
				filter += "</select>"
				return filter.to_s
		end
		
		
		
		# Фильтр по дням
		def days
			time = Time.new
			
			nowDay = time.day
			nowMonth = time.month
			nowYear = time.year
			maxDays = Time.days_in_month(nowMonth, nowYear)
				
			filter = "<select name='day'>"
			i=1
			while i <= maxDays
				day_num = i.to_s
				if(day_num == nowDay.to_s) then selected ='SELECTED' else selected='' end
				filter += "<option value="+day_num+" "+selected+">"+day_num+"</option>"
				i+=1
			end
			filter += "</select>"
			
			return filter.to_s
			
		end
		
		
		# Фильтр по неделям
		
		
		
		# Фильтр по месяца
		def months
			time = Time.new
			
			nowDay = time.day
			nowMonth = time.month
			nowYear = time.year
			maxMonths = 12
			
			months = {}
			months[1] = "Январь"
			months[2] =	"Февраль"
			months[3] =	"Март"
			months[4] =	"Апрель"
			months[5] =	"Май"
			months[6] =	"Июнь"
			months[7] =	"Июль"
			months[8] =	"Август"
			months[9] =	"Сентябрь"
			months[10] = "Октябрь"
			months[11] = "Ноябрь"
			months[12] = "Декабрь"
			
				
			filter = "<select name='month'>"
			i=1
			while i <= maxMonths
				month_num = i.to_s
				if(month_num == nowMonth.to_s) then selected ='SELECTED' else selected='' end
				filter += "<option value="+month_num+" "+selected+">"+months[month_num.to_i]+"</option>"
				i+=1
			end
			filter += "</select>"
			
			return filter.to_s
			
		
		end
		
		# Фильтр по годам
		def years(department)	
			time = Time.new
			
			nowYear = time.year
			
			query = ReportsDB.find_by_sql("SELECT date FROM reports WHERE department='"+department.to_s+"' ORDER BY id ASC LIMIT 1")
			if(query[0]!=nil)
				minYear = query[0]['date'].sub(/^.*([0-9]{4,4}).*$/, '\1').to_i
			else
				minYear = nowYear
			end
			
			filter = "<select name='year'>"
			i=minYear
			while i <= 2012
				year_num = i.to_i
				if(year_num == nowYear.to_i) then selected ='SELECTED' else selected='' end
				filter += "<option value="+year_num+" "+selected+">"+year_num+"</option>"
				i+=1
			end
			filter += "</select>"
			
			
			 
			
			return filter.to_s
			
		end
		
		# Фильтр по типу работ
		def workType
			
			# работа с параметрами
			if(params && params[:worktype]) 
				if(params[:worktype].to_i>0) then worktype_selected = params[:worktype].to_s else worktype_selected = '0' end
	
			else
				
				#worktype_selected = session[:worktype]
				worktype_selected = '0'
			end


			
			filter = "<select name='worktype'>"
			i=0
			max = $category_info.length
			while i < max
				workType_num = i.to_s
				workType_name = $category_info[i]['name']
				if(workType_num == worktype_selected) then selected ='SELECTED' else selected='' end
				filter += "<option value="+workType_num+" "+selected+">"+workType_name+"</option>"
				i+=1
			end
			filter += "</select>"
			
			return filter.to_s
		
		
		
		end
		
		
		
end


class ParamsWork

	def setParams(param)
		#setParam = ":"+param+"".to_sym
		#cookie_name = ":filter_"+param+"".to_sym
		
		if(params && params[setParam]) 
				selected = params[setParam]
				cookies[cookie_name] = { :value => params[cookie_name], :expires => 30.days.from_now }
		elsif(cookies[:filter_empl])
				selected = cookies[:filter_worktype]
		else
				selected = '0'
		end
			
		return selected
	end
end
