<!-- Begin toolbar -->
<div class="toolbar">
	<div class="toolbar-inner">
		<div class="toolbar-buttons">
			<a href="javascript:window.history.back();" class="button button-secondary button-back js-button-back">
				<i class="fas fa-arrow-left icon-blue"></i><?= _("Back") ?>
			</a>
			<a href="javascript:location.reload();" class="button button-secondary">
				<i class="fas fa-arrows-rotate icon-green"></i><?= _("Refresh") ?>
			</a>
		</div>
		<div class="toolbar-right">
			<div class="toolbar-sorting">
				<button class="toolbar-sorting-toggle js-toggle-sorting-menu" type="button" title="<?= _("Sort items") ?>">
					<?= _("Sort by") ?>:
					<span class="u-text-bold">
						<?= _("Date") ?> <i class="fas fa-arrow-down-a-z"></i>
					</span>
				</button>
				<ul class="toolbar-sorting-menu animate__animated animate__fadeIn js-sorting-menu u-hidden">
					<li data-entity="sort-date" data-sort-as-int="1">
						<span class="name <?php if ($_SESSION['userSortOrder'] === 'date') { echo 'active'; } ?>"><?= _("Date") ?> <i class="fas fa-arrow-down-a-z"></i></span><span class="up"><i class="fas fa-arrow-up-a-z"></i></span>
					</li>
					<li data-entity="sort-name">
						<span class="name <?php if ($_SESSION['userSortOrder'] === 'name') { echo 'active'; } ?>"><?= _("Name") ?> <i class="fas fa-arrow-down-a-z"></i></span><span class="up"><i class="fas fa-arrow-up-a-z"></i></span>
					</li>
				</ul>
				<div class="toolbar-search">
					<form action="/search/" method="get">
						<input type="hidden" name="token" value="<?= $_SESSION["token"] ?>">
						<input type="search" class="form-control js-search-input" name="q" value="<? echo isset($_GET['q']) ? htmlspecialchars($_GET['q']) : '' ?>" title="<?= _("Search") ?>">
						<button type="submit" class="toolbar-input-submit" title="<?= _("Search") ?>">
							<i class="fas fa-magnifying-glass"></i>
						</button>
					</form>
				</div>
			</div>
		</div>
	</div>
</div>
<!-- End toolbar -->

