<?php
	require_once("../lib/phast/System.inc.php");
	
	use Phast\System;
	
	System::SetApplicationPath(dirname(__FILE__) . "/../");
	System::Initialize();
	
	if (isset($_POST["Database_ServerName"]))
	{
		$Database_ServerName = $_POST["Database_ServerName"];
	}
	else
	{
		$Database_ServerName = System::GetConfigurationValue("Database.ServerName");
	}
	if (isset($_POST["Database_DatabaseName"]))
	{
		$Database_DatabaseName = $_POST["Database_DatabaseName"];
	}
	else
	{
		$Database_DatabaseName = System::GetConfigurationValue("Database.DatabaseName");
	}
	if (isset($_POST["Database_PortNumber"]))
	{
		$Database_PortNumber = $_POST["Database_PortNumber"];
	}
	else
	{
		$Database_PortNumber = System::GetConfigurationValue("Database.PortNumber");
	}
	if (isset($_POST["Database_UserName"]))
	{
		$Database_UserName = $_POST["Database_UserName"];
	}
	else
	{
		$Database_UserName = System::GetConfigurationValue("Database.UserName");
	}
	if (isset($_POST["Database_Password"]))
	{
		$Database_Password = $_POST["Database_Password"];
	}
	else
	{
		$Database_Password = System::GetConfigurationValue("Database.Password");
	}
	
	$Wizard_PageNumber = 1;
	$Wizard_Exception = null;
	if ($_SERVER["REQUEST_METHOD"] == "POST")
	{
		$Wizard_PageNumber = $_POST["Wizard_PageNumber"];
		if ($Wizard_PageNumber == 1)
		{
			// attempt to connect to the database
			
			try
			{
				$pdo = new \PDO("mysql:host=" . $Database_ServerName . ";port=" . $Database_PortNumber . ";dbname=" . $Database_DatabaseName, $Database_UserName, $Database_Password);
				$Wizard_PageNumber = 2;
			}
			catch (Exception $ex)
			{
				$Wizard_PageNumber = 1;
				$Wizard_Exception = $ex;
			}
		}
		else if ($Wizard_PageNumber == 2)
		{
			$Wizard_PageNumber = 3;
		}
		else if ($Wizard_PageNumber == 3)
		{
			$config_final = $_POST["config_final"];
			$file = fopen(System::GetApplicationPath() . "/include/Configuration.inc.php", "w");
			fwrite($file, $config_final);
			fclose($file);
			
			System::Redirect("~/");
			exit();
		}
	}
	
