<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <% include ../partials/head %>
  <% include ../partials/header %>
  <script type="text/javascript" src="/javascripts/admportal.js" async></script>
  <title>SDN-FC AdminPortal</title>
<script class="init">
    $(document).ready(function() {
    $('#vrr_credentials').DataTable( {
        "order": [[ 0, "asc" ]]
    } );
} );
</script>

</head>
<body>

<div class="well well-sm">
<h3>VRR Credentials</h3>
</div>

<% if ( typeof result != 'undefined' ) {
		if (result.code.length > 0) { 
			if ( result.code == 'success' ) { %>
				<div class='alert alert-success' role='alert'>
				<%
				for ( x in result.msg ){ %>
					<div><%= result.msg[x] %></div>
				<% } %>
				</div>
			<% } else { %> 
				<div class='alert alert-danger' role='danger'>
				<%
				for ( x in result.msg ){ %>
					<div><%= result.msg[x] %></div>
				<% } %>
				</div>
			<% } %>
		<% } %>
<% } %>

<% if( typeof privilege != 'undefined'){
    var priv = privilege.privilege;
} else {
    var priv = 'A';
} %>


<div class="container-fluid">

	 <% if(priv == 'A'){ %>
        <div class="actions" style="padding:15px 0px;">
            <button class="btn btn-primary" data-toggle="modal" data-target="#add_vrr_credentials">
           Add VRR Credentials 
            </button>
            </div>
    <% } %>

	<table id="vrr_CREDENTIALS" class="table table-hover table-condensed">
      <thead>
        <tr>
          <th>*Server Name</th>
          <th>*IP Address</th>
          <th>*Username</th>
          <th>*Password</th>
		   <% if(priv == 'A'){ %>
          <th>Action</th>
          <% } %>
        </tr>
      </thead>
      <tbody>
      <% rows.forEach( function(row) { %> 
        <tr>
            <td><%= row.server_name %></td>
            <td><%= row.ip_address %></td>
            <td><%= row.username %></td>
            <td>*****</td>
			<% if(priv == 'A') { %>
            <td>
				<button type="button" class="btn btn-default btn-xs"
						onclick="updateVrrCredentials('<%= row.server_name %>', '<%= row.ip_address %>', '<%= row.username %>','<%= row.password %>');">Update</button>
				<button type="button" class="btn btn-default btn-xs"
				onclick="deleteVrrCredentials('<%= row.server_name %>');">Delete</button>
            </td>
            <% } %>
        </tr>
    <% }); %>
      </tbody>
    </table>

</div>

<% include ../partials/vrr_credentials %>
<footer>
    <% include ../partials/footer %>
</footer>

<script type="text/javascript">
function submitVrrCredentials(form)
{
    var errorMsg='';
	var server_name = '';
	var ip_address = '';
	var username = '';
	var password = '';
	var confirm_password = '';

    if ( form.name == 'addForm' )
    {
		server_name = form.nf_server_name;
		ip_address = form.nf_ip_address;
		username = form.nf_username;
		password = form.nf_password;
		confirm_password = form.nf_confirm_password;

    	if ( (server_name.value == null) || (server_name.value == "") || isblank(server_name.value) )
    	{
        	errorMsg += 'Server Name is required.<br>';
    	}
    	if ( (ip_address.value == null) || (ip_address.value == "") || isblank(ip_address.value) )
    	{
        	errorMsg += 'IP Address is required.<br>';
    	}
    	if ( (password.value == null) || (password.value == "") || isblank(password.value) )
    	{
        	errorMsg += 'Password is required.<br>';
    	}
    	if ( (confirm_password.value == null) || (confirm_password.value == "") || isblank(confirm_password.value) )
    	{
        	errorMsg += 'Confirm Password is required.<br>';
    	}
    	if( errorMsg.length > 0 ) {
        	bootbox.alert(errorMsg);
        	return;
    	}
		if ( password.value != confirm_password.value )
        {
            bootbox.alert('Passwords do not match.');
            return;
        }
    }
    else
    {
		server_name = form.uf_server_name;
		ip_address = form.uf_ip_address;
		username = form.uf_username;
		password = form.uf_password;
		confirm_password = form.uf_confirm_password;

		if ( (server_name.value == null) || (server_name.value == "") || isblank(server_name.value) )
    	{
        	errorMsg += 'Server Name is required.<br>';
    	}
    	if ( (ip_address.value == null) || (ip_address.value == "") || isblank(ip_address.value) )
    	{
        	errorMsg += 'IP Address is required.<br>';
    	}
    	if ( (password.value == null) || (password.value == "") || isblank(password.value) )
    	{
        	errorMsg += 'Password is required.<br>';
    	}
    	if ( (confirm_password.value == null) || (confirm_password.value == "") || isblank(confirm_password.value) )
    	{
        	errorMsg += 'Confirm Password is required.<br>';
    	}
    	if( errorMsg.length > 0 ) {
        	bootbox.alert(errorMsg);
        	return;
    	}

		if ( password.value != confirm_password.value )
        {
            bootbox.alert('Passwords do not match.');
            return;
        }
    }
    form.submit();
}

function updateVrrCredentials(server_name,ip_address,username,password) {

    document.getElementById('uf_server_name').value=server_name;
    document.getElementById('uf_key_server_name').value=server_name;
    document.getElementById('uf_ip_address').value=ip_address;
    document.getElementById('uf_username').value=username;
    document.getElementById('uf_password').value=password;
    document.getElementById('uf_confirm_password').value=password;

    $('#update_vrr_credentials').modal('show');
}

function deleteVrrCredentials(server_name) {

	var url_string = encodeURI("/sdnfc/deleteVrrCredentials?server_name=" + server_name); 

    bootbox.confirm({
        message: "Are you sure you want to delete VRR Credential [ " + server_name + " ]",
        callback: function(result) {
            if ( result )
            {
				location.assign(url_string);
            }
            return;
        },
        buttons: {
            cancel: {
                label: "Cancel"
            },
            confirm: {
                label: "Yes"
            }
        }
    });
}

</script>

</body>
</html>