<?php
include("user_func.php");
/*
$hddnUserCode = (empty($_SESSION['caller']['args'][1]) || !isset($_SESSION['caller']['args'][1]))?
				$_SESSION['user']['user_code']:$_SESSION['caller']['args'][1];
*/
$hddnUserCode = $_SESSION['caller']['args'][1];		
if ($_SESSION['user']['access_level'] < SABLE_ACCESS_MANAGER)
{
	if ((isset($hddnUserCode)) && ($hddnUserCode != $_SESSION['user']['user_code'])) SableSystem::callPage("user/view.php");
}
if ($_SESSION['user']['access_level'] < SABLE_ACCESS_ADMIN)
{
	$coyWhere = "where company_id = ".$_SESSION['company']['company_id'];
}

$usrSQL = 	"select * 
			from user 
			inner join access on user.access_id = access.access_id 
			where user_code = ".$hddnUserCode;

$row = $sableDB->getRow($usrSQL);

$onclick = "fileUpload('edtImage', 'imgUser', '3', false)";
$imgHTML = SableSystem::getClickableImgHTML($row['user_image'], 225, 'imgUser', $onclick);

//Get Addresses
$sql = "select * from user__address 
		inner join client on client.address_id = user__address.address_id
		where user_code = ".$hddnUserCode;
$addresses = $sableDB->getRows($sql);

//Build JS:
$js  = '<script>'.PHP_EOL;
$js .= 'var ddAddress = document.getElementById("selAddress");'.PHP_EOL;
$js .= 'var tfAddress = document.getElementById("edtAddress");'.PHP_EOL;

foreach($addresses as $address)
{
	$string = str_replace("\r", "\\r", $address['address']);
	$string = str_replace("\n", "\\n", $string);

	$js .= 'tfAddress.value = "'.$string.'";'.PHP_EOL;
	$js .= 'ddAddress.value = "'.$address['address_type_id'].'";'.PHP_EOL;		
	$js .= 'addAddress();'.PHP_EOL;
}

$js .= '</script>';
$poscls = $_SESSION['user']['access_level'] < $row['access_level'] ? ' disabled class="disabled noArrow" readonly ' : '';
$emailClass = $_SESSION['user']['position_id'] == 5 ? 'disabled class="disabled" ' : '';
?>
<script src="<?php SableGeneralFunctions::printModuleJS('user'); ?>" ></script>
<?php include(SABLE_INCLUDES_PATH.'googleMaps.php'); ?>
<!--// User -->

