<!doctype html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Kogito Serverless Workflow Funqy</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/wingcss/0.1.8/wing.min.css"/>
    <!-- Load AngularJS -->
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module("CountriesManagement", []);

        //Controller Part
        app.controller("CountriesManagementController", function ($scope, $http) {

            $scope.countries = [];

            $scope.form = {
                name: ""
            };

            $scope.add = function () {
                var workflowdata = { "workflowdata": {"name": $scope.form.name} };

                //alert("data: " + JSON.stringify(data));

                $http({
                    method: "POST",
                    url: '/countryworkflow',
                    data: workflowdata,
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    }
                }).then(_success, _error);
            };

            function _success(response) {
                $scope.countries.push(response.data.workflowdata);
                _clearForm();
            }

            function _error(response) {
                alert(response.data.message || response.statusText);
            }

            //Clear the form
            function _clearForm() {
                $scope.form.name = "";
            }
        });
    </script>
</head>
<body ng-app="CountriesManagement" ng-controller="CountriesManagementController">

<div class="container">
    <h1>Serverless Workflow - Countries</h1>

    <h3>Enter a country name</h3>
    <form ng-submit="add()">
        <div class="row">
            <div class="col-6"><input type="text" placeholder="Name" ng-model="form.name" size="60"/></div>
        </div>
        <input type="submit" value="Classify"/>
    </form>

    <h3>Country Information</h3>
    <table style="width:100%">
        <tr>
            <th align="left"><b>Name</b></th>
            <th align="left"><b>Capital</b></th>
            <th align="left"><b>Region</b></th>
            <th align="left"><b>Population</b></th>
            <th align="left"><b>Population Classification</b></th>
        </tr>
        <tr ng-repeat="country in countries">
            <td>{{ country.name }}</td>
            <td>{{ country.capital }}</td>
            <td>{{ country.region }}</td>
            <td>{{ country.population }}</td>
            <td>{{ country.classifier }}</td>
        </tr>
    </table>
</div>

</body>
</html>