local template = require "resty.template" 
local handle = require('framework.handle')
local sensors = require('database.sensors')

local function resp(start,ends)
	local view = template.new("climate.html", "layout.html")
	view.start = start
	view.ends = ends
	view.title = "Микроклимат подвала"
	view.data = sensors.cellar.data(start,ends)
	view.cellar = sensors.cellar.now()
	view.room = sensors.room.now()
	view.weather = sensors.weather.now()
	view:render()
end

local request = {
    POST = function()  
		ngx.req.read_body()	
		local args, err = ngx.req.get_post_args()
		resp(string.match(args['datetimes'], "([%d-%s:]+)%s>%s([%d-%s:]+)"))
    end,   
    GET = function()
		local ends = os.date('%Y-%m-%d %H:%M:%S',os.time()+14400)
		local start = os.date('%Y-%m-%d %H:%M:%S',os.time()-86400)
		resp(start,ends)
	end
}

ngx.say(handle(request))