?>
<!DOCTYPE html>
<html>
	<head>
		<title>Mocha Setup</title>
		<link rel="stylesheet" type="text/css" href="<?php echo(System::ExpandRelativePath("~/themes/avondale/theme.css")); ?>" />
		<script type="text/javascript" src="<?php echo(System::ExpandRelativePath("~/scripts/phast/System.js.php")); ?>"></script>
		<script type="text/javascript" src="<?php echo(System::ExpandRelativePath("~/scripts/mocha.js.php")); ?>"></script>
	</head>
	<body class="uwt-header-visible" style="width: 60%; margin-left: auto; margin-right: auto;">
		<div class="uwt-page-header">
			<h1>Mocha Single-User Version (SUV) Setup</h1>
		</div>
		<div class="uwt-page-content">
			<form method="POST">
				<input type="hidden" name="Wizard_PageNumber" value="<?php echo($Wizard_PageNumber); ?>" />
				<div class="uwt-wizard">
					<ol>
						<?php echo("<li" . ($Wizard_PageNumber == 1 ? " class=\"uwt-selected\"" : "") . ">"); ?>
							<span class="uwt-wizard-page-number">1</span>
							<span class="uwt-title">Database Information</span>
							<span class="uwt-description">Use the information provided in your service contract</span>
						</li>
						<?php echo("<li" . ($Wizard_PageNumber == 2 ? " class=\"uwt-selected\"" : "") . ">"); ?>
							<span class="uwt-wizard-page-number">2</span>
							<span class="uwt-title">Tenant Information</span>
							<span class="uwt-description">Select the tenant(s) you would like to make available</span>
						</li>
						<?php echo("<li" . ($Wizard_PageNumber == 3 ? " class=\"uwt-selected\"" : "") . ">"); ?>
							<span class="uwt-wizard-page-number">3</span>
							<span class="uwt-title">Validate Configuration</span>
							<span class="uwt-description">Ensure that everything looks correct</span>
						</li>
					</ol>
					<div class="uwt-wizard-pages">
						<?php
						echo("<div class=\"uwt-wizard-page\"");
						if ($Wizard_PageNumber == 1)
						{
							echo(" style=\"display: block;\"");
						}
						else
						{
							echo(" style=\"display: none;\"");
						}
						echo(">");
						?>
							<?php
							if ($Wizard_Exception != null)
							{
								echo ("<div class=\"\" style=\"margin: 16px; color: #ff0000; padding: 16px; border: 1px solid #ff0000; background-color: #ffcccc;\">The information you provided didn't work.  Please check the details and try again.</div>");
							}
							?>
							<table class="uwt-formview">
								<tr>
									<td colspan="2"><h2>Server Information</h2></td>
								</tr>
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>Server name</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="text" name="Database_ServerName" value="<?php echo($Database_ServerName); ?>" />
									</td>
								</tr>
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>Port number</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="text" name="Database_PortNumber" value="<?php echo($Database_PortNumber); ?>" />
									</td>
								</tr>
								<tr>
									<td colspan="2"><h2>Database Information</h2></td>
								</tr>
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>Database name</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="text" name="Database_DatabaseName" value="<?php echo($Database_DatabaseName); ?>" />
									</td>
								</tr>
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>User name</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="text" name="Database_UserName" value="<?php echo($Database_UserName); ?>" />
									</td>
								</tr>
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>Password</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="password" name="Database_Password" value="<?php echo($Database_Password); ?>" />
									</td>
								</tr>
							</table>
						</div>
						<?php
						echo("<div class=\"uwt-wizard-page\"");
						if ($Wizard_PageNumber == 2)
						{
							echo(" style=\"display: block;\"");
						}
						else
						{
							echo(" style=\"display: none;\"");
						}
						echo(">");
						?>
						
							<h2>Application Templates</h2>
							<p>Please select a template that best describes how you will use your Mocha application.</p>
							<ul class="uwt-listbox">
								<li>
									<div class="uwt-title">Production Phase</div>
									<div class="uwt-detail">Three tenants, one each for production, sandbox, and sandbox preview</div>
								</li>
								<li>
									<div class="uwt-title">Implementation Phase</div>
									<div class="uwt-detail">Five implementation tenants for configuring various applications</div>
								</li>
								<li>
									<div class="uwt-title">Internal Development Only</div>
									<div class="uwt-detail">A single 'super' tenant for internal development</div>
								</li>
							</ul>
							
							<h2>Additional Configuration</h2>
							<table class="uwt-formview">
								<tr class="uwt-formview-item">
									<td class="uwt-formview-item-title">
										<label>Tenant prefix</label>
									</td>
									<td class="uwt-formview-item-content">
										<input type="text" name="Application_DefaultTenant" value="<?php echo(System::GetConfigurationValue("Application.DefaultTenant")); ?>" />
									</td>
								</tr>
							</table>
							
						</div>
						<?php
						echo("<div class=\"uwt-wizard-page\"");
						if ($Wizard_PageNumber == 3)
						{
							echo(" style=\"display: block;\"");
						}
						else
						{
							echo(" style=\"display: none;\"");
						}
						echo(">");
						?>
						
							<h2>Pending Changes</h2>
							<p>Make any additional changes you need to the Phast configuration file, and then click Finish to save changes.</p>
							<textarea name="config_final" style="font-family: monospace; width: 100%; height: 400px; box-sizing: border-box;"><?php
							
								$text = "&lt;?php\n";
								$text .= "use Phast\IncludeFile;\n";
								$text .= "use Phast\System;\n";
								$text .= "\n";
								$text .= "System::\$IncludeFiles[] = new IncludeFile(\"/lib/mocha/system.inc.php\", true);\n";
								$text .= "\n";
								$text .= "System::\$EnableTenantedHosting = false;\n";
								$text .= "\n";
								$text .= "System::\$Configuration[\"Application.Title\"] = \"Mocha SUV\";\n";
								$text .= "System::\$Configuration[\"Application.DefaultTenant\"] = \"" . $_POST["Application_DefaultTenant"] . "\";\n";
								$text .= "System::\$Configuration[\"Application.ThemeName\"] = \"avondale\";\n";
								$text .= "\n";
								$text .= "System::\$Configuration[\"Database.ServerName\"] = \"" . $_POST["Database_ServerName"] . "\";\n";
								$text .= "System::\$Configuration[\"Database.PortNumber\"] = " . $_POST["Database_PortNumber"] . ";\n";
								$text .= "System::\$Configuration[\"Database.DatabaseName\"] = \"" . $_POST["Database_DatabaseName"] . "\";\n";
								$text .= "System::\$Configuration[\"Database.UserName\"] = \"" . $_POST["Database_UserName"] . "\";\n";
								$text .= "System::\$Configuration[\"Database.Password\"] = \"" . $_POST["Database_Password"] . "\";\n";
								$text .= "\n";
								$text .= "System::\$Configuration[\"LoginPageRedirectURL\"] = \"~/madi/authgwy/\$(CurrentTenantName)/login.htmld\";\n";
								$text .= "System::\$Configuration[\"Runtime.DisableAutomaticExtensionParsing\"] = true;\n";
								$text .= "\n";
								$text .= "System::\$Configuration[\"Paths.MasterPages\"] = [ \"/ui/masterPages\" ];\n";
								$text .= "System::\$Configuration[\"Paths.Pages\"] = [ \"/ui/pages\" ];\n";
								$text .= "\n";
								$text .= "require_once (\"BeforeLaunchEvent.inc.php\");\n";
								
								$text .= "\n?&gt;";
								echo($text);
							
							?></textarea>
						</div>
					</div>
					<div class="uwt-footer">
						<input type="submit" class="uwt-button uwt-color-primary" value="<?php echo(($Wizard_PageNumber == 3) ? "Finish" : "Next &gt;"); ?>" />
					</div>
				</div>
			</form>
		</div>
	</body>
</html>

