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

<form method="POST" id="numberplanedit" action="?m=numberplanedit&id={$numberplanedit.id}" name="numberplanedit">
</form>

<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 98%;">
	</colgroup>
	<thead>
		<tr>
			<td rowspan="2">
				{icon name="id"}
			</td>
			<td rowspan="2">
				<strong>{trans("Template")}</strong>
			</td>
			<td>
				<input type="text" name="numberplanedit[template]"
					value="{$numberplanedit.template|escape}"
					form="numberplanedit"
					{tip trigger="template"}>
				{capture assign="template_hint"}{strip}
					Enter number template<br>
					<br>
					Supported substitution symbols:<br>
					<strong>%N</strong> - document number,<br>
					<strong>%m</strong> - month number (always two digits),<br>
					<strong>%Y</strong> - year number (always four digits),<br>
					<strong>%C</strong> - customer id; if this symbol is present then customer dedicated numbering applies.<br>
					<br>
					Other symbols can be used which are described in PHP <a href="https://php.net/strftime"><strong>strftime</strong></a> function documentation.
				{/strip}{/capture}
				{hint content=$template_hint}
			</td>
		</tr>
		<tr style="border-top: none;">
			<td>
				<label>{strip}
					<input type="checkbox" name="numberplanedit[refflag]"
						value="1"
						form="numberplanedit"
						{tip trigger="refflag"}
						{if !empty($numberplanedit.refflag)} checked{/if}>
					{trans("relative to reference document")}
				{/strip}</label>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="document"}
			</td>
			<td class="nobr">
				<strong>{trans("Document type")}</strong>
			</td>
			<td>
				<select name="numberplanedit[doctype]" size="1"
					form="numberplanedit"
					{tip text="Select document type" trigger="doctype"}>
					{foreach Localisation::arraySort($_DOCTYPES) as $doctype => $item}
						<option value="{$doctype}"{if $doctype == $numberplanedit.doctype} selected{/if}>{$item|escape}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="calendar"}
			</td>
			<td>
				<strong>{trans("Period")}</strong>
			</td>
			<td>
				<select name="numberplanedit[period]" size="1"
					form="numberplanedit"
					{tip text="Select numbering period" trigger="period"}>
					{foreach $_NUM_PERIODS as $key => $item}
						<option value="{$key}"{if $key == $numberplanedit.period} selected{/if}>{$item}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="division"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("Division")}</strong>
			</td>
			<td>
				<select name="numberplanedit[divisions][]" id="divisions" multiple
					form="numberplanedit"
					{tip text="Select division" class="lms-ui-multiselect" trigger="divisions"}>
					{foreach $divisions as $division}
						<option value="{$division.id}"{if isset($numberplanedit.divisions[$division.id])} selected{/if}
							>{$division.label|escape}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="user"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("User")}</strong>
			</td>
			<td>
				<select name="numberplanedit[users][]" id="users" multiple
					form="numberplanedit"
					{tip text="Select users" class="lms-ui-multiselect" trigger="users"}>
					{foreach $users as $user}
						<option value="{$user.id}"{if isset($numberplanedit.users[$user.id])} selected{/if}
							>{$user.rname|escape} ({$user.login})</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="legal-personality"}
			</td>
			<td class="nobr valign-top">
				<strong>{trans("Customer type")}</strong>
			</td>
			<td>
				<select name="numberplanedit[customertype]" id="customertype"
						form="numberplanedit"
						{tip text="Select customer type" trigger="customertype"}>
					<option value="">{trans("<!customer-type>— any —")}</option>
					<option value="{$smarty.const.CTYPES_PRIVATE}"
						{if isset($numberplanedit.customertype) && $numberplanedit.customertype == $smarty.const.CTYPES_PRIVATE} selected{/if}
						>{$_CTYPES[$smarty.const.CTYPES_PRIVATE]}</option>
					<option value="{$smarty.const.CTYPES_COMPANY}"
						{if isset($numberplanedit.customertype) && $numberplanedit.customertype == $smarty.const.CTYPES_COMPANY} selected{/if}
						>{$_CTYPES[$smarty.const.CTYPES_COMPANY]}</option>
					<label>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="calendar"}
			</td>
			<td>
				<strong>{trans("Validity period")}</strong>
			</td>
			<td>
				{trans("from")}
				<input type="text" name="numberplanedit[datefrom]"
					value="{if $numberplanedit.datefrom}{$numberplanedit.datefrom}{/if}"
					form="numberplanedit"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date lms-ui-date-unix" trigger="datefrom"}
					size="10">
				{trans("to")}
				<input type="text" name="numberplanedit[dateto]"
					value="{if $numberplanedit.dateto}{$numberplanedit.dateto}{/if}"
					form="numberplanedit"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date lms-ui-date-unix" trigger="dateto"}
					size="10">
			</td>
		</tr>
		<tr>
			<td>
				{icon name="option"}
			</td>
			<td class="nobr">
				<label for="numberplanedit_isdefault">
					<strong>{trans("Default")}</strong>
				</label>
			</td>
			<td>
				<input type="checkbox" name="numberplanedit[isdefault]" id="numberplanedit_isdefault"
					form="numberplanedit"
					value="1" {tip text="Check box to make this plan a default for selected document type"}
					{if $numberplanedit.isdefault} checked{/if}>
			</td>
		</tr>
		<tr>
			<td class="lms-ui-box-buttons" colspan="3">
				{button type="submit" icon="save" label="Submit" form="numberplanedit"}
				{button icon="cancel" label="Cancel" href="?m=numberplanlist"}
			</td>
		</tr>
	</tbody>
</table>
<script>

	$(function() {
		$('[name="numberplanedit[template]"]').focus();

		$('#divisions').change(function() {
			$.ajax('?m={$layout.module}&op=updateusers&api=1', {
				method: "POST",
				dataType: "json",
				data: {
					divisions: $(this).val()
				}
			}).done(function(data) {
				var options = '';
				var users = $('#users').val();
				$.each(data, function(userid, user) {
					options += '<option value="' + userid + '"' +
						(users.indexOf(userid) == -1 ? '' : ' selected') +
						'>' + user.rname + ' (' + user.login + ')</option>';
				});
				$('#users').html(options).trigger('lms:multiselect:updated');
			});
		});
	});

</script>
{/block}
