<?php
	include("team_func.php");
	$hddnTeamId = $_SESSION['caller']['args'][1];
	if ($_SESSION['user']['access_level'] < SABLE_ACCESS_MANAGER)
	{
		SableGeneralFunctions::divertFromPage();
	}
	$row = $sableDB->getRow("select * from team where team_id = ".$hddnTeamId);
	$active = intval($row['team_active']) ? 'checked="checked"' : '';
	$selVehicleId  = $sableDB->getValue("select vehicle_name from `vehicle` where vehicle_id = '".$row["vehicle_id"]."'");
	if (!$selVehicleId) $selVehicleId = null;
	$users = $sableDB->getRows("select * from team__user where team_id = ".$hddnTeamId);

	$days	= $sableDB->getValue("select group_concat(' ', dow_long)
								from team__dow 
								inner join dow on dow.dow_id = team__dow.dow_id 
								where team_id = ".$hddnTeamId." 
								order by team__dow.dow_id");

	$js = '<script>';
	//$js = '$(document).ready(function(e){';
	foreach($users as $user)
	{
		$js .= "document.getElementById('selUserCode').value = '".$user['user_code']."';".PHP_EOL;
		$js .= "userItem.setReadOnly(true)".PHP_EOL;
		$js .= "userItem.addItem(document.getElementById('selUserCode'));".PHP_EOL;
	}
	$js .= '</script>';
?>
<script src="<?php SableGeneralFunctions::printModuleJS('team'); ?>"></script>

<!--// Team -->
<form id="frmTeam" name="frmTeam" autocomplete="on">
	<input type="hidden" value="<?php echo(SableSystem::sableEncode($hddnTeamId)); ?>" id="hddnTeamId" name="hddnTeamId" />
	<div class="forms">
		<div class="container forms-grids">
			<div class="row forms-grid">
				<div class="col-md-12">
					<h3 class="title"><i class="fa fa-eye" aria-hidden="true"></i>&nbsp;View Team</h3>
					<p></p>
				</div>
			</div>
			<!--// edtTeamName -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<input name="edtTeamName" type="text" id="edtTeamName"readonly value="<?php echo($row['team_name']); ?>" >
				</div>
				<div class="col-md-8">
					<div class="fieldHint" id="hint_edtTeamName">Team Name</div>
				</div>
			</div>
			<p></p>
			<!-- edtTeamName //-->
			<p></p>
			<!--// selVehicleId -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<input name="selVehicleId" type="text" id="selVehicleId"readonly value="<?php echo($selVehicleId); ?>" >
				</div>
				<div class="col-md-8">
					<div class="fieldHint" id="hint_selVehicleId">Vehicle</div>
				</div>
			</div>
			<p></p>
			<!-- selVehicleId //-->
			
			<p></p>
			<!--// cbTeamActive -->
			<div class="row forms-grid">
				<div class="col-md-4 forms-grid">
					<div class="switch">
					<input name="cbTeamActive" type="checkbox" id="cbTeamActive" class="form-toggle form-toggle-yes-no" disabled
						   onfocus="showHint('cbTeamActive');" onblur="hideHint('cbTeamActive');" <?php echo($active); ?> placeholder="Active?"/>
<label for="cbTeamActive" data-on="Yes" data-off="No"></label>
					</div>
				</div>
				<div class="col-md-8">
					<div class="fieldHint" id="hint_cbTeamActive">Active?</div>
				</div>
			</div>
			<p></p>
			<!-- cbTeamActive //-->
			
			<div class="row forms-grid">
				<div class="col-sm-12">
					<h4 class="panel-title">Active Days</h4>
				</div>
				<div class="col-sm-12" id="divDayOfWeek">
					<?php echo($days); ?>
				</div>										
			</div>
			<p></p>			
			
			<!--// selUserCode -->
			<div class="row forms-grid" style="display:none;">
				<div class="col-md-4 forms-grid">
					<div class="dropdown">
						<select name="selUserCode" type="select" id="selUserCode" onfocus="showHint('selUserCode');" 
								onblur="hideHint('selUserCode');" onkeypress="handleEnter(this, event)" placeholder="User" onChange="userItem.addItem(this);">
						<?php echo(SableDBFunctions::getOptions("user", "user_code", "concat(user_name, ' ', user_surname)", -1, "where (user_code > 0) and (deleted = 0)")); ?>
						</select>
					</div>
				</div>
				<div class="col-md-8">
					<div class="fieldHint" id="hint_selUserCode">User</div>
				</div>
			</div>

			<div class="row forms-grid">
				<div id="UserElement">
					<!--// Selected elements will be placed here. -->
				</div>
			</div>
			<p></p>
			<!-- selUserCode //-->			
			
			<p></p>
		<!--// buttons -->
			<div class="row forms-grid">
				<div class="col-md-2">
					<input type="button" id="btnCancel" value="Cancel"
					class="submit" onclick="window.location = '<?php SableSystem::printCallPageLink('/team/'); ?>'"/>
				</div>
				<div class="col-md-2 forms-grid">
					<input id="btnOk" type="button" onclick="editTeam('<?php echo(SableSystem::sableEncode($hddnTeamId)); ?>')" value="Modify Team" class="submit"/>
				</div>
			</div>
		<!-- buttons //-->
		</div>
	</div>
</form>
<!-- Team //-->
<?php echo($js); ?>