#*
 * Copyright 2012 LinkedIn, Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
*#

<!DOCTYPE html> 
<html>
	<head>
#parse( "azkaban/webapp/servlet/velocity/style.vm" )
		<script type="text/javascript" src="${context}/js/jquery/jquery.js"></script>    
		<script type="text/javascript" src="${context}/js/jqueryui/jquery-ui.custom.min.js"></script>   
		<script type="text/javascript" src="${context}/js/namespace.js"></script>
		<script type="text/javascript" src="${context}/js/underscore-1.2.1-min.js"></script>
		<script type="text/javascript" src="${context}/js/backbone-0.5.3-min.js"></script>
		<script type="text/javascript" src="${context}/js/jquery.simplemodal.js"></script>
		<script type="text/javascript" src="${context}/js/azkaban.nav.js"></script>
		<script type="text/javascript" src="${context}/js/azkaban.permission.view.js"></script>
		<script type="text/javascript">
			var contextURL = "${context}";
			var currentTime = ${currentTime};
			var timezone = "${timezone}";
			var errorMessage = null;
			var successMessage = null;
			var userId = "$username";
			var projectName = "$project.name";
		</script>
	</head>
	<body>
#set($current_page="all")
#parse( "azkaban/webapp/servlet/velocity/nav.vm" )
		<div class="messaging"><p id="messageClose">X</p><p id="message"></p></div>  

			<div class="content">
#if($errorMsg)
				<div class="box-error-message">$errorMsg</div>
#else
#if($error_message != "null")
				<div class="box-error-message">$error_message</div>
#elseif($success_message != "null")
				<div class="box-success-message">$success_message</div>
#end

				<div id="all-jobs-content">
					<div class="section-hd">
						<h2><a href="${context}/manager?project=${project.name}&permissions">Permissions</a></h2>
						<div class="section-sub-hd">
							<h4><a href="${context}/manager?project=${project.name}">Project <span>$project.name</span></a></h4>
						</div>
					</div>
				</div>

				<div id="project-users">
					<table class="user-table">
						<tr><td class="first">Project Admins:</td><td>$admins</td></tr>
						<tr><td class="first">Your Permissions:</td><td>$userpermission.toString()</td></tr>
						
						<tr><td class="first"></td></tr>
					</table>
					
					#if($isAdmin)
						<button id="addUser" class="btn1">Add User</button>
						<button id="addGroup" class="btn1">Add Group</button>
						<button id="addProxyUser" class="btn2">Add Proxy User</button>
					#end
				</div>

				<div id="project-summary">
					<table class="summary-table">
						<tr><td class="first">Name:</td><td>$project.name</td></tr>
						<tr><td class="first">Created Date:</td><td>$utils.formatDate($project.lastModifiedTimestamp)</td></tr>
						<tr><td class="first">Modified Date:</td><td>$utils.formatDate($project.createTimestamp)</td></tr>
						<tr><td class="first">Last Modified by:</td><td>$project.lastModifiedUser</td></tr>
						<tr><td class="first">Description:</td><td id="pdescription">$project.description</td>
						</tr>
					</table>
				</div>

		<table id="permissions-table" class="all-jobs permission-table">
			<thead>
				<tr>
					<th class="tb-username">User</th>
					<th class="tb-perm">Admin</th>
					<th class="tb-read">Read</th>
					<th class="tb-write">Write</th>
					<th class="tb-execute">Execute</th>
					<th class="tb-schedule">Schedule</th>					
					#if($isAdmin)
						<th class="tb-action"></th>
					#end
				</tr>
			</thead>
			<tbody>
