<?php
/**
 * @package		solo
 * @copyright	2014 Nicholas K. Dionysopoulos / Akeeba Ltd
 * @license		GNU GPL version 3 or later
 */

use Awf\Text\Text;

/** @var \Solo\View\Users\Html $this */

$router = $this->container->router;
$token = $this->container->session->getCsrfToken()->getValue();

/** @var \Solo\Model\Users $model */
$model = $this->getModel();

$permissions = array(
	'backup'	=> false,
	'configure'	=> false,
	'download'	=> false,
);

$tfa = array(
	'method'	=> 'none',
	'yubikey'	=> '',
	'google'	=> '',
);

if ($model->id)
{
	$user = $this->container->userManager->getUser($model->id);
	$permissions = array(
		'backup'	=> $user->getPrivilege('akeeba.backup', false),
		'configure'	=> $user->getPrivilege('akeeba.configure', false),
		'download'	=> $user->getPrivilege('akeeba.download', false),
	);
	$tfa = array(
		'method'	=> $user->getParameters()->get('tfa.method', 'none'),
		'yubikey'	=> $user->getParameters()->get('tfa.yubikey', ''),
		'google'	=> $user->getParameters()->get('tfa.google', ''),
	);

	if (empty($tfa['google']))
	{
		$totp = new \Awf\Encrypt\Totp(30, 6, 10);
		$tfa['google'] = $totp->generateSecret();
	}
}

