from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
import MySQLdb.cursors
import re


app = Flask(__name__)

# Change this to your secret key (can be anything, it's for extra protection)
app.secret_key = 'your secret key'

# Enter your database connection details below
app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'sjcet'

# Intialize MySQL
mysql = MySQL(app)


@app.route('/')
def index():
    return render_template('home.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
    # Output message if something goes wrong...
    msg = ''
    # Check if "username" and "password" POST requests exist (user submitted form)
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']
        # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(
            'SELECT * FROM login WHERE username = %s AND password = %s', (username, password,))
        # Fetch one record and return result
        account = cursor.fetchone()
        # If account exists in accounts table in out database
        if account:
            # Create session data, we can access this data in other routes
            session['loggedin'] = True
            session['id'] = account['id']
            session['username'] = account['username']
            #session['email'] = account['email']
            # Redirect to home page
            return redirect(url_for('home'))
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    # Show the login form with message (if any)
    return render_template('login.html', msg=msg)

# http://localhost:5000/python/logout - this will be the logout page


@app.route('/logout')
def logout():
    # Remove session data, this will log the user out
    session.pop('loggedin', None)
    session.pop('id', None)
    session.pop('username', None)
    # Redirect to login page
    return redirect(url_for('login'))

# http://localhost:5000/pythinlogin/home - this will be the home page, only accessible for loggedin users


@app.route('/s_welcome')
def home():
    # Check if user is loggedin
    if 'loggedin' in session:

        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE username = %s',
                       (session['username'],))
        account = cursor.fetchone()

        return render_template('s_welcome.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))

# http://localhost:5000/pythinlogin/profile - this will be the profile page, only accessible for loggedin users


@app.route('/s_profile')
def profile():
    # Check if user is loggedin
    if 'loggedin' in session:
        # checking weather profile is completed or not

        # We need all the account info for the user so we can display it on the profile page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE username = %s',
                       (session['username'],))
        account = cursor.fetchone()

        # Show the profile page with account info
        return render_template('s_profile.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s1_sattendance')
def s1_sattendance():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM s1sattendance WHERE username = %s',
                       (session['username'],))
        attendance = cursor.fetchone()
        return render_template('s1_sattendance.html', s1sattendance=attendance)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s2_sattendance')
def s2_sattendance():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM s2sattendance WHERE username = %s',
                       (session['username'],))
        attendance = cursor.fetchone()
        return render_template('s2_sattendance.html', s2sattendance=attendance)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s3_sattendance')
def s3_sattendance():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM s3sattendance WHERE username = %s',
                       (session['username'],))
        attendance = cursor.fetchone()
        return render_template('s3_sattendance.html', s3sattendance=attendance)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s4_sattendance')
def s4_sattendance():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM s4sattendance WHERE username = %s',
                       (session['username'],))
        attendance = cursor.fetchone()
        return render_template('s4_sattendance.html', s4sattendance=attendance)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s_marks')
def s_marks():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM marks WHERE username = %s',
                       (session['username'],))
        marks = cursor.fetchone()
        return render_template('s_marks.html', marks=marks)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s1_stimetable')
def s1_stimetable():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE id = %s',
                       (session['id'],))
        account = cursor.fetchone()
        return render_template('s1_stimetable.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s2_stimetable')
def s2_stimetable():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE id = %s',
                       (session['id'],))
        account = cursor.fetchone()
        return render_template('s2_stimetable.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s3_stimetable')
def s3_stimetable():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE id = %s',
                       (session['id'],))
        account = cursor.fetchone()
        return render_template('s3_stimetable.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/s4_stimetable')
def s4_stimetable():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE id = %s',
                       (session['id'],))
        account = cursor.fetchone()
        return render_template('s4_stimetable.html', account=account)
    # User is not loggedin redirect to login page
    return redirect(url_for('login'))


@app.route('/t_login', methods=['GET', 'POST'])
def t_login():
    # Output message if something goes wrong...
    msg = ''
    # Check if "username" and "password" POST requests exist (user submitted form)
    if request.method == 'POST' and 'username' in request.form and 'password' in request.form:
        # Create variables for easy access
        username = request.form['username']
        password = request.form['password']
        # Check if account exists using MySQL
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute(
            'SELECT * FROM tlogin WHERE username = %s AND password = %s', (username, password,))
        # Fetch one record and return result
        account = cursor.fetchone()
        # If account exists in accounts table in out database
        if account:
            # Create session data, we can access this data in other routes
            session['loggedin'] = True
            session['id'] = account['id']
            session['username'] = account['username']
            #session['email'] = account['email']
            # Redirect to home page
            return redirect(url_for('t_dashboard'))
        else:
            # Account doesnt exist or username/password incorrect
            msg = 'Incorrect username/password!'
    # Show the login form with message (if any)
    return render_template('t_login.html', msg=msg)


@app.route('/t_dashboard')
def t_dashboard():
    # Check if user is loggedin
    if 'loggedin' in session:
        # User is loggedin show them the home page
        cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
        cursor.execute('SELECT * FROM accounts WHERE username = %s',
                       (session['username'],))
        account = cursor.fetchone()

        return render_template('t_dashboard.html')
    # User is not loggedin redirect to login page
    return redirect(url_for('t_login'))


@app.route('/t_logout')
def t_logout():
    # Remove session data, this will log the user out
    session.pop('loggedin', None)
    session.pop('id', None)
    session.pop('username', None)
    # Redirect to login page
    return redirect(url_for('t_login'))


if __name__ == "__main__":
    app.run(debug=True)