#if($permissions)
#foreach($perm in $permissions)
	<tr id="${perm.first}-row" >
		<td class="tb-name">#if($perm.first == $username) ${perm.first} <span class="sublabel">(you)</span> #else $perm.first #end</td>
		#if ($perm.second.isPermissionNameSet("ADMIN")) 
			<td><input id="${perm.first}-admin-checkbox" type="checkbox" name="admin" disabled="disabled" checked="true"></input></td>
			<td><input id="${perm.first}-read-checkbox" type="checkbox" name="read" disabled="disabled" checked="true"></input></td>
			<td><input id="${perm.first}-write-checkbox" type="checkbox" name="write" disabled="disabled" checked="true"></input></td>
			<td><input id="${perm.first}-execute-checkbox" type="checkbox" name="execute" disabled="disabled" checked="true"></input></td>
			<td><input id="${perm.first}-schedule-checkbox" type="checkbox" name="schedule" disabled="disabled" checked="true"></input></td>
		#else
			<td><input id="${perm.first}-admin-checkbox" type="checkbox" name="admin" disabled="disabled"></input></td>
			<td><input id="${perm.first}-read-checkbox" type="checkbox" name="read" disabled="disabled"  #if ($perm.second.isPermissionNameSet("READ")) checked="true" #end></input></td>
			<td><input id="${perm.first}-write-checkbox" type="checkbox" name="write" disabled="disabled" #if ($perm.second.isPermissionNameSet("WRITE")) checked="true" #end></input></td>
			<td><input id="${perm.first}-execute-checkbox" type="checkbox" name="execute" disabled="disabled" #if ($perm.second.isPermissionNameSet("EXECUTE")) checked="true" #end></input></td>
			<td><input id="${perm.first}-schedule-checkbox" type="checkbox" name="schedule" disabled="disabled" #if ($perm.second.isPermissionNameSet("SCHEDULE")) checked="true" #end></input></td>
		#end

		#if($isAdmin)
			<td><button id="$perm.first" #if($perm.first == $username) disabled="disabled" class="change-btn btn-disabled" #else class="change-btn btn2" #end >Change</button></td>
		#end
	</tr>
#end
#else
	<tr><td class="last">No Users Found.</td></tr>
#end
			</tbody>
		</table>

		<table id="group-permissions-table" class="all-jobs permission-table">
			<thead>
				<tr>
					<th class="tb-username">Group</th>
					<th class="tb-perm">Admin</th>
					<th class="tb-read">Read</th>
					<th class="tb-write">Write</th>
					<th class="tb-execute">Execute</th>
					<th class="tb-schedule">Schedule</th>
					#if($isAdmin)
						<th class="tb-action"></th>
					#end
				</tr>
			</thead>
			<tbody>
#if($groupPermissions)
#foreach($perm in $groupPermissions)
	<tr id="${perm.first}-row" >
		<td class="tb-name">#if($perm.first == $username) ${perm.first} <span class="sublabel">(you)</span> #else $perm.first #end</td>
		#if ($perm.second.isPermissionNameSet("ADMIN")) 
			<td><input id="group-${perm.first}-admin-checkbox" type="checkbox" name="admin" disabled="disabled" checked="true"></input></td>
			<td><input id="group-${perm.first}-read-checkbox" type="checkbox" name="read" disabled="disabled" checked="true"></input></td>
			<td><input id="group-${perm.first}-write-checkbox" type="checkbox" name="write" disabled="disabled" checked="true"></input></td>
			<td><input id="group-${perm.first}-execute-checkbox" type="checkbox" name="execute" disabled="disabled" checked="true"></input></td>
			<td><input id="group-${perm.first}-schedule-checkbox" type="checkbox" name="schedule" disabled="disabled" checked="true"></input></td>
		#else
			<td><input id="group-${perm.first}-admin-checkbox" type="checkbox" name="admin" disabled="disabled"></input></td>
			<td><input id="group-${perm.first}-read-checkbox" type="checkbox" name="read" disabled="disabled"  #if ($perm.second.isPermissionNameSet("READ")) checked="true" #end></input></td>
			<td><input id="group-${perm.first}-write-checkbox" type="checkbox" name="write" disabled="disabled" #if ($perm.second.isPermissionNameSet("WRITE")) checked="true" #end></input></td>
			<td><input id="group-${perm.first}-execute-checkbox" type="checkbox" name="execute" disabled="disabled" #if ($perm.second.isPermissionNameSet("EXECUTE")) checked="true" #end></input></td>
			<td><input id="group-${perm.first}-schedule-checkbox" type="checkbox" name="schedule" disabled="disabled" #if ($perm.second.isPermissionNameSet("SCHEDULE")) checked="true" #end></input></td>
		#end

		#if($isAdmin)
			<td><button id="group-$perm.first" class="change-btn btn2">Change</button></td>
		#end
	</tr>
