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
from minibase.company.forms import companyForm, selectCompanyForm
import minibase.database.utils as dbUtils

# Declaring a blueprint
company = Blueprint('company', __name__)


@company.route("/company_register", methods=['GET', 'POST'])
def company_register():
    form = companyForm()
    form.industry.choices = [(row.id, row.name) for row in dbUtils.getIndustryNames()]
    form.relation.choices = [(row.id, row.name) for row in dbUtils.getCompanyRelations()]
    form.legal_entity.choices = [(row.id, row.name) for row in dbUtils.getCompanyLegalEntities()]
    form.status.choices = [(row.id, row.name) for row in dbUtils.getCompanyStatuses()]
    form.country.choices = [(row.id, row.name) for row in dbUtils.getCountryNames()]

    if form.validate_on_submit():
        company = Company(
            name=form.company_name.data,
            legal_entity_id=form.legal_entity.data,
            relation_id=form.relation.data,
            industry_id=form.industry.data,
            status_id=form.status.data,
            website=form.website.data,
            country_id=form.country.data,
            state=form.state.data,
            city=form.city.data,
            post_code=form.post.data,
            street=form.street.data,
            street_no=form.no.data)
        # Here we need to give the id of thr role as this is a foreign key
        db.session.add(company)
        db.session.commit()
        flash(f'{"Company succesfull registered!"}', 'success')
        return render_template('register_form.html',
                            title='Register Company',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)
    elif request.method == 'GET':
        form.country.data = str(214)
        return render_template('register_form.html',
                            title='Register Company',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)

    return render_template('register_form.html',
                            title='Register Company',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)


@company.route("/select_company", methods=['GET', 'POST'])
def select_company():
    form = selectCompanyForm()
    # row.id which correspondsd to company.id is returned when the company is elected
    form.company.choices = [(row.id, row.name +' | '+ row.company_country.name) for row in dbUtils.getCompanies()]
    if form.validate_on_submit():
        dbUtils.setSelectedCompany(form.company.data)
        return redirect(url_for('project.select_project'))

    return render_template('project/select_company.html',
                            title='Select Company Name',
                            theme=themeMinibase,
                            globalVars=globalVars,
                            form=form)
