{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
<style>

	#accesslist {
		column-width: 30vh;
	}

	#accesslist label {
		display: block;
		width: fit-content;
		white-space: normal;
		padding-left: 2.3em;
		text-indent: -2.3em;
		break-inside: avoid;
	}

	#copy-rights {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}

	#copy-rights *:not(:first-child) {
		padding-left: 0.5em;
	}

	#copy-rights label {
		white-space: normal;
	}

	textarea.lms-ui-autogrow {
		min-width: 30%;
	}

</style>

<H1>{$layout.pagetitle}</H1>
<form name="useradd" method="post" action="?m=useradd">
<input type="submit" class="hiddenbtn">
<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 99%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{icon name="login"}
				<strong>{trans("Login:")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[login]" value="{$useradd.login}" required
					{tip text="Enter user login" trigger="login" bold=1}>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td class="nobr">
				{icon name="user"}
				<strong>{trans("First name:")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[firstname]" value="{$useradd.firstname|escape}" size="40" required
					{tip text="Enter user first name" trigger="firstname"}>
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="user"}
				<strong>{trans("Last name:")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[lastname]" value="{$useradd.lastname|escape}" size="40" required
					{tip text="Enter user last name" trigger="lastname"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="document"}
				<strong>{trans("Issuer")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[issuer]" value="{$useradd.issuer|escape}" size="40"
					{tip text="Enter issuer name visible on documents" trigger="issuer"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="info"}
				<strong>{trans("Position:")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[position]" value="{$useradd.position}" size="40"
					{tip text="Enter user position (optional)" trigger="position"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="mail"}
				<strong>{trans("E-mail:")}</strong>
			</>
			<td>
				<input type="email" name="useradd[email]" value="{$useradd.email}" size="40"
					{tip text="Enter e-mail address (optional)" trigger="email"} autocomplete="new-password">
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="sms"}
				<strong>{trans("Phone:")}</strong>
			</td>
			<td>
				<input type="tel" name="useradd[phone]" value="{$useradd.phone}" size="40"
					{tip text="Enter phone number for SMS notification (optional)" trigger="phone"}>
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="options"}
				<strong>{trans("Notification:")}</strong>
			</td>
			<td>
				<label>
					<input type="checkbox" name="useradd[ntype][]" value="{$smarty.const.MSG_MAIL}"
						{if ($useradd.ntype & $smarty.const.MSG_MAIL)} checked{/if}>
					{trans("e-mail")}
				</label>
				<label>
					<input type="checkbox" name="useradd[ntype][]" value="{$smarty.const.MSG_SMS}"
						{if ($useradd.ntype & $smarty.const.MSG_SMS)} checked{/if}>
					{trans("sms")}
				</label>
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="disconnected"}
				<strong>{trans("Account active:")}</strong>
			</td>
			<td>
				<label>
					<input type="radio" name="useradd[access]" value="1"{if !isset($useradd.access) || $useradd.access=='1'} checked{/if}>
					{trans("yes")}
				</label>
				<label>
					<input type="radio" name="useradd[access]" value="0"{if $useradd.access=='0'} checked{/if}>
					{trans("no")}
				</label>
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="calendar"}
				<strong>{trans("Access period")}</strong>
			</td>
			<td class="nobr">
				{trans('from')}:
				<input type="text" name="useradd[accessfrom]" value="{if $useradd.accessfrom}{$useradd.accessfrom}{/if}"
					size="10" placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date" text="Enter access start date in YYYY/MM/DD format. If you don't want to define 'From' date leave this field empty" trigger="accessfrom"} autocomplete="new-password">
				{trans('to')}:
				<input type="text" name="useradd[accessto]" value="{if $useradd.accessto}{$useradd.accessto}{/if}"
					size="10" placeholder="{trans("yyyy/mm/dd")}" autocomplete="new-password"
					{tip class="lms-ui-date" text="Enter access end date in YYYY/MM/DD format. If you don't want to define 'To' date leave this field empty" trigger="accessto"}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="password"}
				<strong>{trans("Password:")}</strong>
			</td>
			<td>
				<input type="password" name="useradd[password]" required {tip trigger="password"} autocomplete="new-password">
				{button type="link" icon="password" tip="Generate new password and copy it to clipboard" id="genpasswd" class="lms-ui-button-clipboard" data_clipboard_handler="1"}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="password"}
				<strong>{trans("Repeat password:")}</strong>
			</td>
			<td>
				<input type="password" name="useradd[confirm]" required {tip trigger="password"} autocomplete="new-password">
			</td>
		</tr>
		<tr>
			<td>
				{icon name="password"}
				<strong>{trans("Network password")}</strong>
			</td>
			<td>
				<input type="password" name="useradd[netpassword]" {tip trigger="netpassword"} autocomplete="new-password">
				{hint mode="toggle" content="Network password can be used to authenticate users via Radius server."}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="password"}
				<strong>{trans("Repeat network password")}</strong>
			</td>
			<td>
				<input type="password" name="useradd[confirmnetpassword]" {tip trigger="netpassword"} autocomplete="new-password">
				{hint mode="toggle" content="Network password can be used to authenticate users via Radius server."}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				<label for="passwdforcechange">
					{icon name="password"}
					<strong>{trans("Force password change:")}</strong>
				</label>
			</td>
			<td>
				<input type="checkbox" name="useradd[passwdforcechange]" value="1"
					id="passwdforcechange"{if isset($useradd.passwdforcechange)} checked{/if}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="password"}
				<strong>{trans("Two factor authentication:")}</strong>
			</td>
			<td>
				<label>
					<input type="radio" name="useradd[twofactorauth]" value="1"{if $useradd.twofactorauth == 1} checked{/if}>
					{trans("yes")}
				</label>
				<label>
					<input type="radio" name="useradd[twofactorauth]" value="0"{if !$useradd.twofactorauth} checked{/if}>
					{trans("no")}
				</label>
				<label>
					<input type="radio" name="useradd[twofactorauth]" value="-1"{if $useradd.twofactorauth == -1} checked{/if}>
					{trans("generate secret key")}
				</label>
			</td>
		</tr>
		<tr id="twofactorauthsecretkey"{if $useradd.twofactorauth != 1} style="display: none;"{/if}>
			<td>
				{icon name="password"}
				<strong>{trans("Secret key:")}</strong>
			</td>
			<td>
				<input type="text" name="useradd[twofactorauthsecretkey]" value="{$useradd.twofactorauthsecretkey}"
					maxlength="16" {tip trigger="twofactorauthsecretkey"}>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="ipnetwork"}
				<strong>{trans("Allowed hosts")}</strong>
			</td>
			<td>
				<textarea rows="2" name="useradd[hosts]" class="lms-ui-autogrow" {tip trigger="hosts"}>{strip}
					{$useradd.hosts|escape}
					{/strip}</textarea>
				{hint content="Enter comma-separated list of hosts or networks (optional). User can log in only from IP addresses which belong to this list."}
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="ipnetwork"}
				<strong>{trans("Trusted hosts")}</strong>
			</td>
			<td>
				<textarea rows="2" name="useradd[trustedhosts]" class="lms-ui-autogrow" {tip trigger="trustedhosts"}>{strip}
					{$useradd.trustedhosts|escape}
				{/strip}</textarea>
				{hint content="Enter comma-separated list of hosts or networks (optional). User can log in with bypass two factor authentication from IP addresses which belong to this list."}
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="password"}
				<strong>{trans("Password expiration:")}</strong>
			</td>
			<td>
				<select name="useradd[passwdexpiration]" {tip text="Select password expiration period" trigger="passwdexpiration"}>
					{foreach $_PASSWDEXPIRATIONS as $key => $item}
						<option value="{$key}"{if $key == $useradd.passwdexpiration} selected{/if}>{$item}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td class="nobr">
				{icon name="permissions"}
				<strong>{trans("Access rights:")}</strong>
			</td>
			<td class="nobr">
				<table style="width: 100%;">
					<colgroup>
						<col style="width: 50%;">
						<col style="width: 50%;">
					</colgroup>
					<tbody>
						<tr>
							<td colspan="2">
								<div id="copy-rights">
									{button icon="clone" id="copy-rights" label="Copy rights"}
									<label>
										{trans("of selected user:")}
										<select id="from-user" name="useradd[src_userid]">
											<option value="">{trans("<!access-rights>— empty —")}</option>
											{foreach $users as $user}
												<option value="{$user.id}"
														{if isset($useradd.src_userid) && $useradd.src_userid == $user.id} selected{/if}>{$user.rname|escape} ({$user.login})</option>
											{/foreach}
										</select>
									</label>
								</div>
							</td>
						</tr>
						<tr>
							<td colspan="2">
								{include file="user/usercopypermissions.html" form_variable_prefix="useradd" variables=$useradd}
							</td>
						</tr>
						<tr id="user-rights">
							<td class="valign-top" colspan="2">
								<div id="accesslist">
									{foreach $accesslist as $name => $access}
										{if !empty($access.name)}
											<label>
												<input type="checkbox" data-right-name="{$name}" id="acl_{$access@index}"
													   name="acl[{$name}]" value="1"{if $access.enabled} checked{/if}>
												{$access.name}
											</label>
										{/if}
									{/foreach}
									{$name = ""}
								</div>
							</td>
						</tr>
					</tbody>
				</table>
			</>
		</tr>

		<tr>
			<td class="nobr">
				{icon name="usergroup"}
				<strong>{trans("User groups:")}</strong>
			</td>
			<td>
				<select size="4" name="useradd[usergroups][]" id="usergroups" multiple
					{tip text="Select user groups" class="lms-ui-multiselect" trigger="usergroups"}
					data-default-value="{trans("— none —")}" data-shorten-to-default-value="false">
					{foreach $usergroups as $group}
						<option value="{$group.id}"{if isset($selectedusergroups[$group.id])} selected{/if}>{$group.name}</option>
					{/foreach}
				</select>
			</td>
		</tr>

		<tr>
			<td class="nobr">
				{icon name="customergroup"}
				<strong>{trans("Customer groups:")}</strong>
			</td>
			<td>
				<select size="4" name="useradd[customergroups][]" id="customergroups" multiple
					{tip text="Select customer groups" class="lms-ui-multiselect" trigger="customergroups"}
					data-default-value="{trans("— none —")}" data-shorten-to-default-value="false">
					{foreach $customergroups as $group}
						<option value="{$group.id}"{if isset($selectedgroups[$group.id])} selected{/if}>{$group.name}</option>
					{/foreach}
				</select>
			</td>
		</tr>

		{if is_array($divisions) && count($divisions)}
			<tr>
				<td class="nobr">
					{icon name="division"}
					<strong>{trans("Divisions:")}</strong>
				</td>
				<td>
					<select size="4" name="useradd[divisions][]" id="divisions" multiple
						{tip text="Select divisions" class="lms-ui-multiselect" trigger="division"}
						data-default-value="{trans("— none —")}" data-shorten-to-default-value="false" required>
						{foreach $divisions as $division}
							<option value="{$division.id}" {if isset($useradd.divisions) && in_array($division.id, $useradd.divisions)} selected{/if}>
								{$division.label|trunescape:30}
							</option>
						{/foreach}
					</select>
				</td>
			</tr>
		{/if}

		<tr>
			<td colspan="2" class="lms-ui-box-buttons">
				{button type="submit" icon="save" label="Submit"}
				{button icon="cancel" icon="cancel" label="Cancel" onclick="location.href = '?m=userlist';"}
			</td>
		</tr>
    </tbody>
