from citywok_ms.file.models import EmployeeFile, File
import citywok_ms.employee.messages as employee_msg
import citywok_ms.file.messages as file_msg
from citywok_ms.employee.forms import EmployeeForm
from citywok_ms.file.forms import FileForm
from flask import Blueprint, flash, redirect, render_template, url_for
from citywok_ms.employee.models import Employee

employee = Blueprint("employee", __name__, url_prefix="/employee")


@employee.route("/")
def index():
    return render_template(
        "employee/index.html",
        title=employee_msg.INDEX_TITLE,
        active_employees=Employee.get_active(),
        suspended_employees=Employee.get_suspended(),
    )


@employee.route("/new", methods=["GET", "POST"])
def new():
    form = EmployeeForm()
    if form.validate_on_submit():
        employee = Employee.create_by_form(form)
        flash(employee_msg.NEW_SUCCESS.format(name=employee.full_name), "success")
        return redirect(url_for("employee.index"))
    return render_template(
        "employee/form.html", title=employee_msg.NEW_TITLE, form=form
    )


@employee.route("/<int:employee_id>")
def detail(employee_id):
    return render_template(
        "employee/detail.html",
        title=employee_msg.DETAIL_TITLE,
        employee=Employee.get_or_404(employee_id),
        file_form=FileForm(),
    )


@employee.route("/<int:employee_id>/update", methods=["GET", "POST"])
def update(employee_id):
    employee = Employee.get_or_404(employee_id)
    form = EmployeeForm()
    form.hide_id.data = employee_id
    if form.validate_on_submit():
        employee.update_by_form(form)
        flash(employee_msg.UPDATE_SUCCESS.format(name=employee.full_name), "success")
        return redirect(url_for("employee.detail", employee_id=employee_id))

    form.process(obj=employee)

    return render_template(
        "employee/form.html",
        employee=employee,
        form=form,
        title=employee_msg.UPDATE_TITLE,
    )


@employee.route("/<int:employee_id>/suspend", methods=["POST"])
def suspend(employee_id):
    employee = Employee.get_or_404(employee_id)
    employee.suspend()
    flash(employee_msg.SUSPEND_SUCCESS.format(name=employee.full_name), "success")
    return redirect(url_for("employee.detail", employee_id=employee_id))


@employee.route("/<int:employee_id>/activate", methods=["POST"])
def activate(employee_id):
    employee = Employee.get_or_404(employee_id)
    employee.activate()
    flash(employee_msg.ACTIVATE_SUCCESS.format(name=employee.full_name), "success")
    return redirect(url_for("employee.detail", employee_id=employee_id))


@employee.route("/<int:employee_id>/upload", methods=["POST"])
def upload(employee_id):
    form = FileForm()
    file = form.file.data
    if form.validate_on_submit():
        db_file = EmployeeFile.create_by_form(form, Employee.get_or_404(employee_id))
        flash(file_msg.UPLOAD_SUCCESS.format(name=db_file.full_name), "success")
    elif file is not None:
        flash(
            file_msg.INVALID_FORMAT.format(format=File.split_file_format(file)),
            "danger",
        )
    else:
        flash(file_msg.NO_FILE, "danger")
    return redirect(url_for("employee.detail", employee_id=employee_id))

[(9, 'flask.Blueprint', 'Blueprint', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (24, 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', False, 'from citywok_ms.employee.forms import EmployeeForm\n'), (29, 'flask.render_template', 'render_template', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (46, 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', False, 'from citywok_ms.employee.models import Employee\n'), (47, 'citywok_ms.employee.forms.EmployeeForm', 'EmployeeForm', False, 'from citywok_ms.employee.forms import EmployeeForm\n'), (56, 'flask.render_template', 'render_template', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (66, 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', False, 'from citywok_ms.employee.models import Employee\n'), (74, 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', False, 'from citywok_ms.employee.models import Employee\n'), (82, 'citywok_ms.file.forms.FileForm', 'FileForm', False, 'from citywok_ms.file.forms import FileForm\n'), (26, 'citywok_ms.employee.models.Employee.create_by_form', 'Employee.create_by_form', False, 'from citywok_ms.employee.models import Employee\n'), (68, 'citywok_ms.employee.messages.SUSPEND_SUCCESS.format', 'employee_msg.SUSPEND_SUCCESS.format', True, 'import citywok_ms.employee.messages as employee_msg\n'), (69, 'flask.url_for', 'url_for', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (76, 'citywok_ms.employee.messages.ACTIVATE_SUCCESS.format', 'employee_msg.ACTIVATE_SUCCESS.format', True, 'import citywok_ms.employee.messages as employee_msg\n'), (77, 'flask.url_for', 'url_for', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (94, 'flask.url_for', 'url_for', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (17, 'citywok_ms.employee.models.Employee.get_active', 'Employee.get_active', False, 'from citywok_ms.employee.models import Employee\n'), (18, 'citywok_ms.employee.models.Employee.get_suspended', 'Employee.get_suspended', False, 'from citywok_ms.employee.models import Employee\n'), (27, 'citywok_ms.employee.messages.NEW_SUCCESS.format', 'employee_msg.NEW_SUCCESS.format', True, 'import citywok_ms.employee.messages as employee_msg\n'), (28, 'flask.url_for', 'url_for', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (39, 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', False, 'from citywok_ms.employee.models import Employee\n'), (40, 'citywok_ms.file.forms.FileForm', 'FileForm', False, 'from citywok_ms.file.forms import FileForm\n'), (51, 'citywok_ms.employee.messages.UPDATE_SUCCESS.format', 'employee_msg.UPDATE_SUCCESS.format', True, 'import citywok_ms.employee.messages as employee_msg\n'), (52, 'flask.url_for', 'url_for', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (85, 'citywok_ms.employee.models.Employee.get_or_404', 'Employee.get_or_404', False, 'from citywok_ms.employee.models import Employee\n'), (86, 'citywok_ms.file.messages.UPLOAD_SUCCESS.format', 'file_msg.UPLOAD_SUCCESS.format', True, 'import citywok_ms.file.messages as file_msg\n'), (93, 'flask.flash', 'flash', False, 'from flask import Blueprint, flash, redirect, render_template, url_for\n'), (89, 'citywok_ms.file.models.File.split_file_format', 'File.split_file_format', False, 'from citywok_ms.file.models import EmployeeFile, File\n')]