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

<style>

	table.lmsbox > thead >tr:not(:first-child) {
		border-top: none;
	}

	#document-period {
		display: flex;
	}

	#document-period > *:not(:first-child) {
		margin-left: 0.5em;
	}

	#document-period > div {
		display: flex;
	}

	#document-period > div:first-child {
		display: flex;
		flex-direction: column;
	}

	#document-period > div:first-child > :not(:first-child) {
		margin-top: 0.3em;
	}

	#document-period > div:not(:first-child) {
		display: flex;
		align-items: center;
		margin-left: 0.5em;
	}

</style>

<script>

	function DocTemplateChanged() {
		reset_login_timeout();
		xajax_DocTemplateChanged(
			$('[name="document[type]"]').val(),
			$('[name="document[templ]"]').val(),
			0,
			{}
		);
	}

	function DocTypeChanged() {
		reset_login_timeout();
		xajax_DocTypeChanged($('[name="document[type]"]').val(), 0);
	}

	function enable_templates() {
		$('#templ').prop('disabled', false);
	}

	function disable_templates() {
		$('#templ').val(0);
		$('#templ').prop('disabled', true);
	}

	function printdoc() {
		document.documentgen.action = "?m=documentgen&print=1";
	}

</script>
<form name="documentgen" method="POST" id="documentgen-form" action="?m=documentgen" enctype="multipart/form-data">
<input type="submit" class="hiddenbtn">
<table class="lmsbox">
	<colgroup>
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 98%;">
	</colgroup>
	<thead>
		<tr>
			<td>
				{icon name="type"}
			</td>
			<td>
				<strong>{trans("Type")}</strong>
			</td>
			<td>
				<select size="1" name="document[type]" id="doctype" onChange="DocTypeChanged();"
					{tip class="lms-ui-advanced-select-test" text="Select document type" trigger="type"}
					required>
					<option value="0">{trans("— select type —")}</option>
					{foreach $_DOCTYPES as $key => $item}
						{if $key < 0 && in_array($key, $docrights)}
							<option value="{$key}"{if $document.type == $key} selected{/if}>{$item}</option>
						{/if}
					{/foreach}
				</select>
			</td>
		</tr>
		<tr style="display:table-row;">
			<td>
				{icon name="template"}
			</td>
			<td>
				<strong>{trans("Template")}</strong>
			</td>
			<td>
				<select size="1" name="document[templ]" id="templ" {if $document.type >= 0} disabled{/if}
					onChange="DocTemplateChanged();"
					{tip text="Select document template" trigger="templ"}>
					<option value="0">... {trans("select template")} ...</option>
					{foreach $docengines as $docengine}
						<option value="{$docengine.name}"{if $document.templ == $docengine.name} selected{/if}>{$docengine.title}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="document"}
			</td>
			<td class="nobr">
				<strong>{trans("Title")}</strong>
			</td>
			<td class="nobr">
				<input type="text" id="title" name="document[title]" value="{$document.title}"
					{tip text="Enter document title" trigger="title"} size="40" required>
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="document"}
			</td>
			<td class="nobr">
				<strong>{trans("First number")}</strong>
			</td>
			<td>
				{numberplan_selection
					doctype_selector  = "#doctype"
					numberplanlist    = $numberplans
					input_name        = "document[number]"
					input_value       = $document.number
					select_name       = "document[numberplanid]"
					selected          = $document.numberplanid
					number_trigger    = "number"
					plan_trigger      = "numberplanid"
					time              = $document.cdate
					planDocumentType  = $planDocumentType
				}
			</td>
		</tr>
		<tr>
			<td class="valign-top">
				{icon name="period"}
			</td>
			<td class="valign-top">
				<strong>{trans("Period")}</strong>
			</td>
			<td>
				<div id="document-period">
					{$allow_past_date = ConfigHelper::checkConfig('documents.allow_past_date', true)}
					<div>
						<label>
							{trans("from")}
							<input type="text" name="document[fromdate]" id="fromdate"
								value="{if $document.fromdate}{$document.fromdate}{/if}"
								placeholder="{trans("yyyy/mm/dd")}"
								{tip class="lms-ui-date lms-ui-date-unix" text="Enter start date in YYYY/MM/DD format (optional)" trigger="fromdate"}
								{if !$allow_past_date}data-min-date="{date('Y-m-d')}"{/if}
								size="10">&nbsp;
						</label>
						<label>
							{trans("to")}
							<input type="text" name="document[todate]" id="todate"
								value="{if $document.todate}{$document.todate}{/if}"
								placeholder="{trans("yyyy/mm/dd")}"
								{tip class="lms-ui-date lms-ui-date-unix" text="Enter end date in YYYY/MM/DD format (optional)" trigger="todate"}
								{if !$allow_past_date}data-min-date="{date('Y-m-d')}"{/if}
								size="10">
						</label>
					</div>
					{date_period_preset from="#fromdate" to="#todate" periods="previous-year,previous-month,current-year,current-month,next-month,next-year"}
				</div>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="description"}
			</td>
			<td>
				<strong>{trans("Description")}</strong>
			</td>
			<td>
				<textarea name="document[description]" rows="5" cols="50"
					{tip text="Enter additional information (optional)"}>{$document.description}</textarea>
			</td>
		</tr>
		<tr id="document-file-row"{if !$document.type} style="display: none;"{/if}>
			<td>
				{icon name="import"}
			</td>
			<td>
				<strong>{trans("Files")}</strong>
			</td>
			<td>
				{fileupload id="attachments" fileupload=$fileupload form="documentgen-form"}
			</td>
		</tr>
		<tr id="attachment-row"{if !$attachment_result} style="display: none;"{/if}>
			<td>
				{icon name="import"}
			</td>
			<td class="nobr">
				<strong>{trans("Document attachments")}</strong>
			</td>
			<td id="attachment-cell">
				{$attachment_result}
			</td>
		</tr>
		<tr>
			<td>
				{icon name="confirm"}
			</td>
			<td class="nobr">
				<label for="closed">
					<strong>{trans("<!document>Confirmed")}</strong>
				</label>
			</td>
			<td>
				<input type="checkbox" name="document[closed]" id="closed" value="1"
					{if $document.closed} checked{/if}>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="legal-personality"}
			</td>
			<td class="nobr">
				<strong>{trans("Legal personality")}</strong>
			</td>
			<td>
				<select name="document[ctype]">
					<option value="">{trans("<!legal-personality>— any —")}</option>
					<option value="{$smarty.const.CTYPES_PRIVATE}"
						{if isset($document.ctype) && $document.ctype == $smarty.const.CTYPES_PRIVATE} selected{/if}
						>{$_CTYPES[$smarty.const.CTYPES_PRIVATE]}</option>
					<option value="{$smarty.const.CTYPES_COMPANY}"
							{if $document.ctype == $smarty.const.CTYPES_COMPANY} selected{/if}
						>{$_CTYPES[$smarty.const.CTYPES_COMPANY]}</option>
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="customer"}
			</td>
			<td class="nobr">
				<strong>{trans("Customers")}</strong>
			</td>
			<td>
				<table width="100%">
					<colgroup>
						<col style="width: 1%;">
					</colgroup>
					<tbody>
						<tr>
							<td width="1%" {tip text="Select customer's group"}>
								{foreach $_CSTATUSES as $statusidx => $status}
									<label class="nobr">
										<input type="radio" name="filter" value="{$statusidx}"
											{if (!isset($filter) && $statusidx == $smarty.const.CSTATUS_CONNECTED) || (isset($filter) && $statusidx == $filter)} checked{/if}>
										{$status.plurallabel}
									</label>
									<br>
								{/foreach}
								<label class="nobr">
									<input type="radio" name="filter" value="51"
										{if isset($filter) && $filter == 51} checked{/if}>
									{trans("with disconnected nodes")}
								</label>
								<br>
								<label class="nobr">
									<input type="radio" name="filter" value="52"
										{if isset($filter) && $filter == 52} checked{/if}>
									{trans("in debt")}
								</label>
								<br>
								<label class="nobr">
									<input type="radio" name="filter" value="-1"
										{if isset($filter) && $filter == -1} checked{/if}>
									{trans("without nodes")}
								</label>
								<br>
								<label class="nobr">
									<input type="radio" name="filter" value="0"
										{if isset($filter) && $filter == 0} checked{/if}>
									{trans("all<!customers>")}
								</label>
							</td>
						</tr>
					</tbody>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="ipnetwork"}
			</td>
			<td class="nobr">
				<strong>{trans("Network")}</strong>
			</td>
			<td>
				<select size="1" name="network" {tip text="Select network" trigger="customer"}>
					<option value="0">{trans("— all networks —")}</option>
					{foreach $networks as $net}
						<option value="{$net.id}"{if $network == $net.id} selected{/if}
							>{$net.name|truncate:40:"&hellip;":true}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="customergroup"}
			</td>
			<td class="nobr">
				<strong>{trans("Group")}</strong>
			</td>
			<td>
				<select size="1" name="customergroup[]"
					{tip text="Select group" trigger="customer" class="lms-ui-multiselect"}
					multiple data-default-value="{trans("— all groups —")}">
					{foreach $customergroups as $group}
						<option value="{$group.id}"
							{if is_array($customergroup) && in_array($group.id, $customergroup) || !is_array($customergroup) && $customergroup == $group.id} selected{/if}
							>{$group.name|trunescape:40}</option>
					{/foreach}
				</select>
			</td>
		</tr>
		<tr>
			<td colspan="3" class="lms-ui-box-buttons">
				<div id="document-button-panel">
					<div>
						{button type="submit" icon="save" id="submit-button" label="Submit"}
						{button type="submit" icon="print" label="Print" onclick="printdoc()" external=true}
						{button icon="cancel" label="Cancel" href="?m=documentlist"}
					</div>
				</div>
			</td>
		</tr>
	</tbody>
</table>
<br>
<div id="plugin" name="plugin">{$plugin_result}</div>
{$script_result}
</form>

<script>

	$(function() {
		$('form[name="documentgen"] [name="document[title]"]').focus();

		$('[name="document[type]"]').change(function() {
			$('#document-file-row').toggle(parseInt($(this).val()) != 0);
		});

		{if !empty($defaultDocEngine)}
			DocTemplateChanged();
		{/if}
	});

</script>
{/block}
