@extends(request()->input('popupmode') ? 'layouts.empty' : 'layouts.template')
<?php
$routeName = request()->route()->getName();
?>
@section('content')
<div class="container-fluid h-100 popup-content-container" id="invoice-center">
	<div class="main-row h-100 {{ !request()->input('popupmode') ? '' : 'px-0' }}">
		@if(!request()->input('popupmode'))
		<nav id="sidebarMenu" class="d-block bg-light sidebar collapse px-0">
			<div class="sidebar-sticky pt-3">
				<ul class="nav flex-column mcp-theme-1">
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.companies') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.companies') }}">Companies</a>
					</li>
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.customers') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.customers') }}">Customers</a>
					</li>
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.giftCards') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.giftCards') }}">Vouchers</a>
					</li>
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.invoices') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.invoices') }}">Invoices</a>
					</li>
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.customerTransactions') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.customerTransactions') }}">Customer Transactions</a>
					</li>
					<li class="nav-item">
						<a class="nav-link {{ strpos($routeName, 'invoice-center.invoiceTransactions') === 0 ? 'active' : '' }}" href="{{ route('invoice-center.invoiceTransactions') }}">Invoice Transactions</a>
					</li>
				</ul>
			</div>
		</nav>
		@endif
		<main role="main" class="w-100 mcp-theme-1">
			<div class="pt-3 hide-inside-popup"></div>
			@yield('inner-content')
		</main>
	</div>
</div>
<script>
	(function () {
		function init() {
			initStagSuggest();
			let parentSegment = $('body');
			parentSegment.find('input[stag-suggest][target-key][target-field]')
					.off('stag-suggest-selected')
					.on('stag-suggest-selected', (_e, _input, _data) => {
						_input = $(_input);
						_input.closest('form').find('input[name="' + _input.attr('target-field') + '"]').val(_data[_input.attr('target-key')]);
					});
			parentSegment.find('.copy-target')
					.off('click.copy-target')
					.on('click.copy-target', function() {
						copyTextToClipboard($(this).attr('data-target'));
						return false;
					});

			parentSegment.find('.generate-and-visit-ic-portal')
					.off('click.generate-and-visit-ic-portal')
					.on('click.generate-and-visit-ic-portal', function() {
						$.post('/api/session/proLogInAsCustomer', {
							customerUid: $(this).attr('data-uid')
						}, _data => {
							if(!hasResponseError(_data)) {
								window.location.href = '/ic/home/' + _data.data;
								return false;
							}
						});
						return false;
					});

			parentSegment.find('.generate-and-copy-ic-portal-url')
					.off('click.generate-and-copy-ic-portal-url')
					.on('click.generate-and-copy-ic-portal-url', function() {
						$.post('/api/session/proLogInAsCustomer', {
							customerUid: $(this).attr('data-uid')
						}, _data => {
							if(!hasResponseError(_data)) {
								copyTextToClipboard('{{config('app.url')}}/ic/home/' + _data.data);
								return false;
							}
						});
						return false;
					});

			parentSegment.find('.generate-and-visit-ic-pay')
					.off('click.generate-and-visit-ic-pay')
					.on('click.generate-and-visit-ic-pay', function() {
						$.post('/api/session/proLogInAsCustomer', {
							customerUid: $(this).attr('data-uid')
						}, _data => {
							if(!hasResponseError(_data)) {
								window.location.href = '/ic/pay/' + $(this).attr('data-invoice-uid') + '/' + _data.data;
								return false;
							}
						});
						return false;
					});

			parentSegment.find('.generate-and-copy-ic-pay-url')
					.off('click.generate-and-copy-ic-pay-url')
					.on('click.generate-and-copy-ic-pay-url', function() {
						$.post('/api/session/proLogInAsCustomer', {
							customerUid: $(this).attr('data-uid')
						}, _data => {
							if(!hasResponseError(_data)) {
								copyTextToClipboard('{{config('app.url')}}/ic/pay/' + $(this).attr('data-invoice-uid') + '/' + _data.data);
								return false;
							}
						});
						return false;
					});
		}
		addMCInitializer('invoice-center', init, '#invoice-center')
	}).call(window);
</script>
@endsection