<form id="frmUser" name="frmUser" autocomplete="on">
	<input type="hidden" value="<?php echo(SableSystem::sableEncode($hddnUserCode)); ?>" id="hddnUserCode" name="hddnUserCode" />
    <div class="forms_">
        <div class="container">
            <div class="row forms-grids">
                <div class="col-md-12 form-grid">
                    <h3 class="title"><i class="fa fa-edit" aria-hidden="true"></i>&nbsp;Update Profile</h3>
                    <p></p>
                    <div class="clearfix"></div>
                </div>
            </div>
   
     		<!--// Profile Photo -->
            <div class="row forms-grids"> 
                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 forms-grid">
                    <div><?php echo($imgHTML); ?></div>
                </div>
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtImage" type="text" id="edtImage" onfocus="showHint('edtImage');" onblur="hideHint('edtImage'); 
                    validate_edtImage()" placeholder="Photo" onkeypress="handleEnter(this, event)" 
                    value="<?php echo($row['user_image'])?>" readonly  onClick="<?php echo($onclick); ?>" 
                    style="display:none;" />
                    <label class="fieldHint hintUnder" id="hint_edtImage">
                        <a href="javascript:<?php echo($onclick); ?>">
                            <i class="fa fa-upload"></i>&nbsp;Upload Photo
                        </a>
                    </label>
                </div>
            </div>       
    
         	<!--// Name & Surname -->
            <div class="row forms-grids"> 
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtName" type="text" id="edtName" onfocus="showHint('edtName');" onblur="hideHint('edtName'); 
                    validate_edtName()" placeholder="Name" onkeypress="handleEnter(this, event)" value="<?php echo($row['user_name'])?>" />
                    <label class="fieldHint hintUnder" id="hint_edtName">First Name</label>
                </div>                
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtSurname" type="text" id="edtSurname" onfocus="showHint('edtSurname');" onblur="hideHint('edtSurname');
                    validate_edtSurname()" placeholder="Surname" onkeypress="handleEnter(this, event)" 
                    value="<?php echo($row['user_surname'])?>" />
                    <label class="fieldHint hintUnder" id="hint_edtSurname">Surname</label>
                </div>
            </div>
            
         	<!--// Email Address & DOB -->
            <div class="row forms-grids">          
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtEmail" type="text" id="edtEmail" onfocus="showHint('edtEmail');" onblur="hideHint('edtEmail'); 
                    validate_edtEmail()" placeholder="Email" onkeypress="handleEnter(this, event)" <?php echo($emailClass); ?>
                    value="<?php echo($row['user_email'])?>" />
                    <label class="fieldHint hintUnder" id="hint_edtEmail">Email Address</label>
                </div>
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <select name="selPosition" type="select" id="selPosition" <?php echo($poscls); ?>
                    onfocus="showHint('selPosition');" onblur="hideHint('selPosition'); validate_selPosition()"
                    onkeypress="handleEnter(this, event)" placeholder="Position/Role">
                    <?php echo(SableDBFunctions::getOptions("position", "position_id", "position_title", $row['position_id'])); ?>
                    </select>
                    <label class="fieldHint hintUnder" id="hint_selPosition">Select Position/Role</label>
                </div>                
            </div>            
        
         	<!--// Phone Numbers -->
            <div class="row forms-grids">
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtCell" type="text" id="edtCell" onfocus="showHint('edtCell');" onblur="hideHint('edtCell'); 
                    validate_edtCellPhone()" placeholder="Cell No." onkeypress="handleEnter(this, event)" 
                    value="<?php echo($row['user_cell'])?>" />
                    <label class="fieldHint hintUnder" id="hint_edtCell">Cell Phone Number</label>
                </div>
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtPhone" type="text" id="edtPhone" onfocus="showHint('edtPhone');" onblur="hideHint('edtPhone');
                    validate_edtCellPhone()" placeholder="Phone No." onkeypress="handleEnter(this, event)" 
                    value="<?php echo($row['user_phone'])?>" />
                    <label class="fieldHint hintUnder" id="hint_edtPhone">Phone Number</label>
                </div>
            </div>
			
         	<!--// Company  -->
            <div class="row forms-grids">
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <select name="selCompanyId" type="select" id="selCompanyId" <?php echo($poscls); ?>
                    onfocus="showHint('selCompanyId');" onblur="hideHint('selCompanyId'); validate_selCompanyId()"
                    onkeypress="handleEnter(this, event)" placeholder="Employer">
                    <?php echo(SableDBFunctions::getOptions("company", "company_id", "company_name", $row['company_id'], $coyWhere)); ?>
                    </select>
                    <label class="fieldHint hintUnder" id="hint_selCompanyId">Employer</label>
                </div>
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                </div>
            </div>	
        
       		<!--// Add Addresses -->
            <div class="row forms-grids"> 
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
	                <label class="title">Addresses <span>
                    	<br />[Multiple addresses can be added. Type an address and click "+Address"]</span>
                    </label>
                    <select name="selAddress" id="selAddress" onkeypress="handleEnter(this, event)" placeholder="Address Type">
                        <?php SableDBFunctions::printOptions('address_type', 'address_type_id', 'address_type_name', -1); ?>
                    </select>
                    <label class="fieldHint hintUnder" id="hint_selAddress">Select address type</label>
                </div>
            </div>

            <div class="row forms-grids"> 
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <textarea name="edtAddress" type="text" id="edtAddress" placeholder="Address" sabledata="address"
                    onkeypress="handleEnter(this, event)" rows="6" onKeyUp="updateMapData();"></textarea>
                    <label class="fieldHint hintUnder" id="hint_edtAddress">Address</label>
                </div>
                <div class="col-xs-12 col-sm-6 col-md-2 col-lg-2 forms-grid">
                    <input id="btnAddAddress" type="button" onclick="addAddress()" value="+ Address" class="sub-button" />
                </div>                              
            </div>
            <div class="row forms-grids">
                <div class="col-md-12 forms-grid">
                	<div id="searchResults"></div>
                </div>
            </div>    

			<div class="row forms-grids">
				<div class="col-md-12 forms-grid">
					<div class="forms-grids" id="addressElement">
						<!--// Selected elements will be placed here. -->
					</div>
				</div>
			</div>
			
			<div class="row forms-grids"><p></p></div>
        
         	<!--// Password & Password Confirm -->
            <div class="row forms-grids"> 
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtPwd" type="password" id="edtPwd" onfocus="showHint('edtPwd');" onblur="hideHint('edtPwd');
                    validate_edtPwd()" placeholder="Password" onkeypress="handleEnter(this, event)" />
                    <label class="fieldHint hintUnder" id="hint_edtPwd">Change Password (optional)</label>
                </div>                
                <div class="col-xs-12 col-sm-10 col-md-8 col-lg-6 forms-grid">
                    <input name="edtConfirm" type="password" id="edtConfirm" onfocus="showHint('edtConfirm');" 
                    onblur="hideHint('edtConfirm'); validate_edtConfirm()" placeholder="Confirm password" 
                    onkeypress="handleEnter(this, event)"  />
                    <label class="fieldHint hintUnder" id="hint_edtConfirm">Confirm Password</label>
                </div>
            </div>   
  
        	<!--// buttons -->
            <div class="row forms-grids">
                <div class="col-md-2 forms-grid">
                	<input type="button" id="btnCancel" value="Cancel" class="submit" 
                	onclick="window.location = '<?php SableSystem::printCallPageLink('/user/'); ?>'" />
                </div>
                <div class="col-md-2 forms-grid">
                	<input id="btnOk" type="button" onclick="modifyUser()" value="Update User" class="submit"/>
                </div>
            </div>
		</div>
		<!-- buttons //--> 
	<?php include(SABLE_INCLUDES_PATH.'googleMapsDisplay.php'); ?>
	</div>
</form>
<!-- User //--> 
<?php echo($js); ?>