<?php
	require_once ("include/Configuration.inc.php");

	$pdo = null;
	$pdoex = null;

	try
	{
		$pdo = new \PDO("mysql:host=" . System::$Configuration["Database.ServerName"] . ";port=" . System::$Configuration["Database.PortNumber"] . ";dbname=" . System::$Configuration["Database.DatabaseName"], System::$Configuration["Database.UserName"], System::$Configuration["Database.Password"]);
	}
	catch (Exception $ex)
	{
		$pdoex = $ex;
		echo($pdoex); die();
	}

	$basepath = $_SERVER["SERVER_NAME"];
	
?>
<html>
	<head>
		<title>Tenant Management System</title>
		<link rel="stylesheet" type="text/css" href="style/avondale/avondale.min.css" />
		<style type="text/css">
		a.uwt-external-link::after
		{
			content: "\f08e";
			font-family: "Font Awesome 6 Pro";
			padding-left: 8px;
			font-weight: 300;
		}
		div.uwt-page-header > div.uwt-toolbar
		{
			background-color: #eee;
			padding: 16px;
			margin-bottom: -16px;
			margin-left: -16px;
			margin-right: -16px;
			border-top: 1px solid #aaa;
			margin-top: ;
			padding-bottom: 8px;
		}
		div.uwt-page-header > div.uwt-toolbar > a
		{
			color: #000;
			text-decoration: none;
			padding: 8px;
		}
		div.uwt-page-header > div.uwt-toolbar > a:hover
		{
			background-color: #ccc;
		}
		div.uwt-page-header > div.uwt-toolbar > a:active
		{
			background-color: #aaa;
		}
		div.uwt-toolbar > span.uwt-separator
		{
			border-left: 1px solid #ccc;
			border-right: 1px solid #fff;
		}
		div.uwt-window
		{
			max-width: 800px;
		}
		div.uwt-window > div.uwt-content > p:empty
		{
			display: none;
		}
		p
		{
			margin: 16px 0px;
		}

		ul.uwt-menu > li > a > span.uwt-icon.uwt-color-tile
		{
			background-color: #fff;
			border: 1px solid var(--uwt-color-dark);
			padding: 8px;
			margin: 8px;
			display: inline-block;
			position: absolute;
			left: 0px;
			top: 0px;
		}
		ul.uwt-menu > li > a > span.uwt-title, ul.uwt-menu > li > a > span.uwt-description
		{
			padding-left: 16px;
		}
		</style>
		<script type="text/javascript" src="scripts/phast/System.js.php"></script>
		<script type="text/javascript" src="dialog.js"></script>
		<script type="text/javascript" src="app.js"></script>
	</head>
	<body class="uwt-header-visible">
		<form method="POST">
			<div class="uwt-page-header">
				<h1>Tenant Management</h1>
				<p>Welcome, <strong>superuser</strong></p>
				<div class="uwt-toolbar">
					<a id="tsbProvision" href="#">Provision New Tenant</a>
					<span class="uwt-separator">&nbsp;</span>
					<a id="tsbPackageImport" href="#">Import Package</a>
					<a id="tsbPackageExport" href="#">Export Package</a>
				</div>
			</div>
			<div class="uwt-page-content">
				<?php
				if ($pdoex === null)
				{
				?>
				
				<h2>Customer Tenants</h2>
				<table class="uwt-listview">
					<thead>
						<tr>
							<th>Tenant Name</th>
							<th>Status</th>
							<th>Tenant Type</th>
							<th>Data Center</th>
							<th>Charge / No Charge</th>
							<th>Initial Term</th>
							<th>Renewal Term</th>
							<th>Tenant Start Date</th>
							<th>Tenant End Date</th>
							<th>Purpose</th>
						</tr>
					</thead>
					<tbody>
						<?php
							$query = "SELECT * FROM mocha_tenants";
							$stmt = $pdo->prepare($query);
							$result = $stmt->execute();
							$rows = $stmt->fetchAll();
							foreach ($rows as $values)
							{
								if ($values["is_library"] != 0)
								{
									continue;
								}
						?>
						<tr>
							<td><a class="uwt-external-link" target="_blank" href="<?php echo("https://" . $basepath . "/" . $values["tenant_name"]); ?>"><?php echo ($values["tenant_name"]); ?></a></td>
							<td>Active</td>
							<td>MADI Internal Development</td>
							<td>Local</td>
							<td>No Charge</td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
							<td></td>
						</tr>
						<?php
							}
						?>
					</tbody>
				</table>
				<?php
				}
				else
				{
					?>
					<div class="uwt-window uwt-visible" style="margin-left: auto; margin-right: auto; margin-top: 240px; width: 800px; display: block; position: static;">
						<div class="uwt-header">
							<span class="uwt-title">Database Intialization Error</span>
						</div>
						<div class="uwt-content">
							<p><?php print_r($pdoex->getCode() . " : " . $pdoex->getMessage()); ?></p>
							<table class="uwt-listview">
								<thead>
									<tr>
										<th>#</th>
										<th>File Name</th>
										<th>Line Number</th>
										<th>Function</th>
									</tr>
								</thead>
								<tbody>
								<?php
								$trace = $pdoex->getTrace();
								for ($i = 0; $i < count($trace); $i++)
								{
									?>
									<tr>
										<td><?php echo($i); ?></td>
										<td><?php echo($trace[$i]["file"]); ?></td>
										<td><?php echo($trace[$i]["line"]); ?></td>
										<td><?php echo($trace[$i]["class"] . "::" . $trace[$i]["function"]); ?></td>
									</tr>
									<?php
								}
								?>
								</tbody>
							</table>
						</div>
					</div>
					<?php
				}
				?>
			</div>
		</form>
	</body>
</html>

