import logging
import math

from flask_login import login_user

from webgds import app, login, utils
from flask import render_template, request
from webgds.models import UserRole
from webgds.admin import *

logging.basicConfig(filename='flask_app.log', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

@app.route('/')
def home():  # put application's code here
    menu = utils.load_menu()
    service = utils.load_service()
    category = utils.load_categories()
    teams = utils.load_team()
    randomProject = utils.random_project()
    app.logger.info('Truy cập trang chủ')

    return render_template('index2.html', menus = menu, services = service, randomProject=randomProject, categorys= category, teams = teams, header ="home")

@app.route('/about')
def about_page():
    teams = utils.load_team()
    return render_template('about.html', teams = teams, header="about")

@app.route('/contact')
def contact_page():
     return render_template('contact.html', header="contact")


@app.route('/project')
def project_page():
    cate_id = request.args.get('cate_id')
    kw = request.args.get('kw')
    page = request.args.get('page', 1)
    project = utils.load_project(cate_id, kw, page)
    category = utils.load_categories()
    return render_template('project.html', categorys= category, projects=project, pages=math.ceil(utils.count_project()/app.config['PAGE_SIZE']), header="project")

@app.route('/project/<int:project_id>')
def project_detail(project_id):
    project = utils.get_project_by_id(project_id)
    project_images = utils.load_project_image(project_id)
    randomProject = utils.random_project()
    return render_template('project_detail.html', project=project, randomProject=randomProject, projectImages=project_images)


@app.route('/servicegds')
def service_page():
    return render_template('service.html', header="service")


@app.route("/admin-login", methods=['post'])
def process_admin_login():
    username = request.form.get('username')
    password = request.form.get('password')
    u = utils.auth_user(username=username, password=password, role=UserRole.ADMIN)
    if u:
        login_user(user=u)
    return redirect('/admin')

@app.route('/addContact', methods=['get', 'post'])
def  add_contact():
    err_msg = "Gui thanh cong"
    if request.method.__eq__('POST'):
        username = request.form.get('username')
        email = request.form.get('email')
        phone = request.form.get('phone')
        message = request.form.get('message')

        utils.add_contact(username, email, phone, message)
        return redirect('/contact')
    return render_template('contact.html', err_msg=err_msg)


@app.route('/addSubscriber', methods=['get', 'post'])
def add_subscriber():
    if request.method.__eq__('POST'):
        email = request.form.get('email')
        utils.add_Subscriber(email)
        return redirect('/contact')
    return render_template('contact.html')


@app.context_processor
def common_attributes():
    return {
        'abouts': utils.load_about(),

    }
@login.user_loader
def load_user(user_id):
    return utils.get_user_by_id(user_id)


if __name__ == '__main__':
    app.run()


