<!DOCTYPE html>
<html>
<head>
	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<!-- ============================================================================================================
	Generate OTP
============================================================================================================ -->
	<div ng-app="myApp" ng-controller="myController" ng-init="generate_container = true; verify_container = false">
		<div class="col-md-12 contriner alert alert-info" style="margin:50px; width:500px !important">
			<div class="col-md-12 sub-container">
		        <div ng-show="generate_container">
			        <p>Generate OTP <button type="button" ng-click="generate_otp();" class="btn btn-info">Generate</button></p>
			    </div>
			    <div ng-show="verify_container">
			    	<form ng-submit="conf_otp()">
				    	<div class="col-md-12">
				    		<div class="alert alert-warning">
				    			Your Generated OTP is : <b>{{generated_otp}}</b> OR <a href="#" ng-click="regenerate_otp();">regenerate</a>
				    		</div>
				    	</div>
				    	<div class="col-md-12">
					    	<div class="col-md-4">
						        <label>Enter an OTP : </label>
						    </div>
						    <div class="col-md-6">
						    	<input type="number" class="form-control" ng-disabled="input_disabled_status" ng-model="entered_otp" min="0">
						    </div>
						    <div class="col-md-2 alert alert-danger">
						    	00:{{remaining_time}}
						    </div>
						</div>
						<div class="col-md-12">
							<div class="col-md-6">
								<button class="btn btn-primary">Submit</button>
							</div>
						</div>
					</form>
			    </div>
			</div>
		</div>
	</div>

	<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
	<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"></script>
	<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha256-k2WSCIexGzOj3Euiig+TlR8gA0EmPjuc79OEeY5L45g=" crossorigin="anonymous"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
	<script src="script.js"></script>

</body>
</html>
angular.module('myApp', [])
    .controller('myController', function($scope, $interval){
        $scope.remaining_time;
        $scope.generated_otp;
        $scope.interval;

        $scope.generate_otp 	=	function(){
            $scope.remaining_time 	=	60;
            $scope.input_disabled_status 	=	false;
            $scope.generated_otp 	=	Math.round(Math.random() * 10000);

            if((''+$scope.generated_otp).length != 4){
                $scope.generate_otp();
            }

            $scope.generate_container 	=	false;
            $scope.verify_container 	=	true;

            if($scope.interval)
                $interval.cancel($scope.interval);

            $scope.interval 	=	$interval(function(){
                $scope.remaining_time--;

                if($scope.remaining_time == 0){
                    $interval.cancel($scope.interval);
                    $scope.remaining_time 	=	60;
                    $scope.input_disabled_status 	=	true;

                    alert('Please, regenerate OTP !');

                    $scope.verify_container 		=	false;
                    $scope.generate_container		= true;
                    $scope.entered_otp 		=	'';
                }
            },1000);
        }
        $scope.conf_otp 	=	function(){
            if($scope.generated_otp === $scope.entered_otp){
                alert('Success !');
                $scope.generate_container 	=	true;
                $scope.verify_container 	=	false;
                $interval.cancel($scope.interval);
                $scope.remaining_time 	=	60;
                $scope.entered_otp 		=	'';
            } else {
                alert('You have entered wrong OTP !');
            }
        }
        $scope.regenerate_otp 	=	function(){
            $scope.generate_container 	=	true;
            $scope.verify_container 	=	false;
            $interval.cancel($scope.interval);
            $scope.remaining_time 	=	60;
            $scope.entered_otp 		=	'';
        }
    });
/* Styles go here */