<?php
namespace Modules\Documents\BulkDownload;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{
	/**
	 * (non-PHPdoc)
	 * @see App\Mvc.View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{

		$form = \Engine\Form\General::create();
		$fs = \Engine\Form\Container\Fieldset::create($this->lang['index']['fieldSet']['label']);
		$form->addContainer($fs);
		$checkboxesDocTypes = new \Engine\Form\Input\Checkboxes($this->lang['index']['formFields']['docType'], 'docTypes', $this->values['docTypes']);
		$checkboxesDocTypes->setPossibleValues( $this->documentsList($this->values['ids'], $this->values['pivot']) );
		$checkboxesDocTypes->setStyle('renderMode', 'block');
		$fs->addElement($checkboxesDocTypes);

 		// submit button
 		$form->addElement(new \Engine\Form\Input\Hidden('', $this->submitButtonName, 1));

		return $form;
	}

	/**
	 * Get documents list
	 *
	 * @param array $entityIds
	 * @param string $pivot
	 * @return array
	 */
	private function documentsList($entityIds, $pivot)
	{

		if (in_array($pivot, ['service', 'cashing', 'cashBookDoc', 'importedDocument', 'payment'])) {
			$entityDocs = [];
			foreach ($entityIds as $idEntity) {

				// Document types checkbox
				switch ($pivot) {
					case 'service':
						$entityServiceCode = \Entity\Service::select($idEntity)->serviceCode;
						$entityDocs[] = \App\Config\IssueDocuments::getInstance()->getServiceDocsList($entityServiceCode);
						break;

					case 'cashing':
						$entityDocType = \Entity\InvoiceCashing::select($idEntity)->docTypeCode;
						$entityDocs[] = \App\Config\IssueDocuments::getInstance()->getCashingDocsList($entityDocType);
						break;

					case 'cashBookDoc':
						$entityDocType = \Entity\CashBookDocument::select($idEntity)->type;
						$entityDocs[] = \App\Config\IssueDocuments::getInstance()->getCashBookDocsList($entityDocType);
						break;

					case 'importedDocument':
						$entityDocType = \Entity\ImportedDocument::select($idEntity)->docType;
						$entityDocs[] = \App\Config\IssueDocuments::getInstance()->getImportedDocsList($entityDocType);
						break;

					case 'payment':
						$entityDocType = \Entity\SupplierInvoicePayment::select($idEntity)->docType;
						$entityDocs[] = \App\Config\IssueDocuments::getInstance()->getPaymentDocsList($entityDocType);
						break;
				}

			}

			$documentsList = call_user_func_array('array_merge', $entityDocs);
		}
		else {
			// Get pivot documents
			$documentsList = \App\Config\IssueDocuments::getInstance()->getDocsByPivot($pivot);
		}

		return $documentsList;
	}
}
