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

local function resp(start,ends)
	local view = template.new("index.html", "layout.html")
	view.start = start
	view.ends = ends
	view.title = "Мои активные чаны"
	view.cellar = sensors.cellar.now()
	view.room = sensors.room.now()
	view.tanks = tanks.active(start,ends)
	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))

