<?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'] != '') $department = rst_get_departments(intval($_GET['id']));
if (isset($department) && count($department) == 0) rst_admin_redirect('?option=com_rstickets&task=departments');
?>
<h4 class="rst_h4"><?php echo isset($department) ? LBL_EDIT_DEPARTMENT : LBL_NEW_DEPARTMENT ?></h4>
<!--<a href="<?php echo RST_BACKEND_SCRIPT_PATH;?>?option=com_rstickets&amp;task=departments" class="backButton"><?php echo BTN_BACK; ?></a>-->
<form method="post" action="<?php echo RST_BACKEND_SCRIPT_PATH;?>?option=com_rstickets&amp;task=add_department&amp;action=add_department"  name="add_department">
<input type="hidden" name="DepartmentId" value="<?php echo isset($department) ? $department[0]['DepartmentId'] : 0 ?>" />
<table cellpadding="4" cellspacing="0" class="tableGrey">
	<thead>
	<tr class="tableHead">
		<td colspan="2"><?php echo LBL_DEPARTMENT_PROPERTIES; ?></td>
	</tr>
	</thead>
	<tbody>
	<tr>
		<td class="tdGrey td250"><?php echo LBL_NAME; ?></td>
		<td><input type="text" name="DepartmentName" value="<?php echo isset($department[0]['DepartmentName']) ? $department[0]['DepartmentName'] : (isset($_POST['DepartmentName']) ? $_POST['DepartmentName'] : '') ?>" /></td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_PREFIX; ?></td>
		<td><input type="text" name="DepartmentPrefix" value="<?php echo isset($department[0]['DepartmentPrefix']) ? $department[0]['DepartmentPrefix'] : (isset($_POST['DepartmentPrefix']) ? $_POST['DepartmentPrefix'] : '') ?>" /></td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_TICKET_ASSIGNMENT_TYPE; ?></td>
		<td>
			<select name="TicketAssignmentType">
				<option value="static" <?php echo isset($department[0]['TicketAssignmentType']) && $department[0]['TicketAssignmentType'] == 'static' ? 'selected="selected"' : (isset($_POST['TicketAssignmentType']) && $_POST['TicketAssignmentType'] == 'static' ? 'selected="selected"' : '') ?>>static</option>
				<option value="automatic" <?php echo isset($department[0]['TicketAssignmentType']) && $department[0]['TicketAssignmentType'] == 'automatic' ? 'selected="selected"' : (isset($_POST['TicketAssignmentType']) && $_POST['TicketAssignmentType'] == 'automatic' ? 'selected="selected"' : '') ?>>automatic</option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_TICKET_GENERATION_RULE; ?></td>
		<td>
			<select name="TicketGenerationRule">
				<option value="sequential" <?php echo isset($department[0]['TicketGenerationRule']) && $department[0]['TicketGenerationRule'] == 'sequential' ? 'selected="selected"' : (isset($_POST['TicketGenerationRule']) && $_POST['TicketGenerationRule'] == 'sequential' ? 'selected="selected"' : '') ?>>sequential</option>
				<option value="random" <?php echo isset($department[0]['TicketGenerationRule']) && $department[0]['TicketGenerationRule'] == 'random' ? 'selected="selected"' : (isset($_POST['TicketGenerationRule']) && $_POST['TicketGenerationRule'] == 'random' ? 'selected="selected"' : '') ?>>random</option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_PRIORITY; ?></td>
		<td>
			<select name="DepartmentPriority">
				<?php
				$priority_array = array('low','normal','high');
				foreach ($priority_array as $priority)
					echo '<option '.(isset($department[0]['DepartmentPriority']) && $department[0]['DepartmentPriority'] == $priority ? 'selected="selected"' : (isset($_POST['DepartmentPriority']) && $_POST['DepartmentPriority'] == $priority ? 'selected="selected"' : '')).' value="'.$priority.'">'.$priority_array_lang[$priority].'</option>';
				?>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_CUSTOMER_RECEIVES; ?></td>
		<td>
			<select name="UserEmailTicket">
				<option value="1" <?php echo isset($department[0]['UserEmailTicket']) && $department[0]['UserEmailTicket'] == '1' ? 'selected="selected"' : (isset($_POST['UserEmailTicket']) && $_POST['UserEmailTicket'] == '1' ? 'selected="selected"' : '') ?>><?php echo LBL_YES; ?></option>
				<option value="0" <?php echo isset($department[0]['UserEmailTicket']) && $department[0]['UserEmailTicket'] == '0' ? 'selected="selected"' : (isset($_POST['UserEmailTicket']) && $_POST['UserEmailTicket'] == '0' ? 'selected="selected"' : '') ?>><?php echo LBL_NO; ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_STAFF_RECEIVES; ?></td>
		<td>
			<select name="StaffEmailTicket">
				<option value="1" <?php echo isset($department[0]['StaffEmailTicket']) && $department[0]['StaffEmailTicket'] == '1' ? 'selected="selected"' : (isset($_POST['StaffEmailTicket']) && $_POST['StaffEmailTicket'] == '1' ? 'selected="selected"' : '') ?>><?php echo LBL_YES; ?></option>
				<option value="0" <?php echo isset($department[0]['StaffEmailTicket']) && $department[0]['StaffEmailTicket'] == '0' ? 'selected="selected"' : (isset($_POST['StaffEmailTicket']) && $_POST['StaffEmailTicket'] == '0' ? 'selected="selected"' : '') ?>><?php echo LBL_NO; ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_FILE_UPLOAD; ?></td>
		<td>
			<select name="FileUpload">
				<option value="yes" <?php echo isset($department[0]['FileUpload']) && $department[0]['FileUpload'] == 'yes' ? 'selected="selected"' : (isset($_POST['FileUpload']) && $_POST['FileUpload'] == 'yes' ? 'selected="selected"' : '') ?>><?php echo LBL_EVERYONE; ?></option>
				<option value="users" <?php echo isset($department[0]['FileUpload']) && $department[0]['FileUpload'] == 'users' ? 'selected="selected"' : (isset($_POST['FileUpload']) && $_POST['FileUpload'] == 'users' ? 'selected="selected"' : '') ?>><?php echo LBL_REGISTERED; ?></option>
				<option value="no" <?php echo isset($department[0]['FileUpload']) && $department[0]['FileUpload'] == 'no' ? 'selected="selected"' : (isset($_POST['FileUpload']) && $_POST['FileUpload'] == 'no' ? 'selected="selected"' : '') ?>><?php echo LBL_NOBODY; ?></option>
			</select>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_FILE_UPLOAD_EXTENSIONS; ?></td>
		<td>
			<textarea name="FileUploadExtensions" cols="30" rows="5"><?php echo isset($department[0]['FileUploadExtensions']) ? implode("\n",$department[0]['FileUploadExtensions']) : (isset($_POST['FileUploadExtensions']) ? $_POST['FileUploadExtensions'] : '') ?></textarea>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_DEPARTMENT_NEW_TICKET_NOTIFY; ?></td>
		<td>
			<textarea name="NotifyNewTicketEmails" cols="30" rows="5"><?php echo isset($department[0]['NotifyNewTicketEmails']) ? $department[0]['NotifyNewTicketEmails'] : (isset($_POST['NotifyNewTicketEmails']) ? $_POST['NotifyNewTicketEmails'] : '') ?></textarea>
		</td>
	</tr>
	<tr>
		<td class="tdGrey"><?php echo LBL_DEPARTMENT_ASSIGN_NOTIFY; ?></td>
		<td>
			<select name="NotifyAssign">
				<option value="1" <?php echo isset($department[0]['NotifyAssign']) && $department[0]['NotifyAssign'] == '1' ? 'selected="selected"' : (isset($_POST['NotifyAssign']) && $_POST['NotifyAssign'] == '1' ? 'selected="selected"' : '') ?>><?php echo LBL_YES; ?></option>
				<option value="0" <?php echo isset($department[0]['NotifyAssign']) && $department[0]['NotifyAssign'] == '0' ? 'selected="selected"' : (isset($_POST['NotifyAssign']) && $_POST['NotifyAssign'] == '0' ? 'selected="selected"' : '') ?>><?php echo LBL_NO; ?></option>
			</select>
		</td>
	</tr>
	</tbody>