?>
<form name="adminForm" id="adminForm" action="<?php echo $router->route('index.php?view=users') ?>" method="POST" class="form-horizontal" role="form">
	<input type="hidden" name="task" value="" />
	<input type="hidden" name="id" value="<?php echo $model->id ?>" />
	<input type="hidden" name="<?php echo $token ?>" value="1">

	<fieldset>
		<legend><?php echo Text::_('SOLO_USERS_HEAD_BASIC'); ?></legend>

		<div class="form-group">
			<label class="control-label col-sm-3" for="username">
				<?php echo Text::_('SOLO_USERS_FIELD_USERNAME'); ?> *
			</label>
			<div class="col-sm-9">
				<input type="text" name="username" maxlength="255" size="50"
					   value="<?php echo $this->escape($model->username) ?>"
					   class="form-control" required />
			</div>
		</div>

		<div class="form-group">
			<label class="control-label col-sm-3" for="password">
				<?php echo Text::_('SOLO_USERS_FIELD_PASSWORD'); ?>
			</label>
			<div class="col-sm-9">
				<input type="password" name="password" maxlength="255" size="50"
					   value=""
					   class="form-control" />
			</div>
		</div>

		<div class="form-group">
			<label class="control-label col-sm-3" for="repeatpassword">
				<?php echo Text::_('SOLO_USERS_FIELD_PASSWORDREPEAT'); ?>
			</label>
			<div class="col-sm-9">
				<input type="password" name="repeatpassword" maxlength="255" size="50"
					   value=""
					   class="form-control" />
			</div>
		</div>

		<div class="form-group">
			<label class="control-label col-sm-3" for="email">
				<?php echo Text::_('SOLO_USERS_FIELD_EMAIL'); ?> *
			</label>
			<div class="col-sm-9">
				<input type="email" name="email" maxlength="255" size="50"
					   value="<?php echo $this->escape($model->email) ?>"
					   class="form-control" required />
			</div>
		</div>

		<div class="form-group">
			<label class="control-label col-sm-3" for="name">
				<?php echo Text::_('SOLO_USERS_FIELD_NAME'); ?>
			</label>
			<div class="col-sm-9">
				<input type="text" name="name" maxlength="255" size="50"
					   value="<?php echo $this->escape($model->name) ?>"
					   class="form-control" />
			</div>
		</div>

		<div class="form-group">
			<label class="control-label col-sm-3" for="name">
				<?php echo Text::_('SOLO_USERS_FIELDSET_PERMISSIONS'); ?>
			</label>
			<div class="col-sm-9">
				<div class="checkbox">
					<label>
						<input type="checkbox" name="permissions[backup]" <?php echo $permissions['backup'] ? 'checked' : ''?>>
						<?php echo Text::_('SOLO_USERS_FIELD_PERMISSIONS_BACKUP') ?>
					</label>
				</div>

				<div class="checkbox">
					<label>
						<input type="checkbox" name="permissions[configure]"  <?php echo $permissions['configure'] ? 'checked' : ''?>>
						<?php echo Text::_('SOLO_USERS_FIELD_PERMISSIONS_CONFIGURE') ?>
					</label>
				</div>

				<div class="checkbox">
					<label>
						<input type="checkbox" name="permissions[download]"  <?php echo $permissions['download'] ? 'checked' : ''?>>
						<?php echo Text::_('SOLO_USERS_FIELD_PERMISSIONS_DOWNLOAD') ?>
					</label>
				</div>
			</div>
		</div>
	</fieldset>

	<fieldset>
		<legend><?php echo Text::_('SOLO_USERS_HEAD_TFA'); ?></legend>

		<p>
			<?php echo Text::_('SOLO_USERS_LBL_ABOUTTFA'); ?>
		</p>
	</fieldset>

	<div class="form-group">
		<label class="control-label col-sm-3" for="tfa[method]">
			<?php echo Text::_('SOLO_USERS_LBL_TFAMETHOD'); ?>
		</label>
		<div class="col-sm-9">
			<?php echo \Solo\Helper\Setup::tfaMethods('tfa[method]', $tfa['method']) ?>
		</div>
	</div>

	<div id="tfa_containers" class="col-sm-9 col-sm-push-3">
		<div id="tfa_none" style="display: none">

		</div>

		<div id="tfa_yubikey" style="display: none">
			<p class="well well-sm">
				<?php echo Text::_('SOLO_USERS_TFA_YUBIKEY_INTRO'); ?>
			</p>
			<p>
				<?php echo Text::_('SOLO_USERS_TFA_YUBIKEY_SETUP'); ?>
			</p>

			<div class="form-group">
				<label class="control-label col-sm-2" for="tfa[yubikey]">
					<?php echo Text::_('SOLO_USERS_LBL_TFASECURITYCODE'); ?>
				</label>
				<div class="col-sm-9">
					<input type="text" name="tfa[yubikey]" class="form-control" value="<?php echo $tfa['yubikey'] ?>">
				</div>
			</div>
		</div>

		<div id="tfa_google" style="display: none">
			<p class="well well-sm">
				<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_INTRO'); ?>
			</p>

			<input type="hidden" name="tfa[google]" value="<?php echo $tfa['google']?>">

			<h4><?php echo Text::_('SOLO_USERS_TFA_GOOGLE_LBL_STEP1') ?></h4>
			<p>
				<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP1'); ?>
			</p>
			<ul>
				<li>
					<a href="http://support.google.com/accounts/bin/answer.py?hl=en&answer=1066447">
						Official Google Authenticator app for Android, iOS and BlackBerry
					</a>
				</li>
				<li>
					<a href="http://en.wikipedia.org/wiki/Google_Authenticator#Implementation">
						Compatible clients for other devices and OS (listed in Wikipedia)
					</a>
				</li>
			</ul>

			<div class="alert alert-warning">
				<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP1_WARNING'); ?>
			</div>

			<h4><?php echo Text::_('SOLO_USERS_TFA_GOOGLE_LBL_STEP2') ?></h4>
			<div class="col-sm-6 col-xs-12">
				<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP2A') ?>
				<table class="table table-striped">
					<tr>
						<td>
							<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP2A_ACCOUNT') ?>
						</td>
						<td>
							Akeeba Solo
						</td>
					</tr>
					<tr>
						<td>
							<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP2A_KEY') ?>
						</td>
						<td>
							<code><?php echo $tfa['google']?></code>
						</td>
					</tr>
				</table>
			</div>
			<div class="col-sm-6 col-xs-12">
				<p>
					<?php echo Text::_('SOLO_USERS_TFA_GOOGLE_SETUP2B'); ?>
				</p>
				<img src="https://chart.googleapis.com/chart?chs=200x200&chld=Q|2&cht=qr&chl=<?php echo urlencode('otpauth://totp/Akeeba%20Solo?secret=' . $tfa['google']) ?>">
			</div>

			<h4><?php echo Text::_('SOLO_USERS_TFA_GOOGLE_LBL_STEP3') ?></h4>
			<div class="form-group">
				<label class="control-label col-sm-2" for="tfa[secret]">
					<?php echo Text::_('SOLO_USERS_LBL_TFASECURITYCODE'); ?>
				</label>
				<div class="col-sm-9">
					<input type="text" name="tfa[secret]" class="form-control" value="">
				</div>
			</div>
		</div>
	</div>

</form>

<script type="text/javascript">
Solo.loadScripts[Solo.loadScripts.length] = function () {
	(function($){
		$(document.getElementById('tfa[method]')).change(onTFAMethodChange);

		onTFAMethodChange();

		function onTFAMethodChange()
		{
			$('#tfa_containers>div').each(function(index, container){
				$(container).hide();
			})

			var containerName = 'tfa_' + $(document.getElementById('tfa[method]')).val();
			$('#' + containerName).show();
		}
	}(akeeba.jQuery));
};
</script>