{extends file="layout.html"}
{block name=title}LMS: {$layout.pagetitle|striphtml}{/block}
{block name=module_content}
<!--// $Id$ //-->
	<style>
		@media screen and (max-width: 500px) {
			.lms-ui-filter-container {
				flex-wrap: wrap;
			}

			thead tr {
				display: flex;
				flex-direction: column;
			}
			thead td.text-right {
				text-align: left;
			}
			thead td:last-child {
				text-align: right;
			}
			thead td:first-child > img {
				display: none;
			}

			tbody tr {
				display: flex;
				flex-direction: column;
			}
			tbody td.text-right{
				text-align: left;
				padding-left: 0.3em;
			}
			tbody td:first-child > img:first-child {
				display: none;
			}
			tbody td:first-child > img:last-child {
				display: none;
			}

			tfoot tr {
				display: flex;
				justify-content: space-between;
			}
			tfoot td{
			 flex-grow: 1
			}
			.summary td:last-child {
				text-align: right;
			}
			.summary td:nth-last-child(2) {
				text-align: left;
			}

			table.lmsbox>tfoot>tr:first-child, table.lmsbox>thead>tr:first-child {
				border-top: 0px;
			}

            .lms-ui-filter-definition label {
                text-align: left;
            }
		}
	</style>
<H1>{$layout.pagetitle}</H1>