</table>
<p><a href="" onclick="add_custom_field();return false;" class="addButton"><?php echo BTN_NEW_CUSTOM_FIELD; ?></a></p>
<table cellpadding="4" cellspacing="0" id="custom_fields_table" class="tableGrey">
	<thead>
	<tr style="text-align:center" class="tableHead">
		<td class="tdGrey"><?php echo LBL_NAME; ?></td>
		<td class="tdGrey"><?php echo LBL_CAPTION; ?></td>
		<td class="tdGrey"><?php echo LBL_TYPE; ?></td>
		<td class="tdGrey"><?php echo LBL_VALUES; ?></td>
		<td class="tdGrey"><?php echo LBL_REQUIRED; ?></td>
		<td><?php echo LBL_ACTIONS; ?></td>
	</tr>
	</thead>
	<tbody>
<?php
if (isset($_POST['CustomFieldId']) && count($_POST['CustomFieldId']) > 0)
{
	foreach ($_POST['CustomFieldId'] as $i => $value)
	{
		$custom_fields[] = array(
		'CustomFieldId' => $_POST['CustomFieldId'][$i],
		'CustomFieldName' => $_POST['CustomFieldName'][$i],
		'CustomFieldLabel' => $_POST['CustomFieldLabel'][$i],
		'CustomFieldType' => $_POST['CustomFieldType'][$i],
		'CustomFieldValues' => $_POST['CustomFieldValues'][$i],
		'Required' => $_POST['Required'][$i],
		);
	}
}

