<!--// $Id$ //-->

{$default_all_attachments = ConfigHelper::checkConfig('documents.default_all_attachments')}

{css}
{js}

<form method="POST" name="customerdocuments" id="customerdocuments-form">
</form>

{tab_container id="customerdocuments" label="Customer Documents"}

	{tab_header content_id="docpanel"}
		{tab_header_cell icon="lms-ui-icon-document"}
			<strong>{trans("Customer Documents")}</strong>&nbsp;({t a=$limit}last $a documents{/t})
		{/tab_header_cell}
		{tab_header_cell}
			<a href="?m=documentadd&cid={$customerinfo.id}">
				{trans("New Document")}
				{icon name="next" class="fa-fw"}
			</a>
			<a href="?m=documentlist&c={$customerinfo.id}">
				{trans("All Documents")}
				{icon name="next" class="fa-fw"}
			</a>
		{/tab_header_cell}
	{/tab_header}

	{tab_contents id="docpanel"}

		{tab_table}

			{if $documents}
			<div class="lms-ui-tab-table-row header">
				<div class="lms-ui-tab-table-wrapper col-6">
					<div class="lms-ui-tab-table-wrapper col-4">
						<div class="lms-ui-tab-table-wrapper col-2">
							<div class="lms-ui-tab-table-column number">
								<strong>{trans("Number:")}</strong>
							</div>
							<div class="lms-ui-tab-table-column creation-date">
								{trans("Creation date<!document>")} / {trans("Confirmation date<!document>")}:
							</div>
						</div>
						<div class="lms-ui-tab-table-wrapper col-2">
							<div class="lms-ui-tab-table-column created-by">
								{trans("Created by<!document>")} / {trans("Confirmed by<!document>")}:
							</div>
							<div class="lms-ui-tab-table-column type">
								<strong>{trans("Type:")}</strong>
							</div>
						</div>
					</div>

					<div class="lms-ui-tab-table-wrapper col-3">
						<div class="lms-ui-tab-table-column title">
							{trans("Title:")}
						</div>
						<div class="lms-ui-tab-table-column description">
							{trans("Description:")}
						</div>
						<div class="lms-ui-tab-table-column period">
							{trans("Period:")}
						</div>
					</div>
				</div>

				<div class="lms-ui-tab-table-column buttons">
					&nbsp;
				</div>
			</div>
			{/if}

			{foreach $documents as $doc}
			{if empty($doc.attachments)}
				{$doc.attachments = array()}
			{/if}
			{$docattach = reset($doc.attachments)}
			{capture assign="url"}?m=documentview&id={$doc.docid}{if $default_all_attachments}&attachments=1{elseif $doc.attachments|size == 1}&attachmentid={$docattach.id}{/if}{/capture}
			<div class="lms-ui-tab-table-row{if $doc.closed} lms-ui-document-closed{/if}{if $doc.archived} lms-ui-document-archived{/if}"
				 data-senddate="{if $doc.senddate}{$doc.senddate|date_format:"Y-m-d H:i"}{else}{$doc.senddate}{/if}">
				<div class="lms-ui-tab-table-wrapper col-6">
					<div class="lms-ui-tab-table-wrapper col-4">
						<div class="lms-ui-tab-table-wrapper col-2">
							<div class="lms-ui-tab-table-column number" data-target-url="{$url}"
									{if $doc.description} {tip text=$doc.description}{/if}>
								<strong>{number number=$doc.number template=$doc.template customerid=$customerinfo.id time=$doc.cdate}</strong>
							</div>
							<div class="lms-ui-tab-table-column creation-date" data-target-url="{$url}">
								{$doc.cdate|date_format:"Y-m-d H:i"}{if $doc.sdate} / {$doc.sdate|date_format:"Y-m-d H:i"}{/if}
							</div>
						</div>
						<div class="lms-ui-tab-table-wrapper col-2">
							<div class="lms-ui-tab-table-column created-by" data-target-url="{$url}">
								{$doc.username|escape}
								{if $doc.cuserid} / {$doc.cusername|escape}{/if}
								{if $doc.closed == $smarty.const.DOC_CLOSED_AFTER_CUSTOMER_SMS}
									{if !$doc.cuserid}
										{icon name="sms" tip="customer has accepted document via sms"}
									{/if}
								{elseif $doc.closed == $smarty.const.DOC_CLOSED_AFTER_CUSTOMER_SCAN}
									{icon name="sign" tip="attached document scans with customer signature"}
								{/if}
							</div>
							<div class="lms-ui-tab-table-column type" data-target-url="{$url}">
								<strong>{assign var=type value=$doc.type}{$_DOCTYPES.$type}</strong>
							</div>
						</div>
					</div>

					<div class="lms-ui-tab-table-wrapper col-3">
						<div class="lms-ui-tab-table-column title" data-target-url="{$url}">
							{$doc.title|truncate:40:"&hellip;":true}
						</div>
						<div class="lms-ui-tab-table-column description">
							{if $doc.description}
								{$doc.description|escape|replace:"\n":'<br>'}
							{else}
								&ndash;
							{/if}
						</div>
						<div class="lms-ui-tab-table-column period" data-target-url="{$url}">
							{if $doc.fromdate}{trans("from")} {$doc.fromdate|date_format:"Y-m-d"}{/if}
							{if $doc.todate}{trans("to")} {$doc.todate|date_format:"Y-m-d"}{/if}
							{if empty($doc.closed) && ($doc.fromdate || $doc.todate) && !empty($doc.dynamicperiod)}{trans("<!document-period>(dynamic)")}{/if}
						</div>
					</div>
				</div>

				<div class="lms-ui-tab-table-column buttons" style="flex-direction: column;">
					<div class="lms-ui-tab-table-column buttons">
						{if $customerinfo.senddocuments}
							{textformat assign="tip" wrap=5000}
								{if $doc.senddate}{trans("Send document again")}<br><strong>{t a=$doc.senddate|date_format:"Y-m-d H:i"}(already sent on $a){/t}</strong>{else}{trans("Document send")}{/if}
							{/textformat}
							{button type="link" class="send-doc" icon="{if $doc.senddate}mailsent{else}mail{/if}" href="?m=documentsend&id={$doc.docid}"
								tip=$tip}
						{/if}
						{assign var=type value=$doc.type}
						{button type="link" icon="save" href="?m=documentview&id={$doc.docid}&save=1" tip="Save"}
						{if ! $doc.closed && ($docrights.$type.rights & $smarty.const.DOCRIGHT_CONFIRM)}
							{button type="link" icon="confirm" href="?m=documentedit&id={$doc.docid}&action=confirm" tip="Confirm"}
						{/if}
						{if $doc.closed && !$doc.archived && ($docrights.$type.rights & $smarty.const.DOCRIGHT_ARCHIVE)}
							{button type="link" href="?m=documentedit&id={$doc.docid}&action=archive" class="archive-doc" icon="archive" tip="Archive"}
						{/if}
						{if ($docrights.$type.rights & $smarty.const.DOCRIGHT_DELETE)}
							{button type="link" class="delete-doc" href="?m=documentdel&id={$doc.docid}" icon="delete" onclick=$onclick tip="Remove document"}
						{/if}
						{if ($docrights.$type.rights & $smarty.const.DOCRIGHT_EDIT)}
							{button type="link" icon="edit" href="?m=documentedit&id={$doc.docid}" tip="Edit"}
						{/if}
						{if is_array($doc.attachments)}
							{if $docattach.type == 1 || count($doc.attachments) == 1}
								{documentview
									id="global-{$doc.docid}-{$docattach.id}"
									type=$docattach.contenttype
									doctype=$type
									name=$docattach.filename
									url=$url
									external=true
									text="<i class=\"lms-ui-icon-view{if preg_match('/pdf/',$docattach.contenttype)} pdf{elseif preg_match('/csv|excel|spreadsheet/',$docattach.contenttype)} xls{/if}\"></i>"
								}
							{/if}
							{if count($doc.attachments) > 1 || !$docattach.type < 1}
								<a href="#" id="allattachments-toggle-{$doc.docid}" onclick="toggle_all_attachments({$doc.docid}); return false;" title="{trans("more document attachments")}">
									<img src="img/asc_order.gif">
								</a>
							{/if}
						{/if}
						<INPUT TYPE="checkbox" NAME="marks[{$doc.docid}]" form="customerdocuments-form" class="lms-ui-multi-check" VALUE="{$doc.docid}">
					</div>
					<div id="allattachments-{$doc.docid}" class="document-attachment-container" style="display: none;">
						{foreach $doc.attachments as $docattach}
							{documentview
								id="local-{$doc.docid}-{$docattach.id}"
								type=$docattach.contenttype
								name=$docattach.filename
								url="?m=documentview&id={$doc.docid}&attachmentid={$docattach.id}"
								external=true
							}
							{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}
							<br>
						{/foreach}
					</div>
				</div>

			</div>
			{foreachelse}
			<div class="lms-ui-tab-empty-table">
				{trans("That customer hasn't got any documents.")}
			</div>
			{/foreach}

		{/tab_table}

