# -*- coding: utf-8 -*-
import web
from common import Sesion
import datetime
from view import render

urls = (
  "", "reuser",
  "/", "dashboard",
  "/eventos", "eventos",
  "/search", "search",
  "/reportday", "reportday",
  "/reporthistoric", "reporthistoric",
  "/printreportday", "printreportday",
  # JSON
  "/alleventjson", "alleventjson",
  "/reportdayjson", "reportdayjson",
  "/listeventjson", "listeventjson",
  "/listvehiclesjson", "listvehiclesjson",
)


class reuser:
    def GET(self):
        raise web.seeother('/')


class dashboard:
    @Sesion
    def GET(self):
        from db import listingVehicleClient
        # return render.user.monitoreo(web.ctx.path, web.ctx.session)
        return render.user.dashboard(
            web.ctx.session, listingVehicleClient(
                web.ctx.session.clienteId), datetime)


class eventos:
    @Sesion
    def GET(self):
        # web.ctx.session.logged_in = True
        # raise web.seeother('/')

        # return eventos
        # return render.user.eventos(web.ctx.session)
        # from db import listingVehicleClient
        # return render.user.eventos(
        # web.ctx.session, listingVehicleClient(web.ctx.session.clienteId))
        from db import eventsClient
        return render.user.eventos(
            web.ctx.session, eventsClient(web.ctx.session.clienteId))


class search:
    @Sesion
    def GET(self):
        from db import listingVehicleClient
        return render.user.search(
            web.ctx.session, listingVehicleClient(web.ctx.session.clienteId))


class reportday:
    @Sesion
    def GET(self):
        from db import listingVehicleClient
        return render.user.reportday(
            web.ctx.session, listingVehicleClient(web.ctx.session.clienteId))


class printreportday:
    @Sesion
    def GET(self):  # Pasarlo a POST
        """http://127.0.0.1:8080/user/printreportday?carid=9&date=04-02-2013
        http://0.0.0.0:8080/user/printreportday?carid=427&date=23-10-2018
        """
        i = web.input(carid=None, date=None)
        #from db import reportday, listVehicle, listingVehicleClient
        from db import allevent, listVehicle, listingVehicleClient
        #try:
            # return render.user.printreportday(
            #    i.date, listVehicle(i.carid), reportday(i.carid, i.date))
            #return render.user.printreportday(
            #    i.date, listVehicle(i.carid), allevent(i.carid, i.date))
        #except:
        #    return render.user.reportday(
        #        web.ctx.session, listingVehicleClient(
        #            web.ctx.session.clienteId))
        return render.user.printreportday(
            i.date, listVehicle(i.carid), allevent(i.carid, i.date))


class reporthistoric:
    @Sesion
    def GET(self):
        return reporthistoric


# JSON

class reportdayjson:
    @Sesion
    def GET(self):
        """http://0.0.0.0:8080/user/reportdayjson?id=427&fecha=23-10-2018
        """
        import simplejson as json
        from db import reportday

        i = web.input(id=None, fecha=None)
        # print "VEHICLE ID: ", i.id
        # print "FECHA: ", i.fecha
        web.header('content-Type', 'application/json')

        def dthandler(obj):
            obj.fecha = obj.fecha.strftime("%F %H:%M:%S")
            return obj
        return json.dumps([dthandler(row) for row in reportday(i.id, i.fecha)])


class alleventjson:
    @Sesion
    def GET(self):
        """http://0.0.0.0:8080/user/alleventjson?id=427&fecha=23-10-2018"""
        import simplejson as json
        from db import allevent

        i = web.input(id=None, fecha=None)
        # print "VEHICLE ID: ", i.id
        # print "FECHA: ", i.fecha
        web.header('content-Type', 'application/json')

        def dthandler(obj):
            obj.fecha = obj.fecha.strftime("%F %H:%M:%S")
            return obj
        return json.dumps([dthandler(row) for row in allevent(i.id, i.fecha)])


class listeventjson:
    @Sesion
    def GET(self):
        import simplejson as json
        from db import countEventClient
        web.header('content-Type', 'application/json')
        return json.dumps([row for row in countEventClient(
            web.ctx.session.clienteId)])


class listvehiclesjson:
    @Sesion
    def GET(self):
        import simplejson as json
        from db import listingVehicleClient
        web.header('content-Type', 'application/json')
        # return json.dumps(
        # [row for row in listingVehicleClient(web.ctx.session.clienteId)])

        def dthandler(obj):
            obj.fecha = obj.fecha.strftime("%F %H:%M:%S")
            return obj
        # return json.dumps([dthandler(row) for row in views])
        return json.dumps(
            [dthandler(row) for row in listingVehicleClient(
                web.ctx.session.clienteId)])


app_user = web.application(urls, locals())

@@ -2043,16 +2043,17 @@
        #
+ 
 from db 
@@ -2096,32 +2096,32 @@
 ngVehicleClient%0A
-
         from db 
@@ -2179,17 +2179,16 @@
         
-#
 try:%0A   
@@ -2320,17 +2320,16 @@
         
-#
 return r
@@ -2359,33 +2359,32 @@
 ay(%0A            
-#
     i.date, list
@@ -2436,17 +2436,16 @@
         
-#
 except:%0A
@@ -2448,25 +2448,24 @@
 pt:%0A        
-#
     return r
@@ -2486,33 +2486,32 @@
 ortday(%0A        
-#
         web.ctx.
@@ -2545,25 +2545,24 @@
 nt(%0A        
-#
             
@@ -2593,120 +2593,8 @@
 d))%0A
-        return render.user.printreportday(%0A            i.date, listVehicle(i.carid), allevent(i.carid, i.date))%0A
 %0A%0Acl

update redirect printreportday