<?php
use Orchard\Service\Encryption\EncryptionManager;

$canImpersonate = $this->view->canImpersonate;
$canShowLegacyLogin = !$this->view->isWorkstationImpersonateLinkInOaFFEnabled;

$config = Zend_Registry::get('configuration');
$session_id = session_id();
# encrypt the session id
$auth0Config = yaml_parse_file(APPLICATION_PATH . '/configs/auth0.yml');
$encryptionHelper = new EncryptionManager();
$sid = $encryptionHelper->encryptWithCustomKey($session_id, $auth0Config['client_id']);

?>
<div class="panelHeader">
	<h3>Sub Account Contacts</h3>
</div>
<div class="panelContent">
	<table class="data" id="sub-account-contacts-table">
		<tr>
			<th>Name</th>
			<th class="center">Login</th>
			<th class="center">Active</th>
			<th class="center">Email</th>
			<th class="center">Phone</th>
			<th class="center">Address</th>
			<th class="center">Country</th>
			<th></th>
		</tr>
		<?	if (count($this->view->labelContacts)>0) {
				$rci = 0;
				foreach($this->view->labelContacts as $vendContact) {
					$row_class = (++$rci & 1) ? '' : 'alt'; ?>
					<form name="login<?=$vendContact->id?>" action="<?=$this->view->workstation_login_url?>" target="_blank" method="post">
						<input type="hidden" name="username" value="<?=$vendContact->login?>" />
						<input type="hidden" name="session_id" value="<?=$this->view->session_id?>" />
						<input type="hidden" name="vend_contact_id" value="<?=$vendContact->id?>" />
					</form>
					<tr class="<?=$row_class?><?=($vendContact->master == 'Y' ? ' primaryContact' : '')?><?=($vendContact->active == 'N' ? ' taskClosed' : '')?>">
						<td><?=(($vendContact->active=="Y") ? $vendContact->Contact->contact_first_name." ".$vendContact->Contact->contact_last_name : ("<strike>".$vendContact->Contact->contact_first_name." ".$vendContact->Contact->contact_last_name."</strike>"))?></td>
						<td>
							<?php if ($vendContact->Vendor->status == 'signed' || $vendContact->Vendor->status == 'deletion') {?>

									<?php
										if($vendContact->login){
											if ($vendContact->active=="Y") {
												if ($canImpersonate) { ?>
													<?= $vendContact->login ?>
													<a href="<?php echo $config->workstation_url; ?>#sid=<?php echo $sid; ?>&username=<?php echo $vendContact->login; ?>&vend_contact_id=<?php echo $vendContact->id; ?>" target="_blank">Login</a> |
													<a class="subaccount-contact-link-short" href="<?php echo $config->workstation_url; ?>?impersonate=alw:<?php echo $vendContact->id; ?>" target="_blank">Impersonate</a>
												<?php } elseif ($canShowLegacyLogin) { ?>
													<a href="<?php echo $config->workstation_url; ?>#sid=<?php echo $sid; ?>&username=<?php echo $vendContact->login; ?>&vend_contact_id=<?php echo $vendContact->id; ?>" target="_blank"><?= $vendContact->login ?></a>
												<?php } else { ?>
													<?= $vendContact->login ?>
												<?php }
											}else{
									?>
												<strike><?=$vendContact->login?></strike>
									<?php
											}
										}else{
											echo "-";?>
									<?php }
									?>
							<?php }?>
						</td>
						<td><?=$vendContact->active?></td>
						<td>
							<?php
								if($vendContact->active=="Y"){
									?><a href="mailto:<?=$vendContact->Contact->contact_email?>" title="Send Email"><?=$vendContact->Contact->contact_email?></a>
									<? if ($vendContact->Contact->alt_email) { ?><br />Alt. <a href="mailto:<?=$vendContact->Contact->alt_email?>" title="Send Email"><?=$vendContact->Contact->alt_email?></a><? } ?><?php
								}else{
									?><strike><?=$vendContact->Contact->contact_email?>
								<? if($vendContact->Contact->alt_email){ ?><br />Alt. <?=$vendContact->Contact->alt_email?><? } ?></strike><?php
								}
							?>
						</td>
						<td><?=(($vendContact->active=="Y") ? $vendContact->Contact->contact_phone : ("<strike>".$vendContact->Contact->contact_phone."</strike>"))?></td>
						<td>
							<? if ($vendContact->Contact->address_street) { ?>
								<?=(($vendContact->active=="Y") ? $vendContact->Contact->address_street : ("<strike>".$vendContact->Contact->address_street."</strike>"))?><br />
							<?	} ?>
							<? if ($vendContact->Contact->address2) { ?>
								<?=(($vendContact->active=="Y") ? $vendContact->Contact->address2 : ("<strike>".$vendContact->Contact->address2."</strike>"))?><br />
							<?	} ?>
							<?=(($vendContact->active=="Y") ? $vendContact->Contact->address_city : ("<strike>".$vendContact->Contact->address_city.", ".$vendContact->Contact->OrchardState->name." ".$vendContact->Contact->address_zip."</strike>"))?>
						</td>
						<td><?=(($vendContact->active=="Y") ? $vendContact->Contact->Country->name : ("<strike>". $vendContact->Contact->Country->name."</strike>"))?></td>
						<td nowrap="nowrap">
							<?
							    $s_roles = explode(',', $_SESSION['S_ROLES']);
							    if ($vendContact->Vendor->status != 'deletion' || $_SESSION['S_PRIMARY_ROLE_ID'] == 15 || in_array('15', $s_roles)) { ?>
									<a href="/cont_mgmt/edit_label_contact.php?vendor_id=<?=$vendContact->Vendor->vendor_id?>&contact_id=<?=$vendContact->contact_id?>" title="Edit">Edit</a>
									<? /*if ($vendContact['master'] == 'N') { ?> &nbsp;&nbsp;&nbsp; <a href="#" onclick="deleteConfirm('view_vendor.php?vendor_id=<?=$vendor_id?>&delete=1&contact_id=<?=$vendContact['contact_id']?>');" title="Remove">Remove</a><? }*/ ?>
							<?	} ?>
						</td>
					</tr>
			<?	}
		} ?>
	</table>
</div>
