{extends file='Modules/Invoices/SupplierInvoices/Management/View/_tpl/insert.tpl'}

{block name="template-head" append}
	{$CSS->load("{asset path="_css/update.css"}")}
{/block}

{block name="submitButton"}
	<div class="pageControls">
		{if !$closePopup}
			{********************** PAGINATION *******************}
			{$pagesData=[]}
			{$pagesData['currentPage'] = 			$currentPage}
			{$pagesData['countResults'] =			$countResults}
			{$pagesData['resultsPerPage'] =			$resultsPerPage}
			{$pagesData['resultsPerPageSelector'] =	Array($resultsPerPage)}
			{$pagesData['ajax'] = 'callback'}
			{$pagesData['ajaxCallback'] = 'MVC.supplierInvoice.changePage'}
			{$pagesData['short'] = true}

			{pageControls_v2 pagesData=$pagesData method="get"}

			{******************* SUBMIT BUTTON ********************}
			<button
				type="button"
				class="button-popupSubmit"
				onclick="$('form').submit()"
			>{$_LANG['save']}</button>
		{/if}
	</div>
{/block}

{block name="js" append}
	<script type="text/javascript">
		{if !$closePopup}
			MVC.supplierInvoice.changePage = function(params) {
				if (MVC.supplierInvoice.formChanged) {
					var alert = new _alertConfirm();
					alert.setTitle('{$lang['messages']['confirmPageChange_title']}')
						.setContent('{$lang['messages']['confirmPageChange_description']}')
						.onAccept(MVC.supplierInvoice._changePage.bind(null, params))
						.show();
					
					return;
				}
				
				MVC.supplierInvoice._changePage(params);
			}
			
			MVC.supplierInvoice._changePage = function(params) {
				$('pageChanged').value=1;
				$('page').value = params.currentPage;
				$('save').value = 0;
				form.submit();
				$('save').value = 1;
			}
			
			MVC.supplierInvoice.formChanged = false;
			
			MVC.supplierInvoice.onFormChange = function(params) {
				MVC.supplierInvoice.formChanged = true;
			}
			
			MVC.scheduleOnLoad(function() {
				Event.on(document, 'change', 'form.form', function() {
					MVC.supplierInvoice.onFormChange();
				});
				
				Event.on(document, 'inputElement:change', 'form.form', function() {
					MVC.supplierInvoice.onFormChange();
				});
			});
		{/if}
	</script>
{/block}