from flask import Flask, render_template, request, url_for, redirect, flash
import requests
#from flask_mysqldb import MySQL
import json

import Read

app = Flask(__name__)

#app.config['MYSQL_HOST'] = 'localhost'
#app.config['MYSQL_USER'] = 'root'
#app.config['MYSQL_PASSWORD'] = ''
#app.config['MYSQL_DB'] = 'dbname'

#mysql = MySQL(app)


@app.route("/", methods=['GET', 'POST'])
def home():
    panel = request.form.get('panel')
    jmeno = request.form.get('name')
    krev_sk = request.form.get('krev_sk')
    alergie = request.form.get('alergie')
    stav = request.form.get('stav')
    
    login_id = request.form.get('login_id')
    passw = request.form.get('passw')
    
    jsonFile = {
        "name" : jmeno,
        "krevni_skupina" : krev_sk,
        "alergie" : alergie,
        "stav_pacienta" : stav
    }

    f = open('static/panely.txt', 'r')
    panely = f.readlines()
    f.close()

    if request.method == 'POST':      
        
        if (login_id == "admin" and passw == "admin" or Read.auth() == True):
            with open('templates/data.json', 'w') as outfile:
                json.dump(jsonFile, outfile)

            if (jmeno != ""):
                try:
                    ip = panel.split("|", 1)

                    r = requests.get("http://" + str(ip[0]) + "/push")
                    
                    if (r.status_code == 200):

                        print(ip[0])
                        print(jmeno)
                        print(krev_sk)
                        print(alergie)
                        print(stav)
            
                        flash('Úspěšně nahráno - Jméno: ' + jmeno + '; Krevní skupina: ' + krev_sk+'; Alergie: ' + alergie+'; Stav: ' + stav)
                        return render_template('index.php', panels = panely)
                    else:
                        flash('Nahrávání selhalo (panel nepřipojen)')
                        return render_template('index.php', panels = panely)
                except:
                    flash('Nahrávání selhalo (panel nepřipojen)')
                    return render_template('index.php', panels = panely)
            else:
                flash('Zadejte jméno')
                return render_template('index.php', panels = panely)
        else:
            flash('Chyba ověření!')
            return render_template('index.php', panels = panely)
    
    else:
        return render_template('index.php', panels = panely)


@app.route("/uploadPanel", methods=["GET", "POST"])
def uploadPanel():
    if request.method == 'GET':
        return "Přidejte panel přes formulář"
     
    if request.method == 'POST':
        panelN = request.form['panelN']
        panelIP = request.form['panelIP']
        f2 = open("static/panely.txt", "a")
        f2.write(str(panelIP) + "|" + str(panelN) + "\n")
        f2.close()
        return f"Panel úspěšně přidán!"

@app.route("/data", methods=['GET', 'POST'])
def data():
    return render_template('data.json')

if __name__ == '__main__':
    app.secret_key = 'fdjkvbhvbjkhbvfd'
    app.run(host="0.0.0.0", debug=True)

