<?php
/**
* @version 1.0.0
* @package RStickets! 1.0.0
* @copyright (C) 2007-2008 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

// DIE()
defined('_JEXEC') or die('Restricted access');

// MUST BE LOGGED IN & ADMIN PERMISSIONS
if (!isset($_SESSION['RST_User']['UserId']) || (isset($_SESSION['RST_User']['Admin']) && $_SESSION['RST_User']['Admin'] == 0)) rst_admin_redirect('?option=com_rstickets');
if (isset($_GET['id']) && $_GET['id'] != '') $staff = rst_get_staff($_GET['id']);
if (isset($staff) && count($staff) == 0) rst_admin_redirect('?option=com_rstickets&task=staff');

if (isset($staff) && count($staff) > 0)
	$users = rst_get_users(0,0,1,1);
else
	$users = rst_get_users(0,0,1,0);
	
$groups = rst_get_groups();
$departments = rst_get_departments();
if (isset($staff) && count($staff) > 0)
	$staff_to_departments = rst_get_staff_to_departments($staff[0]['UserId']);
?>
<h4 class="rst_h4"><?php echo isset($staff) ? LBL_EDIT_STAFF : LBL_NEW_STAFF ?></h4>
<!--<a href="<?php echo RST_BACKEND_SCRIPT_PATH;?>?option=com_rstickets&task=staff" class="backButton"><?php echo BTN_BACK; ?></a>-->
<form method="post" action="<?php echo RST_BACKEND_SCRIPT_PATH;?>?option=com_rstickets&task=add_staff&amp;action=add_staff" name="add_staff">
<input type="hidden" name="add" value="add_staff"/>
<table cellpadding="4" cellspacing="0" class="tableGrey">
	<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_STAFF_PROPERTIES; ?></td>
	</tr>
	</thead>
	<tbody>
	<?php
	if (!isset($staff) &&(count($users) > 0)) {
	?>
	<tr>
			<td class="tdGrey td250"><?php echo BTN_SEARCH; ?><img src="<?php echo RST_FRONTEND_REL_PATH; ?>/img/loading.gif" alt="" style="width: 13px; height: 13px; display: none" id="LoadingDiv" /></td>
			<td>
			<input type="text" name="" value="" onkeyup="setTimeout('rst_search_users(document.getElementById(\'rst_search_user\').value,\'<?php echo RST_BACKEND_SCRIPT_PATH;?>\')',750);" id="rst_search_user" />
			<input type="hidden" name="" value="<?php echo time().'000' - 4000; ?>" id="rst_last_search" />
			<input type="hidden" name="" value="" id="rst_last_search_value" />
			</td>
	</tr>
	<?php
	}
	?>
	<tr>
		<td class="tdGrey td250"><?php echo LBL_USERNAME; ?></td>
		<td>
			<div id="UserIdDiv">
			<?php
			echo isset($staff) ? '<input type="hidden" name="UserId" value="'.$staff[0]['UserId'].'" />' : '';
			if (count($users) > 0)
			{
			?>
			<select size="5" name="UserId" <?php echo isset($staff) ? 'disabled="disabled"' : '' ?>>
				<?php
				foreach ($users as $user)
					echo '<option value="'.$user['UserId'].'" '.(isset($staff) && $staff[0]['UserId']==$user['UserId'] ? 'selected="selected"' : '').'>'.$user['Username'].' ('.$user['Fullname'].')</option>';
				?>
			</select>
			<?php
			}
			else echo LBL_NO_USERS;
			?>
			</div>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_GROUP; ?></td>
		<td>
			<?php if (count($groups) > 0) { ?>
			<select name="GroupId">
				<?php
				foreach ($groups as $group)
					echo '<option value="'.$group['GroupId'].'" '.(isset($staff) && $staff[0]['GroupId']==$group['GroupId'] ? 'selected="selected"' : '').'>'.$group['GroupName'].'</option>';
				?>
			</select>
			<?php } else echo '<strong>'.LBL_NO_GROUPS.'</strong>'; ?>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_DEPARTMENTS; ?><br/><?php echo LBL_DEPARTMENTS_DESC; ?></td>
		<td>
			<?php if (count($departments) > 0) { ?>
			<select name="DepartmentId[]" size="5" multiple="multiple">
				<?php
				foreach ($departments as $department)
				{
					echo '<option value="'.$department['DepartmentId'].'" ';
					if (isset($staff_to_departments) && count($staff_to_departments) > 0)
						foreach ($staff_to_departments as $staff_to_department)
							if ($staff_to_department['DepartmentId'] == $department['DepartmentId']) echo 'selected="selected"';
					echo '>'.$department['DepartmentName'].'</option>';
				}
				?>
			</select>
			<?php } else echo '<strong>'.LBL_NO_DEPARTMENTS.'</strong>'; ?>
		</td>
	</tr>
	</tbody>
</table>
<p><?php if(count($departments) > 0 && count($groups) > 0 && count($users) > 0)  $GLOBALS['showSaveStaff'] = true;
else 
{
	$GLOBALS['showSaveStaff'] = false; 
	echo '<p class="rst_message rst_error">'.LBL_NO_DEPARTMENTS_STAFF_USERS.'</p>';
}
?></p>
</form>