<FORM method="get" action="?m={$layout.module}" name="choosefilter" id="choosefilter">
<p style="display: none;">
<INPUT type="submit" class="hiddenbtn">
<INPUT type="hidden" NAME="m" VALUE="customerlist">
</p>
<TABLE class="lmsbox lms-ui-background-cycle">
	<COLGROUP>
                {block name="customerlist-list-columns"}
		<COL style="width: 97%;">
		<COL style="width: 1%;" span="3">
                {assign var='number_of_table_columns' value='4'}
                {/block}
	</COLGROUP>
	<THEAD>
        {block name="customerlist-list-header"}
	<TR {tip text="Click on column name to change sorting order"}>
		<td scope="col" class="nobr">
			{icon name="customer"}
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=customername{if $filter.direction == "asc" && $filter.order == "customername"},desc{/if}">
				{trans("First/last or Company name")}
			</A>
			{if $filter.order == "customername"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
			/
			{icon name="karma"}
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=karma{if $filter.direction == "asc" && $filter.order == "karma"},desc{/if}">
				{trans("Karma")}
			</A>
			{if $filter.order == "karma"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
			<br>
			{icon name="home"}
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=address{if $filter.direction == "asc" && $filter.order == "address"},desc{/if}">
				{trans("Address")}
			</A>
			{if $filter.order == "address"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		</td>
		<td scope="col" class="text-right nobr">
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=extid{if $filter.direction == "asc" && $filter.order == "extid"},desc{/if}"><span class="bold">{trans("External ID")}</span></A> {if $filter.order == "extid"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
			/
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=id{if $filter.direction == "asc" && $filter.order == "id"},desc{/if}"><span class="bold">{trans("ID")}</span></A> {if $filter.order == "id"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
			<BR>
			{trans("Services")}
		</td>
		<td scope="col" class="text-right">
		{if ConfigHelper::checkConfig('privileges.superuser') || !ConfigHelper::checkConfig('privileges.hide_finances')}
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=balance{if $filter.direction == "asc" && $filter.order == "balance"},desc{/if}">{trans("Balance")}</A>&nbsp;{if $filter.order == "balance"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}<BR>
			<a href="?m={$layout.module}{if !empty($filter.state)}&s[]={implode('&s[]=', $filter.state)}{/if}&o=tariff{if $filter.direction == "asc" && $filter.order == "tariff"},desc{/if}">{trans("Subscription")}</A>&nbsp;{if $filter.order == "tariff"}<IMG src="img/{if $filter.direction == "desc"}asc{else}desc{/if}_order.gif" alt="">{/if}
		{/if}
		</td>
		<td scope="col" class="text-right nobr">
                        {t a=$pagination->getTotal()}Total: $a{/t}
		</td>
	</TR>
        {/block}
        {block name="customerlist-list-filters"}
	<TR>
		<TD colspan="{$number_of_table_columns}" class="nobr">
			<div class="lms-ui-filter-container">
			<div class="lms-ui-filter-definition">
			{icon name="filter"}
			<label for="s">
				{trans("Customers")}
				<input type="hidden" name="s[]" value="0">
				<select size="1" name="s[]" onchange="document.choosefilter.submit();" class="lms-ui-multiselect" multiple data-default-value="{trans("— all customers —")}">
					<optgroup label="Status">
						{foreach $_CSTATUSES as $statusidx => $status}
							<option value="{$statusidx}"{if in_array($statusidx, $filter.state)} selected{/if}>{$status.plurallabel}</option>
						{/foreach}
					</optgroup>
					<optgroup label="{trans("Customers")}">
						<option value="50"{if in_array(50, $filter.state)} selected{/if}>{trans("deleted<!plural>")}</option>
						<option value="72"{if in_array(72, $filter.state)} selected{/if}>{trans("existing<!plural>")}</option>
						<option value="53"{if in_array(53, $filter.state)} selected{/if}>{trans("online")}</option>
						<option value="56"{if in_array(56, $filter.state)} selected{/if}>{trans("suspended<!plural>")}</option>
						<option value="54"{if in_array(54, $filter.state)} selected{/if}>{trans("without group")}</option>
					</optgroup>
					<optgroup label="{trans("Documents")}">
						<option value="73"{if in_array(73, $filter.state)} selected{/if}>{trans("with unarchived documents")}</option>
					</optgroup>
					<optgroup label="{trans("Nodes")}">
						<option value="63"{if in_array(63, $filter.state)} selected{/if}>{trans("with connected nodes")}</option>
						<option value="51"{if in_array(51, $filter.state)} selected{/if}>{trans("with disconnected nodes")}</option>
						<option value="64"{if in_array(64, $filter.state)} selected{/if}>{trans("with nodes")}</option>
						<option value="65"{if in_array(65, $filter.state)} selected{/if}>{trans("without nodes")}</option>
						<option value="84"{if in_array(84, $filter.state)} selected{/if}>{trans("with all nodes not connected to network device")}</option>
					</optgroup>
					<optgroup label="{trans("Contracts")}">
						<option value="59"{if in_array(59, $filter.state)} selected{/if}>{trans("without contracts")}</option>
						<option value="76"{if in_array(76, $filter.state)} selected{/if}>{trans("without active contracts")}</option>
						<option value="60"{if in_array(60, $filter.state)} selected{/if}>{trans("with expired contracts")}</option>
						<option value="77"{if in_array(77, $filter.state)} selected{/if}>{trans("with expired, active contracts")}</option>
						<option value="61"{if in_array(61, $filter.state)} selected{/if}>{trans("with expiring contracts")}</option>
						<option value="78"{if in_array(78, $filter.state)} selected{/if}>{trans("with expiring, active contracts")}</option>
					</optgroup>
					<optgroup label="{trans("Finances")}">
						{if !ConfigHelper::checkPrivilege('hide_finances')}
							<option value="52"{if in_array(52, $filter.state)} selected{/if}>{trans("in debt")}</option>
							<option value="57"{if in_array(57, $filter.state)} selected{/if}>{trans("in debt above 100%")}</option>
							<option value="58"{if in_array(58, $filter.state)} selected{/if}>{trans("in debt above 200%")}</option>
							<option value="82"{if in_array(82, $filter.state)} selected{/if}>{trans("in debt above or equal 100%")}</option>
							<option value="83"{if in_array(83, $filter.state)} selected{/if}>{trans("in debt above or equal 200%")}</option>
							<option value="71"{if in_array(71, $filter.state)} selected{/if}>{trans("overdue receivables")}</option>
						{/if}
						<option value="55"{if in_array(55, $filter.state)} selected{/if}>{trans("without tariff")}</option>
						<option value="62"{if in_array(62, $filter.state)} selected{/if}>{trans("with e-invoice")}</option>
						<option value="66"{if in_array(66, $filter.state)} selected{/if}>{trans("without invoice flag")}</option>
						<option value="75"{if in_array(75, $filter.state)} selected{/if}>{trans("with discount")}</option>
						<option value="79"{if in_array(79, $filter.state)} selected{/if}>{trans("with active tariffless liabilities")}</option>
						<option value="80"{if in_array(80, $filter.state)} selected{/if}>{trans("with tariffless liabilities")}</option>
						<option value="86"{if in_array(86, $filter.state)} selected{/if}>{trans("with recipient address")}</option>
					</optgroup>
					<optgroup label="{trans("Addresses")}">
						<option value="67"{if in_array(67, $filter.state)} selected{/if}>{trans("without building number")}</option>
						<option value="69"{if in_array(69, $filter.state)} selected{/if}>{trans("without city")}</option>
						<option value="70"{if in_array(70, $filter.state)} selected{/if}>{trans("TERYT not specified")}</option>
						<option value="68"{if in_array(68, $filter.state)} selected{/if}>{trans("without zip (main address)")}</option>
						<option value="74"{if in_array(74, $filter.state)} selected{/if}>{trans("without zip (other addresses)")}</option>
						<option value="81"{if in_array(81, $filter.state)} selected{/if}>{trans("without location address")}</option>
					</optgroup>
				</select>
			</label>

			{division_selection name="d" selected=$filter.division onchange="document.choosefilter.submit();" label="Division" tip=""}

			<label>
				{trans("Legal personality")}
				<select name="type" onchange="document.choosefilter.submit();">
					<option value="-1">{trans("— all customers —")}</option>
					{foreach $_CTYPES as $key => $item}
						<option value="{$key}"{if $filter.type === $key} selected{/if}>{$item}</option>
					{/foreach}
				</select>
			</label>

			<label for="n" class="nobr">
			{trans("Network")}
			<SELECT SIZE="1" NAME="n" ONCHANGE="document.choosefilter.submit();">
				<OPTION value="0" {if !$filter.network} SELECTED {/if}>{trans("— all networks —")}</OPTION>
				{section name=networks loop=$networks}
				<OPTION value="{$networks[networks].id}" {if $filter.network == $networks[networks].id} SELECTED {/if}>{$networks[networks].name|truncate:30:"&hellip;":true}</OPTION>
				{/section}
			</SELECT>
			</label>

			{if is_array($customergroups) && count($customergroups)}
				<label for="g[]">
					{trans("Group")}
				</label>
				<select name="gop" onChange="document.choosefilter.submit();"
					{tip text="logical operator" class="text-center"}>
					<option value="AND"{if $filter.customergroupsqlskey == 'AND'} selected{/if}>{trans("and")}</option>
					<option value="OR"{if $filter.customergroupsqlskey == 'OR'} selected{/if}>{trans("or")}</option>
				</select>
				<label for="g[]">
					<input type="hidden" name="g[]" value="">
					<select size="1" name="g[]" onChange="document.choosefilter.submit();"
						class="lms-ui-multiselect lms-ui-multiselect-filter" multiple data-default-value="{trans("— all groups —")}">
						{foreach $customergroups as $customergroup}
							<option value="{$customergroup.id}"
								{if (is_array($filter.customergroup) && in_array($customergroup.id, $filter.customergroup))
									|| (!is_array($filter.customergroup) && $filter.customergroup == $customergroup.id)} selected{/if}>{$customergroup.name|trunescape:50}</option>
						{/foreach}
					</select>
				</label>
			{else}
				<input type="hidden" name="g[]" value="">
			{/if}

			{if !empty($nodegroups) && count($nodegroups)}
				<label for="ng[]">
					{trans("Node Group<!short>")}
					<input type="hidden" name="ng" value="all">
					<select size="1" name="ng[]" onchange="document.choosefilter.submit();"
							class="lms-ui-multiselect lms-ui-multiselect-filter" multiple>
						<option value=""{if is_array($filter.nodegroup) && empty($filter.nodegroup) || !is_array($filter.nodegroup) && !$filter.nodegroup} selected{/if}
							data-exclusive>{trans("— all groups —")}</option>
						{foreach $nodegroups as $nodegroup}
							<option value="{$nodegroup.id}"
								{if (is_array($filter.nodegroup) && in_array($nodegroup.id, $filter.nodegroup))
								|| (!is_array($filter.nodegroup) && $filter.nodegroup == $nodegroup.id)} selected{/if}
							>{$nodegroup.name|trunescape:30}</option>
						{/foreach}
					</select>
				</label>
			{else}
				<input type="hidden" name="ng" value="all">
			{/if}

			<label for="assignments">
			{trans("Assignments")}
			<select size="1" name="assignments" onchange="document.choosefilter.submit();" {if $filter.assignments}class="active"{/if}>
				<option value="0" {if $filter.assignments eq '0'} selected {/if}>{trans('— all —')}</option>
				<option value="7" {if $filter.assignments eq '7'} selected {/if}>{trans('expire in $a days', 7)}</option>
				<option value="14" {if $filter.assignments eq '14'} selected {/if}>{trans('expire in $a days', 14)}</option>
				<option value="30" {if $filter.assignments eq '30'} selected {/if}>{trans('expire in $a days', 30)}</option>
				<option value="60" {if $filter.assignments eq '60'} selected {/if}>{trans('expire in $a days', 60)}</option>
				<option value="90" {if $filter.assignments eq '90'} selected {/if}>{trans('expire in $a days', 90)}</option>
				<option value="-1" {if $filter.assignments eq '-1'} selected {/if}>{trans('without end date')}</option>
				<option value="-2" {if $filter.assignments eq '-2'} selected {/if}>{trans('active')}</option>
				<option value="-3" {if $filter.assignments eq '-3'} selected {/if}>{trans('active, with invoice')}</option>
				<option value="-5" {if $filter.assignments eq '-5'} selected {/if}>{trans('active, with pro forma')}</option>
				<option value="-4" {if $filter.assignments eq '-4'} selected {/if}>{trans('suspended')}</option>
				<option value="-9" {if $filter.assignments eq '-9'} selected {/if}>{trans('all suspended')}</option>
			</select>
			</label>

			<label>
				{trans("Flags")}
				<input type="hidden" name="flags[]" value="0">
				<select name="flags[]" {tip text="Select customer flags (optional)" class="lms-ui-multiselect"}
						multiple onchange="document.choosefilter.submit();">
					{foreach Localisation::arraySort($_CUSTOMERFLAGS, 'label') as $cflag => $flag}
						<option value="{$cflag}" title="{$flag.tip}"
							{if is_array($filter.flags) && in_array($cflag, $filter.flags)} selected{/if}>{$flag.label}</option>
					{/foreach}
				</select>
			</label>

			</div>
			{persistent_filter}
			</div>
		</TD>
	</TR>
        {/block}
        {block name="customerlist-list-header-pagination"}
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" colspan="{$number_of_table_columns}">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
        {/block}
	</THEAD>
	<TFOOT>
        {block name="customerlist-list-footer-pagination"}
	{if $pagination->getTotal() != 0}
	<TR>
		<TD class="lms-ui-pagination" colspan="{$number_of_table_columns}">
			{include file="pagination.html"}
		</TD>
	</TR>
	{/if}
        {/block}
        {block name="customerlist-list-footer"}
	<TR class="summary">
		<TD>
			&nbsp;
		</TD>
		<TD class="bold">
			{if ConfigHelper::checkConfig('privileges.superuser') || (!ConfigHelper::checkConfig('privileges.hide_finances') && !ConfigHelper::checkConfig('privileges.hide_summaries'))}
			{trans("Outstandings")}:<BR>
			{trans("Overcharges")}:
			{/if}
		</TD>
		<TD class="bold nobr">
			{if ConfigHelper::checkConfig('privileges.superuser') || (!ConfigHelper::checkConfig('privileges.hide_finances') && !ConfigHelper::checkConfig('privileges.hide_summaries'))}
			{$filter.below|money_format}<BR>
			{$filter.over|money_format}
			{/if}
		</TD>
		<TD class="bold">
			{t a=$pagination->getTotal()}Total: $a{/t}
		</TD>
	</TR>
        {/block}
	</TFOOT>
	<TBODY>

	{foreach $customerlist as $customer}
        {block name="customerlist-list-row"}
	<TR class="highlight {if $customer.deleted} lms-ui-resource-deleted{/if} {if $customer.account && !$customer.nodeac} blend{/if}"
		data-target-url="?m=customerinfo&id={$customer.id}" data-name="{$customer.customername|escape:"javascript"|escape:"html"}">
		<TD>
			<i class="lms-ui-icon-customer-status-{$_CSTATUSES[$customer.status].alias} fa-fw"></i>
			{if $customer.info}
				<i class="lms-ui-icon-optional-info" title="{nl2br(htmlspecialchars($customer.info))}"></i>
			{/if}
			<a name="{$customer.id}" class="lms-ui-hint-rollover"
				data-url="?m=customerinfoshort&id={$customer.id}">
				<span class="bold">{$customer.customername|escape}</span>
			</a>
			{if !empty($customer.karma)}
				<i class="lms-ui-icon-star{if $customer.karma > 0} green{else} red{/if}" title="{trans('Karma')}"></i>
				(<span class="lms-ui-counter">{$customer.karma}</span>)
			{/if}
			<BR>
			<i class="lms-ui-icon-home fa-fw"></i>&nbsp;{$customer.full_address}{if $customer.country != ""}, {t}{$customer.country}{/t}{/if}
		</TD>
		<TD class="text-right nobr">
			({if $customer.extid}{$customer.extid|escape} / {/if}{$customer.id|string_format:"%04d"})
			<BR>
			{if $customer.account}
				<i class="lms-ui-hint-rollover lms-ui-icon-node{if !$customer.online}off{else}on{/if}"
					data-url="?m=nodelistshort&id={$customer.id}"></i>
			{else}
				---
			{/if}
		</TD>
		<TD class="text-right nobr">
			{if ConfigHelper::checkConfig('privileges.superuser') || !ConfigHelper::checkConfig('privileges.hide_finances')}
			<span class="{if $customer.balance < 0}alert{/if}"> <IMG src="img/empty.gif" width="1" height="16" alt=""> {$customer.balance|money_format}</span>
			<BR>
			<span class="blend"><IMG src="img/empty.gif" width="1" height="16" alt="">{$customer.tariffvalue|money_format}</span>
			{/if}
		</TD>
		<TD class="lms-ui-buttons text-right nobr">
			{if ConfigHelper::checkConfig('privileges.superuser') || !ConfigHelper::checkConfig('privileges.hide_finances')}
				{if $customer.balance < 0}
					{button type="link" class="balanceok-customer" href="?m=customerbalanceok&id={$customer.id}" icon="settle" tip="Account"}
				{/if}
			{/if}
			{if ConfigHelper::checkPrivileges('customer_management', 'node_connections')}
				{if $customer.account}
					{if $customer.status == $smarty.const.CSTATUS_CONNECTED}
						{if $customer.nodeac == 1}
							{button type="link" icon="connected" tip="Disconnect All" href="?m=nodeset&ownerid={$customer.id}"}
						{else}
							{if $customer.nodeac == 2}
								{button type="link" icon="mixconnected" tip="Disconnect All" href="?m=nodeset&ownerid={$customer.id}"}
							{else}
								{button type="link" icon="disconnected" tip="Connect All" href="?m=nodeset&ownerid={$customer.id}&access=1"}
							{/if}
						{/if}
					{/if}
				{/if}
			{/if}
			{if ConfigHelper::checkPrivileges('customer_management', 'node_management')}
				{if $customer.warncount}
					{if $customer.status == $smarty.const.CSTATUS_CONNECTED}
						{if $customer.nodewarn == 1}
							{if $customer.message}{$tip=htmlspecialchars($customer.message)}{else}{$tip="Disable notices for all"}{/if}
							{button type="link" icon="warnon" tip=$tip href="?m=nodewarn&ownerid={$customer.id}"}
						{else}
							{if $customer.nodewarn == 2}
								{if $customer.message}{$tip=htmlspecialchars($customer.message)}{else}{$tip="Disable notices for all"}{/if}
								{button type="link" icon="warnmix" tip=$tip href="?m=nodewarn&ownerid={$customer.id}"}
							{else}
								{if $customer.message}{$tip=htmlspecialchars($customer.message)}{else}{$tip="Enable notices for all"}{/if}
								{button type="link" icon="warnoff" tip=$tip href="?m=nodewarn&ownerid={$customer.id}&warning=1"}
							{/if}
						{/if}
					{/if}
				{/if}
			{/if}
			{if ConfigHelper::checkPrivilege('customer_management')}
				{if $customer.deleted}
					{if ConfigHelper::checkPrivilege('permanent_customer_removal')}
						{button type="link" class="remove-button" icon="clear" tip="Remove permanently" href="?m=customerdel&id={$customer.id}&type=permanent"}
					{/if}
					{button type="link" icon="restore" tip="Recover" href="?m=customeredit&id={$customer.id}&action=recover"}
				{else}
					{if ConfigHelper::checkPrivilege('customer_removal')}
						{button type="link" class="delete-customer" icon="delete" tip="Delete" href="?m=customerdel&id={$customer.id}"}
					{/if}
					{button type="link" icon="edit" tip="Edit" href="?m=customeredit&id={$customer.id}"}
				{/if}
			{/if}
			{button type="link" icon="info" tip="Info" href="?m=customerinfo&id={$customer.id}"}
		</TD>
	</TR>
        {/block}
	{foreachelse}
        {block name="customerlist-list-empty-row"}
	<tr>
		<td colspan="{$number_of_table_columns}" class="empty-table">
			{trans("No such customers matching search criteria or list is empty.")}
		</td>
	</tr>
        {/block}
	{/foreach}
	</TBODY>
</TABLE>
</FORM>

<script>

	$(function() {
		$('.division-context').on( 'lms:division_selection', function() {
			document.choosefilter.submit();
		});

		$('.balanceok-customer').click(function () {
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to account ALL debts of customer \'$a\'?", name), this).done(function () {
				location.href = $(this).attr('href');
			});
			return false;
		});

		$('.delete-customer').click(function () {
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Are you sure, you want to remove customer '$a' from database?\n\nIf that customer have some nodes, they will be also removed and data definitely lost!", name), this).done(function () {
				location.href = $(this).attr('href');
			});
			return false;
		});

		$('.remove-button').click(function() {
			var name = $(this).closest('tr').attr('data-name');
			confirmDialog($t("Do you want to permanently remove $a customer?", name), this).done(function() {
				location.href = $(this).attr('href');
			});
			return false;
		});
	});

</script>

{/block}
