{extends file=$smarty.const.SMARTY_LAYOUT_MAIN_POPUP}

{block name="template-head" append}
	{$JS->loadGroup("remoteLoad")}
	{$CSS->loadGroup("remoteLoad")}

	{$CSS->load("{asset path="_css/index.css"}")}
{/block}

{block name="content"}
	<div id="container" class="container">
		<div class="generatedDocuments">
			<fieldset>
				<legend>
					{$lang['index']['formFields']['documentsList']['fieldsetLegend']}
				</legend>

				{********************************* DOCUMENTS LIST **************************************************}
				<div class="documentsList">
					<label>{$lang['index']['formFields']['documentsList']['selectLabel']}</label>
					<select id="idDocument" class="idDocument">
						<option value="">{$lang['index']['formFields']['documentsList']['defaultValue']}</option>

						{foreach $documentsList as $idDocument => $documentDetails}
							<option value="{$idDocument}" {if $idDocument == $idSelectedDocument} selected="1"{/if}>
								{\App\Config\IssueDocuments::getInstance()->getDocTypeName($documentDetails['docTypeCode'])} {if $documentDetails['docNumber']}{$documentDetails['docNumber']}{/if}/{$documentDetails['docDate']|dateTimeFormat}
								{if $documentDetails['generation'] == 'manual'}({$lang['index']['formFields']['documentsList']['uploadedFlag']}-{$documentDetails['docFileName']}){/if}
								{if $documentDetails['preview']}({$lang['index']['formFields']['documentsList']['previewFlag']}){/if}
								{if $documentDetails['voided']}({$lang['index']['formFields']['documentsList']['cancelledFlag']}){/if}
								{if !$documentDetails['docFileName']}({$lang['index']['formFields']['documentsList']['deletedFile']}){/if}
								{if !$documentDetails['preview'] and !$documentDetails['voided'] and $documentDetails['indicateCopy'] and $documentDetails['genCount'] > 1}[{$lang['index']['formFields']['documentsList']['copyFlag']}]{/if}
							</option>
						{/foreach}
					</select>
				</div>


				{********************************* DOCUMENTS BUTTONS ************************************************}
				<div class="documentsButtons hidden">

					{***** DETAILS BUTTON *****}
					<button type="button" class="detailsButton">
						{$lang['index']['formFields']['documentsList']['buttons']['details']}
					</button>

					{***** VIEW BUTTON *****}
					<button type="button" class="viewButton">
						{$lang['index']['formFields']['documentsList']['buttons']['view']}
					</button>

					{******* DOWNLOAD BUTTON ******}
					<button type="button" class="downloadButton">
						{$lang['index']['formFields']['documentsList']['buttons']['download']}
					</button>

					{****** PRINT BUTTON ********}
					<button type="button" class="printButton">
						{$lang['index']['formFields']['documentsList']['buttons']['print']}
					</button>

					{****** EMAIL BUTTON ********}
					<button type="button" class="emailButton">
						{$lang['index']['formFields']['documentsList']['buttons']['email']}
					</button>

				</div>
			</fieldset>
		</div>

		<div class="generateDocuments">
			<fieldset>
				<legend>
					{$lang['index']['formFields']['generateDocuments']['fieldsetLegend']}
				</legend>

				<div class="generateDocumentsButtons">

					{**************************** GENERATE NEW DOCUMENT **********************}
					<div class="generateNewDocument">
						<button type="button" class="generateNewDocument">
							{$lang['index']['formFields']['generateDocuments']['buttons']['generate']}
						</button>

						<span class="buttonExtraInfo">
							-
							{$lang['index']['formFields']['generateDocuments']['buttons']['generateTooltip']}
						</span>

					</div>

					{**************************** UPLOAD DOCUMENT **********************}
					<div class="uploadDocument">
						<button type="button" class="uploadDocument">
							{$lang['index']['formFields']['generateDocuments']['buttons']['upload']}
						</button>
						<span class="buttonExtraInfo">
							-
							{$lang['index']['formFields']['generateDocuments']['buttons']['uploadTooltip']}
						</span>
					</div>
				</div>
			</fieldset>
		</div>
	</div>
{/block}