<div class="container">
	<div class="units js-units-container">
		<div class="header units-header">
			<div class="l-unit__col l-unit__col--right">
				<div class="clearfix l-unit__stat-col--left super-compact">
					&nbsp;
				</div>
				<div class="clearfix l-unit__stat-col--left u-text-center compact-2"><b><?= _("Status") ?></b></div>
				<div class="clearfix l-unit__stat-col--left wide-5"><b><?= _("Search Results") ?></b></div>
				<div class="clearfix l-unit__stat-col--left compact-3"><b>&nbsp;</b></div>
				<div class="clearfix l-unit__stat-col--left u-text-center"><b><?= _("Date") ?></b></div>
				<div class="clearfix l-unit__stat-col--left u-text-center"><b><?= _("Owner") ?></b></div>
				<div class="clearfix l-unit__stat-col--left u-text-center"><b><?= _("Type") ?></b></div>
			</div>
		</div>

		<!-- Begin search result item loop -->
		<?php
			foreach ($data as $key => $value) {
				++$i;

				if ($value['SUSPENDED'] == 'yes') {
					$status = 'suspended';
					$spnd_action = 'unsuspend';
				} else {
					$status = 'active';
					$spnd_action = 'suspend';
				}

				if ($value['TYPE'] == 'db') {
					$object = 'database';
				} else {
					$object = strtolower($value['TYPE'] . ' ' . $value['KEY']);
				}

				$uniq_id = $value['TYPE'] . '-';
				if ($value['KEY'] == 'ACCOUNT'){
					$uniq_id .= 'acc-';
				}
				$uniq_id .= sha1($value['RESULT']);
			?>
			<div class="l-unit <?php if($status == 'suspended') echo 'l-unit--suspended'; ?> animate__animated animate__fadeIn js-unit"
				data-uniq-id="<?=$uniq_id?>"
				data-sort-date="<?=strtotime($value['DATE'].' '.$value['TIME'])?>"
				data-sort-name="<?=$value['RESULT']?>"
				data-sort-type="<?=_($object)?>"
				data-sort-owner="<?=$value['USER']?>"
				data-sort-status="<?=$status?>"
				style="<?php if (($_SESSION['POLICY_SYSTEM_HIDE_ADMIN'] === 'yes') && ($value['USER']) === 'admin') { echo 'display: none;';}?>">

				<div class="l-unit__col l-unit__col--right">
					<div class="clearfix l-unit__stat-col--left super-compact u-text-center">
						<?php
							if ($object === 'web domain') {
									$icon = 'fa-earth-americas';
							}
							if ($object === 'mail domain') {
									$icon = 'fa-envelopes-bulk';
							}
							if ($object === 'dns domain') {
									$icon = 'fa-book-atlas';
							}
							if ($object === 'dns record') {
									$icon = 'fa-book-atlas';
							}
							if ($object === 'database') {
									$icon = 'fa-database';
							}
							if ($object === 'cron job') {
									$icon = 'fa-clock';
							}
						?>
						<i class="fa <?=$icon;?> icon-dim"></i>
					</div>
					<div class="clearfix l-unit__stat-col--left compact-2 u-text-center">
						<b>
							<?php if ($status === "active") { ?>
								<i class="fas fa-circle-check icon-green"></i>
							<?php } ?>
							<?php if ($status === "suspended") { ?>
								<i class="fas fa-triangle-exclamation icon-orange"></i>
							<?php } ?>
						</b>
					</div>
					<div class="clearfix l-unit__stat-col--left wide-5 truncate">
						<?php
							if ($value['KEY'] == 'RECORD') {
								$edit_lnk = '/edit/'.$value['TYPE'].'/?domain='.$value['PARENT'].'&record_id='.$value['LINK'].'&user='.$value['USER'];
							}
							if ($value['KEY'] == 'ACCOUNT') {
								$edit_lnk = '/edit/'.$value['TYPE'].'/?domain='.$value['PARENT'].'&account='.$value['LINK'].'&user='.$value['USER'];
							}
							if ($value['KEY'] == 'JOB') {
								$edit_lnk = '/edit/'.$value['TYPE'].'/?job='.$value['LINK'].'&user='.$value['USER'];
							}
							if ($value['KEY'] == 'DATABASE') {
								$edit_lnk = '/edit/'.$value['TYPE'].'/?database='.$value['RESULT'].'&user='.$value['USER'];
							}
							if (($value['KEY'] != 'RECORD') && ($value['KEY'] != 'ACCOUNT') && ($value['KEY'] != 'JOB') && ($value['KEY'] != 'DATABASE') ) {
								$edit_lnk = '/edit/'.$value['TYPE'].'/?'.strtolower($value['KEY']).'='.$value['RESULT'].'&user='.$value['USER'];
							}
						?>
						<b>
							<?php
								if (($_SESSION['userContext'] === 'admin') && ($_SESSION['user'] !== 'admin') && ($value['USER'] === 'admin') && ($_SESSION['POLICY_SYSTEM_PROTECTED_ADMIN'] === 'yes')) {
									echo $value['RESULT'];
								} else {
									if ($value['USER'] == $_SESSION['user']) {
										$href = $edit_lnk.'&token='.$_SESSION['token'];
									} else {
										$href = '/login/?loginas='.$value['USER'].'&token='.$_SESSION['token'].'&edit_link='.urlencode($edit_lnk);
									}
									echo '<a href="' . $href . '">' . $value['RESULT'] . '</a>';
								}
							?>
						</b>
					</div>
					<div class="clearfix l-unit__stat-col--left u-text-right compact-3">
						<div class="l-unit-toolbar__col l-unit-toolbar__col--right u-noselect">
							<div class="actions-panel clearfix">
								&nbsp;
							</div>
						</div>
					</div>
					<div class="clearfix l-unit__stat-col--left u-text-center"><?= translate_date($value["DATE"]) ?></div>
					<div class="clearfix l-unit__stat-col--left u-text-center">
						<b>
							<a href="/search/?q=<?=htmlentities($_GET['q']); ?>&u=<?=$value['USER']; ?>&token=<?=$_SESSION['token']?>"><?=$value['USER']; ?></a>
							<?php if (!($_SESSION['POLICY_SYSTEM_HIDE_ADMIN'] === 'yes' && $value['USER'] !== 'admin') && ($_SESSION['userContext'] === 'admin')) { ?>
								<a href="/login/?loginas=<?=$value['USER']?>&token=<?=$_SESSION['token']?>" title="<?= _("Log in as") ?> <?=$value['USER']?>">
									<i class="fas fa-right-to-bracket icon-green icon-dim"></i>
								</a>
							<?php } ?>
						</b>
					</div>
					<div class="clearfix l-unit__stat-col--left u-text-center"><?= _($object) ?></b></div>
				</div>
			</div>
		<?php } ?>
	</div>
</div>

<footer class="app-footer">
	<div class="container app-footer-inner">
		<p>
			<?php printf(ngettext("%d object", "%d objects", $i), $i); ?>
		</p>
	</div>
</footer>
