<?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"' : '';
	$vid = intval($row['vehicle_id']) ? $row['vehicle_id'] : -1;

	$users	= $sableDB->getRows("select * from team__user where team_id = ".$hddnTeamId);
	$days	= $sableDB->getRows("select * from team__dow where team_id = ".$hddnTeamId);

	$mon = '';
	$tue = '';
	$wed = '';
	$thu = '';
	$fri = '';
	$sat = '';
	$sun = '';
	foreach($days as $day)
	{
		switch(intval($day['dow_id']))
		{
			case 1: {$mon = 'checked'; break;}
			case 2: {$tue = 'checked'; break;}
			case 3: {$wed = 'checked'; break;}
			case 4: {$thu = 'checked'; break;}
			case 5: {$fri = 'checked'; break;}
			case 6: {$sat = 'checked'; break;}
			case 7: {$sun = 'checked'; break;}
		}
	}

	$js = '<script>';
	//$js = '$(document).ready(function(e){';
	foreach($users as $user)
	{
		$js .= "document.getElementById('selUserCode').value = '".$user['user_code']."';".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-edit" aria-hidden="true"></i>&nbsp;Modify 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" onfocus="showHint('edtTeamName'); validate_edtTeamName();" onblur="hideHint('edtTeamName');" placeholder="Team Name" onkeypress="handleEnter(this, event);" 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">
					<div class="dropdown">
						<select name="selVehicleId" type="select" id="selVehicleId" onfocus="showHint('selVehicleId');" onblur="hideHint('selVehicleId'); validate_selVehicleId()" onkeypress="handleEnter(this, event)" placeholder="Vehicle">
						<?php echo(SableDBFunctions::getOptions("vehicle", "vehicle_id", "vehicle_name", $vid)); ?>
						</select>
					</div>
				</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" 
						   onfocus="showHint('cbTeamActive');" onblur="hideHint('cbTeamActive'); validate_cbTeamActive()" <?php echo($active); ?> placeholder="Active?" onkeypress="handleEnter(this, event)"/>
						<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">
					<label for="chkbxMonday" class="holder">
						<span style="margin-left: 6px;">Monday</span>
						<input type="checkbox" id="chkbxMonday" value="Monday" name="dayOfWeek" <?php echo($mon); ?> />
						<span class="checkmark"></span>
					</label>
					<label for="chkbxTuesday" class="holder">
						<span style="margin-left: 6px;">Tuesday</span>
						<input type="checkbox" id="chkbxTuesday" value="Tuesday" name="dayOfWeek" <?php echo($tue); ?>/>
						<span class="checkmark"></span>
					</label>
					<label for="chkbxWednesday" class="holder">
						<span style="margin-left: 6px;">Wednesday</span>
						<input type="checkbox" id="chkbxWednesday" value="Wednesday" name="dayOfWeek" <?php echo($wed); ?> />
						<span class="checkmark"></span>
					</label>
					<label for="chkbxThursday" class="holder">
						<span style="margin-left: 6px;">Thursday</span>
						<input type="checkbox" id="chkbxThursday" value="Thursday" name="dayOfWeek" <?php echo($thu); ?> />
						<span class="checkmark"></span>
					</label>
					<label for="chkbxFriday" class="holder">
						<span style="margin-left: 6px;">Friday</span>
						<input type="checkbox" id="chkbxFriday" value="Friday" name="dayOfWeek" <?php echo($fri); ?> />
						<span class="checkmark"></span>
					</label>
					<label for="chkbxSaturday" class="holder">
						<span style="margin-left: 6px;">Saturday</span>
						<input type="checkbox" id="chkbxSaturday" value="Saturday" name="dayOfWeek" <?php echo($sat); ?> />
						<span class="checkmark"></span>
					</label>
					<label for="chkbxSunday" class="holder">
						<span style="margin-left: 6px;">Sunday</span>
						<input type="checkbox" id="chkbxSunday" value="Sunday" name="dayOfWeek" <?php echo($sun); ?> />
						<span class="checkmark"></span>
					</label>
				</div>										
			</div>
			<p></p>
			
			<!--// selUserCode -->
			<div class="row forms-grid">
				<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="modifyTeam();" value="Update Team" class="submit"/>
				</div>
			</div>
		<!-- buttons //-->
		</div>
	</div>
</form>
<!-- Team //-->
<?php echo($js); ?>