from flask import render_template, Blueprint, request, redirect, url_for
import requests
from backstage.utils.routes import update_manage_table
from backstage.config import PORTAL_SERVER, BHOUSE_SERVER

room_planner_app = Blueprint('room_planner', __name__)


@room_planner_app.route('/backstage/room_planner')
def main():
    response = requests.get('{}manages/data?page=room_planner'.format(PORTAL_SERVER))
    return render_template('room_planner.html',
                           title='規劃師服務',
                           form_url='room_planner.update',
                           page='room_planner',
                           contents=response.json(),
                           bhouse_server=BHOUSE_SERVER)


@room_planner_app.route('/backstage/room_planner/update', methods=['POST'])
def update():
    update_manage_table(request.form.getlist('data'),
                        request.form.getlist('element'),
                        request.args,
                        request.files.getlist('image'),
                        'room_planner3')
    return redirect(url_for('room_planner.main'))