if (isset($department[0]['DepartmentId']) || isset($custom_fields))
{
	$custom_fields = !isset($custom_fields) ? rst_get_departments_custom_fields($department[0]['DepartmentId']) : $custom_fields;
	if (count($custom_fields) > 0)
	{
		foreach ($custom_fields as $custom_field)
		{
			echo '<tr>
						<td class="tdGrey"><input type="hidden" name="CustomFieldId[]" value="'.$custom_field['CustomFieldId'].'" /><input type="text" name="CustomFieldName[]" value="'.$custom_field['CustomFieldName'].'" /></td>
						<td class="tdGrey"><input type="text" name="CustomFieldLabel[]" value="'.$custom_field['CustomFieldLabel'].'" /></td>
						<td class="tdGrey">
						<select name="CustomFieldType[]">
							<option value="textbox" '.($custom_field['CustomFieldType'] == 'textbox' ? 'selected="selected"' : '').'>'.LBL_TEXTBOX.'</option>
							<option value="textarea" '.($custom_field['CustomFieldType'] == 'textarea' ? 'selected="selected"' : '').'>'.LBL_TEXTAREA.'</option>
							<option value="select" '.($custom_field['CustomFieldType'] == 'select' ? 'selected="selected"' : '').'>'.LBL_SELECT_LIST.'</option>
							<option value="multipleselect" '.($custom_field['CustomFieldType'] == 'multipleselect' ? 'selected="selected"' : '').'>'.LBL_MULTIPLE_SELECT_LIST.'</option>
							<option value="checkbox" '.($custom_field['CustomFieldType'] == 'checkbox' ? 'selected="selected"' : '').'>'.LBL_CHECKBOX_GROUP.'</option>
							<option value="radio" '.($custom_field['CustomFieldType'] == 'radio' ? 'selected="selected"' : '').'>'.LBL_RADIO_GROUP.'</option>
							<option value="calendar" '.($custom_field['CustomFieldType'] == 'calendar' ? 'selected="selected"' : '').'>'.LBL_CALENDAR.'</option>
							<option value="calendartime" '.($custom_field['CustomFieldType'] == 'calendartime' ? 'selected="selected"' : '').'>'.LBL_CALENDAR_TIME.'</option>
						</select>
						</td>
						<td class="tdGrey"><textarea rows="3" cols="20" name="CustomFieldValues[]">'.$custom_field['CustomFieldValues'].'</textarea></td>
						<td class="tdGrey">
						<select name="Required[]">
						<option value="0" '.($custom_field['Required'] == 0 ? 'selected="selected"' : '').'>'.LBL_NO.'</option>
						<option value="1" '.($custom_field['Required'] == 1 ? 'selected="selected"' : '').'>'.LBL_YES.'</option>
						</select>
						</td>
						<td>
						<a onclick="return confirm(\''.LBL_SURE.'\')" href="'.RST_BACKEND_SCRIPT_PATH.'?option=com_rstickets&task=add_department'.(isset($department) ? '&amp;id='.$department[0]['DepartmentId'] : '').'&amp;action=delete_custom_field&amp;cid='.$custom_field['CustomFieldId'].'" class="deleteButton">'.BTN_DELETE.'</a>
						</td>
					</tr>';
		}
	}
	else
	echo '<tr id="no_fields"><td colspan="6"><strong>'.LBL_NO_CUSTOM_FIELDS.'</strong></td></tr>';
}
else
echo '<tr id="no_fields"><td colspan="6"><strong>'.LBL_NO_CUSTOM_FIELDS.'</strong></td></tr>';
?>
	</tbody>
</table>
<!--<p><button type="submit" style="display:none;"><?php echo BTN_SUBMIT; ?></button></p>-->
</form>