{if !empty($documents) && count($documents)}
		{tab_button_panel}
			{tab_buttons}
				{button id="send-documents" icon="mail" label="Send documents" data_send_documents="{$customerinfo.senddocuments}"}
				{button id="confirm-docs" icon="confirm" label="Confirm"}
				{button id="archive-docs" icon="archive" label="Archive"}
				{button id="delete-docs" icon="delete" label="Delete"}
			{/tab_buttons}
			{tab_buttons}
				<label>
					{trans("Check All")}
					<INPUT TYPE="checkbox" class="lms-ui-multi-check-all" VALUE="1">
				</label>
			{/tab_buttons}
		{/tab_button_panel}
		{tab_button_panel}
			{tab_buttons}
				{button id="print-docs" icon="print" label="Print"}
				<fieldset>
					<label>
						<input type="checkbox" name="attachments" value="1" form="customerdocuments-form">
						{trans("attachments")}
					</label>
					<label>
						<input type="checkbox" name="related" value="1" form="customerdocuments-form">
						{trans("related documents")}
					</label>
					<select name="related_documents[]" form="customerdocuments-form" multiple class="lms-ui-multiselect" }
							data-default-value="{trans("- all -")}" data-shorten-to-default-value="false">
						{foreach Localisation::arraySort($_DOCTYPES) as $key => $related_document}
							{if $key < 0}
								<option value="{$key}">{$related_document}</option>
							{/if}
						{/foreach}
					</select>
				</fieldset>
			{/tab_buttons}
		{/tab_button_panel}
{/if}

	{/tab_contents}
{/tab_container}
