@admin_bp.route('/manage-account', methods=['GET', 'POST'])
@login_required
@operator_role_required
def manage_account():
    if (request.method == 'GET'):
        accounts = Account.query.order_by(Account.name).all()
        for account in accounts:
            account.user_num = AccountUser.query.filter((AccountUser.account_id == account.id)).count()
        return render_template('admin_manage_account.html', accounts=accounts)
    if (request.method == 'POST'):
        try:
            jdata = request.json
            data = jdata['data']
            if (jdata['action'] == 'delete_account'):
                account = Account.query.filter((Account.name == data)).first()
                if (not account):
                    return make_response(jsonify({'status': 'error', 'msg': 'Account not found.'}), 404)
                for domain in account.domains:
                    Domain(name=domain.name).assoc_account(None)
                result = account.delete_account()
                if result:
                    history = History(msg='Delete account {0}'.format(data), created_by=current_user.username)
                    history.add()
                    return make_response(jsonify({'status': 'ok', 'msg': 'Account has been removed.'}), 200)
                else:
                    return make_response(jsonify({'status': 'error', 'msg': 'Cannot remove account.'}), 500)
            else:
                return make_response(jsonify({'status': 'error', 'msg': 'Action not supported.'}), 400)
        except Exception as e:
            current_app.logger.error('Cannot update account. Error: {0}'.format(e))
            current_app.logger.debug(traceback.format_exc())
            return make_response(jsonify({'status': 'error', 'msg': 'There is something wrong, please contact Administrator.'}), 400)
@admin_bp.route('/manage-account', methods=['GET', 'POST'])
@login_required
@operator_role_required
def manage_account():
    if (request.method == 'GET'):
        accounts = Account.query.order_by(Account.name).all()
        for account in accounts:
            account.user_num = AccountUser.query.filter((AccountUser.account_id == account.id)).count()
        return render_template('admin_manage_account.html', accounts=accounts)
    if (request.method == 'POST'):
        try:
            jdata = request.json
            data = jdata['data']
            if (jdata['action'] == 'delete_account'):
                account = Account.query.filter((Account.name == data)).first()
                if (not account):
                    return make_response(jsonify({'status': 'error', 'msg': 'Account not found.'}), 404)
                for domain in account.domains:
                    Domain(name=domain.name).assoc_account(None)
                result = account.delete_account()
                if result:
                    history = History(msg='Delete account {0}'.format(data), created_by=current_user.username)
                    history.add()
                    return make_response(jsonify({'status': 'ok', 'msg': 'Account has been removed.'}), 200)
                else:
                    return make_response(jsonify({'status': 'error', 'msg': 'Cannot remove account.'}), 500)
            else:
                return make_response(jsonify({'status': 'error', 'msg': 'Action not supported.'}), 400)
        except Exception as e:
            current_app.logger.error('Cannot update account. Error: {0}'.format(e))
            current_app.logger.debug(traceback.format_exc())
            return make_response(jsonify({'status': 'error', 'msg': 'There is something wrong, please contact Administrator.'}), 400)