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

<form name="numberplanlist" id="numberplanlist" method="post" action="?m={$layout.module}">
</form>

<style>

	#clear-filter {
		cursor: pointer;
	}

</style>

<table class="lmsbox lms-ui-background-cycle">
	<colgroup>
		<col style="width: 92%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
		<col style="width: 1%;">
	</colgroup>
	<thead>
		<tr>
			<td class="nobr">
				<strong>
					{icon name="id"}
					{trans("Template")}
				</strong>
				<br>
				{icon name="calendar"}
				{trans("Validity period")}
			</td>
			<td class="text-right nobr">{trans("ID")}</td>
			<td class="text-right nobr">{trans("Example (next) number")}</td>
			<td class="text-right nobr">{trans("No. of issued documents:")}</td>
			<td class="text-right nobr">{trans("Document type")}</td>
			<td class="text-right nobr">{trans("Period")}</td>
			<td class="text-right nobr">{trans("Default")}</td>
			<td class="text-right">{trans("Relative to reference document")}</td>
			<td class="text-right nobr">
				{button icon="add" label="Add plan" href="?m=numberplanadd"}
			</td>
		</tr>
		<tr>
			<td colspan="9">
				<div class="lms-ui-filter-container">
					<div class="lms-ui-filter-definition">
						{icon name="filter" tip="Filter"}
						<label>
							{trans("Division")}
							<select name="divisionid" form="numberplanlist" data-default-value="0">
								<option value="0">{trans("— any —")}</option>
								{foreach $divisions as $division}
									<option value="{$division.id}"{if $division.id == $divisionid} selected{/if}
										>{$division.label|escape}</option>
								{/foreach}
							</select>
						</label>
						<label>
							{trans("User")}
							<select name="userid" form="numberplanlist" data-default-value="0">
								<option value="0">{trans("— any —")}</option>
								{foreach $users as $user}
									<option value="{$user.id}"{if $user.id == $userid} selected{/if}
										>{$user.rname|escape} ({$user.login})</option>
								{/foreach}
							</select>
						</label>
						<label>
							{trans("Type")}
							<select name="type" form="numberplanlist" data-default-value="0">
								<option value="0">{trans("— any —")}</option>
								{foreach Localisation::arraySort($_DOCTYPES) as $key => $label}
									<option value="{$key}"{if $key == $type} selected{/if}
									>{$label|escape}</option>
								{/foreach}
							</select>
						</label>
						{icon name="clear" id="clear-filter"}
					</div>
				</div>
			</td>
		</tr>
		{if $pagination->getTotal() != 0}
			<tr>
				<td class="lms-ui-pagination" colspan="9"">
					{include file="pagination.html"}
				</td>
			</tr>
		{/if}
	</thead>
	<tbody>
		{foreach $numberplanlist as $plan}
		<tr class="highlight {if (!empty($plan.datofrom) && $plan.datefrom > $smarty.now) || (!empty($plan.dateto) && $plan.dateto < $smarty.now)} blend{/if}" data-name="{$plan.template}">
			<td class="nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				<strong>
					{icon name="id"}
					{$plan.template}
					{if !empty($plan.divisions)}
						{capture assign="tip"}{foreach $plan.divisions as $division}{if !$division@first}<br>{/if}{$division.shortname|escape|escape}{/foreach}{/capture}
						{icon name="division" tip=$tip}
					{/if}
					{if !empty($plan.users)}
						{capture assign="tip"}{foreach $plan.users as $user}{if !$user@first}<br>{/if}{$user.rname|escape|escape} ({$user.login|escape|escape}){/foreach}{/capture}
						{icon name="user" tip=$tip}
					{/if}
					{if isset($plan.customertype)}
						{icon name="legal-personality" tip=$_CTYPES[$plan.customertype]}
					{/if}
				</strong>
				{if !empty($plan.datefrom) || !empty($plan.dateto)}
					<br>
					{icon name="calendar"}
					{if !empty($plan.datefrom)}
						{trans("from")}
						{$plan.datefrom|date_format:"Y/m/d"}
					{/if}
					{if !empty($plan.dateto)}
						{trans("to")}
						{$plan.dateto|date_format:"Y/m/d"}
					{/if}
				{/if}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				({$plan.id|string_format:"%04d"})
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{number number=$plan.next template=$plan.template time=$smarty.now}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{$plan.issued|default:"0"}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{assign var="doctype" value=$plan.doctype}
				{$_DOCTYPES.$doctype}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{assign var="period" value=$plan.period}
				{$_NUM_PERIODS.$period}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{if $plan.isdefault}{icon name="checked" tip="yes"}{/if}
			</td>
			<td class="text-right nobr" data-target-url="?m=numberplanedit&id={$plan.id}">
				{if $plan.refflag}{icon name="checked" tip="yes"}{/if}
			</td>
			<td class="text-right nobr">
				{if empty($plan.issued)}
					{button type="link" icon="delete" tip="Delete"
						class="delete-numberplan" href="?m=numberplandel&id={$plan.id}"}
				{else}
					{button type="link" icon="delete" tip="Number plan is used by existing documents!"
						class="delete-numberplan" href="?m=numberplandel&id={$plan.id}" disabled=true}
				{/if}
				{button type="link" icon="edit" tip="Edit" href="?m=numberplanedit&id={$plan.id}"}
			</td>
		</tr>
		{foreachelse}
		<tr>
			<td colspan="9" class="empty-table">
				{trans("No such numbering plans in database.")}
			</td>
		</tr>
		{/foreach}
	</tbody>
	<tfoot>
		{if $pagination->getTotal() != 0}
			<tr>
				<td class="lms-ui-pagination" colspan="9"">
					{include file="pagination.html"}
				</td>
			</tr>
		{/if}
		<tr>
			<td colspan="9" class="text-right">
				{button icon="add" label="Add plan" href="?m=numberplanadd"}
			</td>
		</tr>
	</tfoot>
</table>
<script>

	$(function() {
		$('.delete-numberplan:not([disabled])').click(function () {
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to delete numberplan '$a'?", name), this).done(function () {
				location.href = $(this).attr('href');
			});
			return false;
		});

		$('[form="numberplanlist"]').change(function() {
			$('#numberplanlist').submit();
		});

		$('#clear-filter').click(function() {
			var reloadRequired = false;
			$(this).closest('.lms-ui-filter-definition').find('[data-default-value]').each(function() {
				var currVal = parseInt($(this).val());
				var defVal = parseInt($(this).attr('data-default-value'));
				if (currVal != defVal) {
					$(this).val(defVal);
					reloadRequired = true;
				}
			});
			if (reloadRequired) {
				$('#numberplanlist').submit();
			}
		});
	});

</script>
{/block}
