from flask import render_template, url_for, flash, redirect, request, Blueprint
from minibase import db
from minibase.config import themeMinibase, globalVars
from minibase.database.models import Company, Company_legal_entity, Company_relation,Company_status 
from minibase.database.models import Industry, Note_status
from minibase.database.models import Person_role, Person_competence
from minibase.database.models import Product_status, Product_physical, Product_packaging, Product_eligibility, Product_classification, Product_domain, Product_category, Product_sub_category
from minibase.database.models import Project_status
from minibase.admin.forms import compLegalEntityForm, compRelationForm, compStatusForm
from minibase.admin.forms import personRoleForm, personCompetenceForm
from minibase.admin.forms import industryRegisterForm, noteStatusForm
from minibase.admin.forms import projectStatusForm
from minibase.admin.forms import productStatusForm, productEligibilityForm, productDomainForm, productClassificationForm, productCategoryForm, productSubCategoryForm
# Declaring a blueprint
admin = Blueprint('admin', __name__)


@admin.route("/company_register_legal_entity", methods=['GET', 'POST'])
def company_register_legal_entity():
    toPrint = "Company Legal Entity"
    form = compLegalEntityForm()
    query = Company_legal_entity.query.order_by(Company_legal_entity.name.asc())
    if form.validate_on_submit():
        dbRow = Company_legal_entity(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/company_register_relation", methods=['GET', 'POST'])
def company_register_relation():
    toPrint = "Company Relation"
    form = compRelationForm()
    query = Company_relation.query.order_by(Company_relation.name.asc())
    if form.validate_on_submit():
        dbRow = Company_relation(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/company_register_status", methods=['GET', 'POST'])
def company_register_status():
    toPrint = "Company Status"
    form = compStatusForm()
    query = Company_status.query.order_by(Company_status.name.asc())
    if form.validate_on_submit():
        dbRow = Company_status(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/register_industry", methods=['GET', 'POST'])
def register_industry():
    toPrint = "Company Industry"
    form = industryRegisterForm()
    query = Industry.query.order_by(Industry.name.asc())
    if form.validate_on_submit():
        dbRow = Industry(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)



@admin.route("/person_register_role", methods=['GET', 'POST'])
def person_register_role():
    toPrint = "Person Role"
    form = personRoleForm()
    query = Person_role.query.order_by(Person_role.name.asc())
    if form.validate_on_submit():
        dbRow = Person_role(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/person_register_competence", methods=['GET', 'POST'])
def person_register_competence():
    toPrint = "Person Competence"
    form = personCompetenceForm()
    query = Person_competence.query.order_by(Person_competence.name.asc())
    if form.validate_on_submit():
        dbRow = Person_competence(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/note_register_status", methods=['GET', 'POST'])
def note_register_status():
    toPrint = "Note Status"
    form = noteStatusForm()
    query = Note_status.query.order_by(Note_status.name.asc())
    if form.validate_on_submit():
        dbRow = Company_status(
            name=form.name.data,
            description=form.description.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)

@admin.route("/project_register_status", methods=['GET', 'POST'])
def project_register_status():
    toPrint = "Project Status"
    form = projectStatusForm()
    query = Project_status.query.order_by(Project_status.name.asc())
    if form.validate_on_submit():
        dbRow = Project_status(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_status", methods=['GET', 'POST'])
def product_register_status():
    toPrint = "Product Status"
    form = productStatusForm()
    query = Product_status.query.order_by(Product_status.name.asc())
    if form.validate_on_submit():
        dbRow = Product_status(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_eligibility", methods=['GET', 'POST'])
def product_register_eligibility():
    toPrint = "Product Eligibility"
    form = productEligibilityForm()
    query = Product_eligibility.query.order_by(Product_eligibility.name.asc())
    if form.validate_on_submit():
        dbRow = Product_eligibility(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_domain", methods=['GET', 'POST'])
def product_register_domain():
    toPrint = "Product Domain"
    form = productEligibilityForm()
    query = Product_domain.query.order_by(Product_domain.name.asc())
    if form.validate_on_submit():
        dbRow = Product_domain(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_classification", methods=['GET', 'POST'])
def product_register_classification():
    toPrint = "Product Classification"
    form = productEligibilityForm()
    query = Product_classification.query.order_by(Product_classification.name.asc())
    if form.validate_on_submit():
        dbRow = Product_classification(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_category", methods=['GET', 'POST'])
def product_register_category():
    toPrint = "Product Category"
    form = productEligibilityForm()
    query = Product_category.query.order_by(Product_category.name.asc())
    if form.validate_on_submit():
        dbRow = Product_category(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@admin.route("/product_register_sub_category", methods=['GET', 'POST'])
def product_register_sub_category():
    toPrint = "Product Sub Category"
    form = productEligibilityForm()
    query = Product_sub_category.query.order_by(Product_sub_category.name.asc())
    if form.validate_on_submit():
        dbRow = Product_sub_category(
            name=form.name.data,
            description=form.description.data)
        db.session.add(dbRow)
        db.session.commit()

        flash(f' {toPrint} {" registered!"}', 'success')
        return render_template('register_form.html',
                                title='Register ' + toPrint,
                                tableTitle='Existing ' + toPrint,
                                dbTable=query,
                                theme=themeMinibase,
                                globalVars=globalVars,
                                form=form)

    return render_template('register_form.html',
                            title='Register ' + toPrint,
                            tableTitle='Existing ' + toPrint,
                            dbTable=query,
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)
