from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__, static_folder='static', static_url_path='')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///product.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class Product(db.Model):
    """Class for products."""

    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(length=50), nullable=False, unique=True)
    description = db.Column(db.String(), nullable=False)
    price = db.Column(db.String(length=10), nullable=False)
    img = db.Column(db.String(length=50), nullable=False)
    alt = db.Column(db.String(length=50), nullable=False)
    group = db.Column(db.String(length=25), nullable=False)


menu = [
    {"name": "Главная", "url": "home", "active": "", "aria": ""},
    {"name": "ЭЦП + Отчетность", "url": "astral", "active": "", "aria": ""},
    {"name": "Программы 1С", "url": "1c", "active": "", "aria": ""},
    {"name": "Онлайн-кассы", "url": "online-kassa", "active": "", "aria": ""},
    {"name": "Контакты", "url": "contacts", "active": "", "aria": ""}
]


@app.route("/")
@app.route("/home")
def home():
    """Render home page."""
    for m in menu:
        m["active"] = ""
        m['aria'] = ''

    menu[0]['active'] = 'active'
    menu[0]['aria'] = 'page'
    return render_template("home.html", menu=menu,
                           title="ЭЦП, Программы 1С, Онлайн-кассы в Клину")


@app.route("/astral")
def astral_page():
    """Render astral page."""
    for m in menu:
        m['active'] = ''
        m['aria'] = ''

    menu[1]['active'] = 'active'
    menu[1]['aria'] = 'page'

    product_astral = Product.query.filter_by(group="astral")
    return render_template("astral.html", menu=menu, product=product_astral,
                           title="ЭЦП в Клину")


@app.route("/1c")
def one_c_page():
    """Render 1c page."""
    for m in menu:
        m['active'] = ''
        m['aria'] = ''

    menu[2]['active'] = 'active'
    menu[2]['aria'] = 'page'

    product_1c = Product.query.filter(Product
                                      .group.in_(["buh", "cloud", "trade"]))
    return render_template("1c.html", menu=menu,
                           title="программы 1С в Клину", product_1c=product_1c)


@app.route("/online-kassa")
def kassa_page():
    """Render kassa page."""
    for m in menu:
        m['active'] = ''
        m['aria'] = ''

    menu[3]['active'] = 'active'
    menu[3]['aria'] = 'page'

    product_kkt = Product.query.filter_by(group="kkt")
    product_fn = Product.query.filter_by(group="fn")
    return render_template("online-kassa.html", menu=menu,
                           title="Онлайн кассы в Клину",
                           product_kkt=product_kkt, product_fn=product_fn)


@app.route("/contacts")
def contacts_page():
    """Render contact page."""
    for m in menu:
        m['active'] = ''
        m['aria'] = ''

    menu[4]['active'] = 'active'
    menu[4]['aria'] = 'page'

    return render_template("contacts.html", menu=menu, title="Контакты")


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