</table>
</form>
<script>

	$(function() {
		$('form[name="useradd"] [name="useradd[login]"]').focus();

		$('[name="useradd[twofactorauth]"]').click(function() {
			var twofactorauth = parseInt($(this).val());
			$('#twofactorauthsecretkey').toggle(twofactorauth == 1);
		});

		$('#copy-rights').click(function() {
			$('[data-right-name]').prop('checked', false);
			var from_user;
			if (from_user = parseInt($('#from-user').val())) {
				$.ajax({
					data: {
						fromuser: from_user
					},
					dataType: "json",
					success: function(data) {
						var user_rights = $('#user-rights');
						$.each(data['rights'], function(key, value) {
							$('[data-right-name="'  + value + '"]', user_rights).prop('checked', true);
						});

						var usergroups = $('#usergroups');
						usergroups.find('option').prop('selected', false);
						$.each(data['usergroups'], function(key, value) {
							usergroups.find('option[value="' + value + '"]').prop('selected', true);
						});
						usergroups.trigger('lms:multiselect:updated');

						var customergroups = $('#customergroups');
						customergroups.find('option').prop('selected', false);
						$.each(data['customergroups'], function(key, value) {
							customergroups.find('option[value="' + value + '"]').prop('selected', true);
						});
						customergroups.trigger('lms:multiselect:updated');

						var divisions = $('#divisions');
						divisions.find('option').prop('selected', false);
						$.each(data['divisions'], function(key, value) {
							divisions.find('option[value="' + value + '"]').prop('selected', true);
						});
						divisions.trigger('lms:multiselect:updated');
					}
				});
			}
		});

		$("#genpasswd").on("lms:clipboard:click", function(e) {
			var newPassword = generate_random_string({ConfigHelper::getConfig('users.default_password_length', 14)}, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
			$('[name="useradd[password]"],[name="useradd[confirm]"]').val(newPassword);
			e.result = newPassword;
		});
	});

</script>
{/block}
