from datetime import datetime

from nicegui import ui, app
from web_ui import *

from users import *
from definitions import *
from calendar import monthrange

import hashlib
import calendar
import locale

@ui.page('/login')
def page_login():

    # Settingsdatei einlesen
    data = load_adminsettings()

    def login():
        nonlocal data

        if username.value == data["admin_user"]:
            if password.value == data["admin_password"]:
                active_login = cookie_hash(data["admin_user"], data["admin_password"])
                app.storage.user['secret'] = active_login
                ui.navigate.to("/admin")
            else:
                ui.notify("Login fehlgeschlagen")

    #ui.markdown(f"## {app_title} {app_version}")
    #ui.markdown("Bitte einloggen")

    pageheader("Bitte einloggen:")

    with ui.grid(columns=2):
        ui.markdown("Benutzer:")
        username = ui.input('Benutzername')
        ui.markdown("Passwort:")
        password = ui.input('Passwort', password=True)
        ui.button(text="Login", on_click=lambda: login())