#end
#else
	<tr><td class="last">No Groups Found.</td></tr>
#end
			</tbody>
		</table>
		
		<br></br>
		<table id="proxy-user-table" class="all-jobs permission-table">
			<thead>
				<tr>
					<th class="tb-username">Proxy User</th>
					<th class="tb-perm">Admin</th>
					<th class="tb-read">Read</th>
					<th class="tb-write">Write</th>
					<th class="tb-execute">Execute</th>
					<th class="tb-schedule">Schedule</th>
					<th class="tb-proxy">Proxy</th>
					#if($isAdmin)
						<th class="tb-action"></th>
					#end
				</tr>
			</thead>
			<tbody>
#if($proxyUsers)
#foreach($proxyUser in $proxyUsers)
	<tr id="${proxyUser}-row" >
		<td class="tb-name">#if($proxyUser == $username) ${proxyUser} <span class="sublabel">(you)</span> #else $proxyUser #end</td>
			<td><input id="proxy-${proxyUser}-admin-checkbox" type="checkbox" name="admin" disabled="disabled" ></input></td>
			<td><input id="proxy-${proxyUser}-read-checkbox" type="checkbox" name="read" disabled="disabled" ></input></td>
			<td><input id="proxy-${proxyUser}-write-checkbox" type="checkbox" name="write" disabled="disabled" ></input></td>
			<td><input id="proxy-${proxyUser}-execute-checkbox" type="checkbox" name="execute" disabled="disabled" ></input></td>
			<td><input id="proxy-${proxyUser}-schedule-checkbox" type="checkbox" name="schedule" disabled="disabled" ></input></td>
			<td><input id="proxy-${proxyUser}-proxy-checkbox" type="checkbox" name="proxy" disabled="disabled" checked="true"></input></td>

		#if($isAdmin)
			<td><button id="proxy-${proxyUser}" class="change-btn btn2">Change</button></td>
		#end
	</tr>
#end
#else
	<tr><td class="last">No Proxy User Found.</td></tr>
#end
			</tbody>
		</table>
#end

		</div>
	
		<div id="change-permission" class="modal">
			<h3 id="change-title">Change Permissions</h3>
			<div id="errorMsg" class="box-error-message"></div>
			<div class="message">
				<fieldset>
					<dl>
						<dt>User</dt>
						<dd><input id="user-box" name="userid" type="text" /></dd>
					<div id="otherCheckBoxes">
						<dt class="nextline">Admin</dt>
						<dd><input id="admin-change" name="admin" type="checkbox" /></dd>
						<dt>Read</dt>
					    <dd><input id="read-change" name="read" type="checkbox" /></dd>
					    <dt>Write</dt>
					    <dd><input id="write-change" name="write" type="checkbox" /></dd>
					    <dt>Execute</dt>
					    <dd><input id="execute-change" name="execute" type="checkbox" /></dd>
					    <dt>Schedule</dt>
					    <dd><input id="schedule-change" name="schedule" type="checkbox" /></dd>
					</div>
					<div id="proxyCheckBox" hidden=true>
				    	<dt>Proxy</dt>
				    	<dd><input id="proxy-change" name="proxy" type="checkbox" /></dd>
				    </div>
					</dl>
				</fieldset>
			</div>
			<div class="actions">
				<a class="yes btn2" id="change-btn" href="#">Commit</a>
				<a class="no simplemodal-close btn3" href="#">Cancel</a>
			</div>
			<div id="invalid-session" class="modal">
				<h3>Invalid Session</h3>
				<p>Session has expired. Please re-login.</p>
				<div class="actions">
					<a class="yes btn2" id="login-btn" href="#">Re-login</a>
				</div>
			</div>
		</div>
	</body>
</html>