{block name="js" append}
	<script type="text/javascript">

		MVC.pageRefresh = function (module, action, windowName, execOutput) {
			if (module == "Documents/Generate/Email/SendEmail") {
				return null;
			}

			MVC.reloadPage();
		}


		MVC.generatedDocuments = {};

		MVC.generatedDocuments.documentsList = {$documentsList|json_encode};

		MVC.generatedDocuments.getSelectedDocumentId = function(element)
		{
			return $F('idDocument');
		}

		/**
		 	Togggle documents button container
		*/
		MVC.generatedDocuments.toggleDocumentsButtons = function(selectedDocumentId)
		{
			if (selectedDocumentId) {
				if (MVC.generatedDocuments.documentsButtonContainer.hasClassName('hidden')) {
					MVC.generatedDocuments.documentsButtonContainer.removeClassName('hidden').addClassName('visible');
				}
			}
			else {
				if (MVC.generatedDocuments.documentsButtonContainer.hasClassName('visible')) {
					MVC.generatedDocuments.documentsButtonContainer.removeClassName('visible').addClassName('hidden');
				}
			}
		}

		MVC.generatedDocuments.checkDocumentAvailableOptions = function(selectedDocumentId)
		{
			// document is only allowd to be printed (once)
			var documentProperties = MVC.generatedDocuments.documentsList[selectedDocumentId];

			if (documentProperties) {
				if (documentProperties.printOriginalOnce && documentProperties.isOriginal) {
					// do not allow to view document
					$('container').down('div.generatedDocuments div.documentsButtons button.viewButton').disabled = true;

					// do not allow to download document
					$('container').down('div.generatedDocuments div.documentsButtons button.downloadButton').disabled = true;

					// do not allow to send email
					$('container').down('div.generatedDocuments div.documentsButtons button.emailButton').disabled = true;

					// do not allow document print (except for the first time)
					if (documentProperties.printed) {
						$('container').down('div.generatedDocuments div.documentsButtons button.printButton').disabled = true;
					}
					else {
						$('container').down('div.generatedDocuments div.documentsButtons button.printButton').disabled = false;
					}
				}
				if (!documentProperties.docFileName) {
					// do not allow to view document
					$('container').down('div.generatedDocuments div.documentsButtons button.viewButton').disabled = true;

					// do not allow to download document
					$('container').down('div.generatedDocuments div.documentsButtons button.downloadButton').disabled = true;

					// do not allow to send email
					$('container').down('div.generatedDocuments div.documentsButtons button.emailButton').disabled = true;

					// do not allow to print doc
					$('container').down('div.generatedDocuments div.documentsButtons button.printButton').disabled = true;
				}
				// document has no restrictions
				else {
					// allow to view document
					$('container').down('div.generatedDocuments div.documentsButtons button.viewButton').disabled = false;

					// allow to download document
					$('container').down('div.generatedDocuments div.documentsButtons button.downloadButton').disabled = false;

					// allow to send email
					$('container').down('div.generatedDocuments div.documentsButtons button.emailButton').disabled = false;

					// allow to print document
					$('container').down('div.generatedDocuments div.documentsButtons button.printButton').disabled = false;
				}
			}
		}

		MVC.scheduleOnLoad(function() {

			// populate documents button container
			MVC.generatedDocuments.documentsButtonContainer = $('container').down('div.generatedDocuments div.documentsButtons');

			// display the buttons if on element selected
			var selectedDocumentId = MVC.generatedDocuments.getSelectedDocumentId({});
			if (selectedDocumentId) {
				// show/hide buttons
				MVC.generatedDocuments.toggleDocumentsButtons(selectedDocumentId);

				// check (enable/disable) the options for current document
				MVC.generatedDocuments.checkDocumentAvailableOptions(selectedDocumentId);
			}

			// display option buttons when change the document
			$('container').down('div.generatedDocuments select#idDocument').observe('change', function(event, element) {
				// get selected document id
				var selectedDocumentId = MVC.generatedDocuments.getSelectedDocumentId(element);

				// show/hide buttons
				MVC.generatedDocuments.toggleDocumentsButtons(selectedDocumentId);

				// check (enable/disable) the options for current document
				MVC.generatedDocuments.checkDocumentAvailableOptions(selectedDocumentId);

				// remote load when change the document
				RemoteLoad.hide();
			});


			///////////////////////////////// DETAILS BUTTON ///////////////////////////////////////////////////////////////////////////////
			$('container').down('div.generatedDocuments div.documentsButtons').on('click', 'button.detailsButton', function(event, element) {
				RemoteLoad.load(this, MVC.appUrl('RemoteData/InfoBox', 'document', { 'id' : MVC.generatedDocuments.getSelectedDocumentId(this) }), null);
				RemoteLoad.attach(element);
			});

			///////////////////////////////// VIEW BUTTON //////////////////////////////////////////////////////////////////////////////////
			$('container').down('div.generatedDocuments div.documentsButtons').on('click', 'button.viewButton', function(event, element) {
				Popup2.create().setId('Documents/Generate/OpenDocument').setUrl(MVC.appUrl('Documents/Generate/OpenDocument', 'view', { 'idDocument': MVC.generatedDocuments.getSelectedDocumentId(this) })).setDimensions(800, 500).show();
			});

			///////////////////////////////// DOWNLOAD BUTTON //////////////////////////////////////////////////////////////////////////////////
			$('container').down('div.generatedDocuments div.documentsButtons').on('click', 'button.downloadButton', function(event, element) {
				Popup2.create().setId('Documents/Generate/OpenDocument').setUrl(MVC.appUrl('Documents/Generate/OpenDocument', 'download', { 'idDocument': MVC.generatedDocuments.getSelectedDocumentId(this) })).setDimensions(800, 500).show();
			});

			///////////////////////////////// PRINT BUTTON //////////////////////////////////////////////////////////////////////////////////
			$('container').down('div.generatedDocuments div.documentsButtons').on('click', 'button.printButton', function(event, element) {
				Popup2.create().setId('Documents/Generate/OpenDocument').setUrl(MVC.appUrl('Documents/Generate/OpenDocument', 'print', { 'idDocument': MVC.generatedDocuments.getSelectedDocumentId(this) })).setDimensions(800, 500).show();
			});

			///////////////////////////////// EMAIL BUTTON //////////////////////////////////////////////////////////////////////////////////
			$('container').down('div.generatedDocuments div.documentsButtons').on('click', 'button.emailButton', function(event, element) {
				var idDocument = MVC.generatedDocuments.getSelectedDocumentId(this);
				if (MVC.generatedDocuments.documentsList[idDocument]["emailSent"]) {
					Popup2.create().setId('Documents/Generate/Email/EmailList').setUrl(MVC.appUrl('Documents/Generate/Email/EmailList', 'index', { 'idDocument': MVC.generatedDocuments.getSelectedDocumentId(this) })).setDimensions(800, 500).show();
				}
				else {
					Popup2.create().setId('Documents/Generate/Email/EmailList').setUrl(MVC.appUrl('Documents/Generate/Email/SendEmail', 'index', { 'idDocument': MVC.generatedDocuments.getSelectedDocumentId(this) })).setDimensions(800, 500).show();
				}
			});

			//////////////////////////////// GENERATE NEW DOCUMENT BUTTON //////////////////////////////////////////////////////////////////
			$('container').down('div.generateDocuments div.generateDocumentsButtons').on('click', 'button.generateNewDocument', function(event, element) {
				window.location = MVC.appUrl('Documents/Generate/GenerateDocument', 'index', { 'pivot': '{$pivot}', 'idPivot': '{$idPivot}' } );
			});

			//////////////////////////////// UPLOAD NEW DOCUMENT BUTTON //////////////////////////////////////////////////////////////////
			$('container').down('div.generateDocuments div.generateDocumentsButtons').on('click', 'button.uploadDocument', function(event, element) {
				window.location = MVC.appUrl('Documents/Generate/Upload', 'index', { 'pivot': '{$pivot}', 'idPivot': '{$idPivot}' } );
			});

			///////////////////////////////// REFRESH PARENT PAGES ////////////////////////////////////////////////////////////////////////
			{if $parentPageRefresh}
				if (window.parent.opener && window.parent.opener.MVC) {
					window.parent.opener.MVC.reloadPage();
				}
			{/if}
		});
	</script>
{/block}