{box title="Your balance"}
{$disable_invoices = ConfigHelper::checkConfig('userpanel.disable_invoices')}
{$offline_support = ConfigHelper::checkConfig('ksef.offline_support')}

<script>

	$(function() {
		$(".table-click[data-href] td:not(.buttons)").click(function() {
			window.open($(this).closest('tr').attr("data-href"));
			reset_login_timeout();
		});

		$(".table-click[data-href] td:last-child").click(function(e) {
			if (!$(e.target).is('.custom-control-label') && !$(e.target).is('input')) {
				$(this).find(':checkbox').trigger('click');
			}
		});
	});

</script>

<style>
	.table-click[data-href] td:not(.buttons):hover {
		cursor: pointer;
	}
	#print-all {
		margin-bottom: 0.3em;
	}

	.check-invoice {
		margin-left: 0.3em;
	}

	.custom-control-label.valign-fix {
		vertical-align: top;
	}

	#checkall-checkbox {
		margin-bottom: 0.4em;
	}

	.text-muted .text-danger {
		color: #ff8888 !important;
	}

</style>
<div class="row">
    <div class="col">
        <form name="invoices" action="?m=finances&amp;f=invoice" method="POST" target="_blank">
			<div class="table-responsive">
				<table class="table table-sm table-hover table-bordered table-striped table-condensed">
					<thead class="bg-secondary lms-userpanel-table-header">
							<th>{trans("Date")}</th>
							<th>{trans("Commitment")}</th>
							<th>{trans("Payment")}</th>
							<th>{trans("Balance")}</th>
							<th>{trans("Transfer title")} / {trans("Invoice title")}</th>
							{if !$disable_invoices}
							<th></th>
							<th class="text-center">{trans("Check")}</th>
							{/if}
					</thead>
					{$ldate = ConfigHelper::getConfig('userpanel.show_last_years', 5)}
					{if $ldate}
						{$ldate = ($smarty.now - $ldate * 366 * 86400)}
					{else}
						{$ldate = 0}
					{/if}
					<tbody>
					{section name=balancelist loop=$balancelist.list step=-1}
						{$item=$balancelist.list[balancelist]}
						{if $item.time > $ldate}
						<tr class="{strip}
							table-click
							{if ($item.doctype == $smarty.const.DOC_INVOICE || $item.doctype == $smarty.const.DOC_CNOTE)
								&& !empty($ksef_enabled)
								&& !empty($item.ksefsubmission)
								&& $item.ksefstatus != 200
								&& (!$offline_support || !isset($item.ksefstatus) || !empty($item.ksefstatus))}
								{/strip} text-muted{strip}
							{/if}
							{/strip}"
							{if $item.doctype == $smarty.const.DOC_INVOICE || $item.doctype == $smarty.const.DOC_CNOTE}
								{if empty($ksef_enabled)
									|| empty($item.ksefsubmission)
									|| $item.ksefstatus == 200
									|| $offline_support && isset($item.ksefstatus) && $item.ksefstatus == 0}
									data-href="?m=finances&f=invoice&id={$item.docid}"
								{/if}
							{/if}
							>
							<td class="text-center text-nowrap">{$item.date|date_format:"Y-m-d"}</td>
							{if $item.value > 0 && !empty($item.type)}
								<td></td>
								<td class="text-center text-nowrap table-success text-success">
									{strip}
										{if $item.value > 0 && $item.type != 4}+{/if}
										{moneyf($item.value, $item.currency)}
									{/strip}
								</td>
							{else}
								<td class="text-center text-nowrap {if $item.value < 0}table-danger text-danger{else}table-success text-success{/if}">
									{strip}
										{if $item.type == 4}-{elseif $item.value > 0}+{/if}
										{moneyf($item.value, $item.currency)}
									{/strip}
								</td>
								<td></td>
							{/if}
							<td class="text-right text-nowrap">
								{if $item.after < 0}
									<strong>{moneyf($item.after)}</strong>
								{else}
									{moneyf($item.after)}
								{/if}
							</td>
							<td>{$item.comment}</td>
							{if !$disable_invoices}
							<td class="text-center buttons">
									{foreach $item.customlinks as $link}
										<a {if isset($link.url)}href="{$link.url}"{/if} target="_blank" {if isset($link.onclick)}onclick="{$link.onclick}"{/if}>{if isset($link.icon)}{img src="`$link.icon`" alt="`$link.label`" text=$link.tip}{else}{$link.label}{/if}</a>
									{if isset($link.extra)}{$link.extra}{/if}
								{/foreach}
								{if $item.doctype == $smarty.const.DOC_INVOICE || $item.doctype == $smarty.const.DOC_CNOTE}
									{if empty($ksef_enabled)
										|| empty($item.ksefsubmission)
										|| $item.ksefstatus == 200
										|| $offline_support && isset($item.ksefstatus) && $item.ksefstatus == 0}
										{button class="btn btn-primary btn-sm" onclick="reset_login_timeout(); location.href='?m=finances&f=invoice&id={$item.docid}'" external=true icon="print" label="Print"}
									{/if}
								{else}
									&nbsp;
								{/if}
							</td>
							<td class="text-center buttons">
								{if $item.doctype == $smarty.const.DOC_INVOICE || $item.doctype == $smarty.const.DOC_CNOTE}
									{if empty($ksef_enabled)
										|| empty($item.ksefsubmission)
										|| $item.ksefstatus == 200
										|| $offline_support && isset($item.ksefstatus) && $item.ksefstatus == 0}
										<div class="custom-control custom-checkbox custom-control-inline check-invoice">
											<input class="custom-control-input" type="checkbox" id="inv-{$item.docid}" name="inv[{$item.docid}]" value="1" />
											<label class="custom-control-label valign-fix" for="inv-{$item.docid}"></label>
										</div>
									{/if}
								{/if}
							</td>
							{/if}
						</tr>
						{/if}
					{sectionelse}
						<tr>
							<td class="text-center" colspan="{if $disabled_invoices}5{else}7{/if}">
								{trans("No such transactions on your account.")}
							</td>
						</tr>
					{/section}
					</tbody>
				{if !$disable_invoices && !empty($balancelist.list)}
					<tfoot>
						<tr>
							<td colspan="5">
								{if $aggregate_documents}
									{button type="submit" class="btn btn-primary" formaction="?m=finances&aggregate_documents=0#userbalancebox" icon="document" label="Don't aggregate documents"}
								{else}
									{button type="submit" class="btn btn-primary" formaction="?m=finances&aggregate_documents=1#userbalancebox" icon="document" label="Aggregate documents"}
								{/if}
							</td>
							<td colspan="2" class="text-center">
								<div class="custom-control custom-checkbox" id="checkall-checkbox">
									<input class="custom-control-input" type="checkbox" name="allbox" id="allbox" onchange="CheckAll('invoices', this)" value="1" />
									<label class="custom-control-label" for="allbox">{trans("Check all")}</label>
								</div>
								{button class="btn btn-primary btn-sm" id="print-all" onclick="document.invoices.submit()" icon="print" label="Print selected"}
							</td>
						</tr>
					</tfoot>
				{/if}
            </table>
		</div>
	</form>
</div>
</div>
{/box}
