{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;
	}

</style>

<form name="documentedit" method="POST" id="documentedit-form" action="?m=documentedit&id={$document.id}" enctype="multipart/form-data">
<input type="submit" class="hiddenbtn">
<input type="hidden" name="customerid" value="{$document.customerid}">
<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="setnumberplans();"
						{tip class="lms-ui-advanced-select-test" text="Select document type" trigger="type"}>
					{foreach Localisation::arraySort($_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>
			<td>
				{icon name="document"}
			</td>
			<td class="bold nobr">
				<strong>{trans("Title:")}</strong>
			</td>
			<td>
				<input type="text" name="document[title]" value="{$document.title}" required
					{tip text="Enter document title" trigger="title"} size="40">
			</td>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>
				{icon name="customer"}
			</td>
			<td class="nobr">
				<strong>{trans("Customer:")}</strong>
			</td>
			<td>
				<a href="?m=customerinfo&id={$document.customerid}">{$document.customer_name}</a>
			</td>
		</tr>
		<tr>
			<td>
				{icon name="id"}
			</td>
			<td>
				<strong>{trans("Number:")}</strong>
			</td>
			<td>
				{numberplan_selection
					doctype_selector  = "#doctype"
					cdate_selector    = "[name='document[cdate]']"
					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
					customer_id       = $document.customerid
					planDocumentType  = $planDocumentType
				}
			</td>
		</tr>
		{if !$document.closed}
		<tr>
			<td>
				{icon name="calendar"}
			</td>
			<td class="nobr">
				<strong>{trans("Consent date")}</strong>
			</td>
			<td class="nobr">
				{if ConfigHelper::checkPrivilege("document_consent_date")}
					<input type="text" name="document[cdate]" value="{$document.cdate|date_format:"Y/m/d"}" size="12"
						placeholder="{trans("yyyy/mm/dd")}" data-cdate="{$document.cdate}"
						{tip class="lms-ui-date" text="Enter date of settlement in YYYY/MM/DD format (empty field means current date) or click to select it from calendar" trigger="cdate"}
					>
					{if $document.cdatewarning}
						<input type="hidden" name="document[cdatewarning]" value="1">
					{/if}
				{else}
					<input type="hidden" name="document[cdate]" value="{$document.cdate|date_format:"Y/m/d"}">
					{$document.cdate|date_format:"Y-m-d"}
				{/if}
			</td>
		</tr>
		{/if}
		<tr>
			<td>
				{icon name="period"}
			</td>
			<td>
				<strong>{trans("Period:")}</strong>
			</td>
			<td>
				{trans("from:")}
				<input type="text" name="document[fromdate]" value="{if $document.fromdate}{$document.fromdate}{/if}"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date" text="Enter start date in YYYY/MM/DD format (optional)" trigger="fromdate"} size="10">&nbsp;
				{trans("to:")}
				<input type="text" name="document[todate]" value="{if $document.todate}{$document.todate}{/if}"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date" text="Enter end date in YYYY/MM/DD format (optional)" trigger="todate"} size="10">
			</td>
		</tr>
		{if !$document.closed}
		<tr>
			<td>
				{icon name="period"}
			</td>
			<td>
				<strong>{trans("Wait for customer action till:")}</strong>
			</td>
			<td>
				<input type="text" name="document[confirmdate]" id="confirmdate"
					value="{if $document.confirmdate > 0}{$document.confirmdatestring}{/if}"
					placeholder="{trans("yyyy/mm/dd")}"
					{tip class="lms-ui-date" text="Enter date in YYYY/MM/DD format we wait for customer action till (optional)" trigger="confirmdate"}
					size="10">
				{if $document.confirmdate == -1}
					<span class="red">
						<strong>{trans("Warning! Customer submitted signed document scan. Consider to review it and then possibly confirm.")}</strong>
					</span>
				{elseif $document.confirmdate && $document.confirmdate < $smarty.now}
					<span class="red">
						<strong>{trans("Warning! Customer action time limit is expired is expired!")}</strong>
					</span>
				{/if}
			</td>
		</tr>
		{/if}
		<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>
			<td>
				{icon name="import"}
			</td>
			<td class="nobr">
				<strong>{trans("New files:")}</strong>
			</td>
			<td>
				{fileupload id="attachments" fileupload=$fileupload form="documentedit-form"}
			</td>
		</tr>
		{if !empty($document.attachments)}
		<tr>
			<td>
				{icon name="import"}
			</td>
			<td class="nobr">
				<strong>{trans("Attached files:")}</strong>
			</td>
			<td>
				{foreach $document.attachments as $docattach}
				<div class="docattachrow{if $docattach.type <= 0} docattach{if $document.attachments[$docattach.id].deleted} blend crossed{/if}{/if}">
					<a class="lms-ui-button toggle-button">
						<i class="fa-fw lms-ui-icon-{if $document.attachments[$docattach.id].deleted}restore{else}delete{/if}"
							{if $docattach.type == 1} style="visibility: hidden;"{/if}></i>
					</a>
					{$class = ""}
					{if $document.attachments[$docattach.id].deleted}
						{$class = "blend"}
					{/if}
					{documentview
						id="{$document.id}-{$docattach.id}"
						type=$docattach.contenttype
						name=$docattach.filename
						url="?m=documentview&id={$document.id}&attachmentid={$docattach.id}"
						external=true
						class=$class
					}
					{if $docattach.type == -1}<i class="lms-ui-icon-user" title="<strong>{t a=date("Y/m/d H:i", $docattach.cdate)}Sent by customer at $a{/t}</strong>"></i>{/if}
					<input type="hidden" name="document[attachments][{$docattach.id}][deleted]" value="{$document.attachments[$docattach.id].deleted}">
				</div>
				{/foreach}
			</td>
		</tr>
		{/if}
{*
		<tr>
			<td>
				{icon name="template"}
			</td>
			<td>
				<strong>{trans("Template:")}</strong>
			</td>
			<td>
				<select size="1" name="document[template]" {tip text="Select template to generate new document if you haven't got prepared file" trigger="template"}>
					<option value="0">... {trans("select template")} ...</option>
					{foreach $docengines as $item}
						<option value="{$item.name}"{if $document.template == $item.name} selected{/if}>{$item.title}</option>
					{/foreach}
				</select>
			</td>
		</tr>
*}
		{if (!$document.closed && ($document.docrights & $smarty.const.DOCRIGHT_CONFIRM)) || ConfigHelper::checkPrivilege('full_access')}
		<tr>
			<td>
				{icon name="confirm"}
			</td>
			<td class="nobr">
				<label for="closed">
					<strong>{trans("Confirmed:")}</strong>
				</label>
			</td>
			<td>
				<input type="hidden" name="document[closed]" value="0">
				<input type="checkbox" name="document[closed]" id="closed" value="1" {if $document.closed} checked{/if}>
			</td>
		</tr>
		{/if}
		{if !empty($document.closed) && ($document.docrights & $smarty.const.DOCRIGHT_ARCHIVE)}
		<tr>
			<td>
				{icon name="archive"}
			</td>
			<td class="nobr">
				<label for="archived">
					<strong>{trans("Archived:")}</strong>
				</label>
			</td>
			<td>
				<input type="checkbox" name="document[archived]" id="archived" value="1" {if $document.archived} checked{/if}>
			</td>
		</tr>
		{/if}
		<tr>
			<td colspan="3" class="lms-ui-box-buttons">
				{button icon="save" label="Submit" onclick="document.documentedit.submit();"}
				{button icon="cancel" label="Cancel" href="?m=customerinfo&id={$document.customerid}"}
			</td>
		</tr>
	</tbody>
</table>
</form>

<script>

	function setnumberplans() {
		reset_login_timeout();
	}

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

		$('.toggle-button').on('click', function() {
			var elem = $(this);
			elem.siblings('.documentview').find('.lms-ui-button').toggleClass('blend')
			elem.find('i').toggleClass('lms-ui-icon-delete').toggleClass('lms-ui-icon-restore');
			elem.siblings('input').val(+elem.find('i').is('.lms-ui-icon-restore'));
			return false;
		});

		$('#closed').change(function() {
			if ($('#archived').prop('checked') && !$(this).prop('checked')) {
				$(this).prop('checked', true);
				alertDialog($t("Cannot undo document confirmation while it is archived!"), this);
			}
			$('#archived').closest('tr').toggle($(this).prop('checked'));
			$('#confirmdate').closest('tr').toggle(!$(this).prop('checked'));
		});
	});

</script>
{/block}
