<?php
namespace Modules\Invoices\SupplierInvoices\Compensate;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

	/**
	 *
	 * @see \App\Mvc\View_Utils_FormAbstract::getForm()
	 */
	public function getForm()
	{
		$form = \Engine\Form\General::create();

		$paymentDocDetails = \Engine\Form\Container\Fieldset::create();
		$form->addContainer($paymentDocDetails);

		// payment type
		$paymentDocType = new \App\Form\Input\Select_PaymentDocType($this->lang['index']['form']['paymentDocType'], 'paymentDocType', $this->values['paymentDocType']);
		$paymentDocType->showDefaultOption();
		$paymentDocType->setMandatory();
		$paymentDocDetails->addElement($paymentDocType);

		if ($this->values['ids']) {
			$possibleValues = [];
			foreach ($this->values['ids'] as $idInvoice) {
				$invoiceEnt = \Entity\SupplierInvoice::select($idInvoice);
				$invoiceDate = \Engine\DateTime::createFromIsoDate($invoiceEnt->invoiceDate)->formatDateStd();
				$valueToBePayed = \App\Config\CurrencyDecimals::roundPrice($invoiceEnt->totalValue - $invoiceEnt->payedValue, $invoiceEnt->currency);
				$invoiceNumber = $invoiceEnt->invoiceNumber?:'-';
				$invoiceDetails =  $invoiceNumber.'/'.$invoiceDate.': '.$valueToBePayed.'/'.$invoiceEnt->totalValue.' '.$invoiceEnt->currency;
				if ($this->values['compensatedValues'][$idInvoice]) {
				    $invoiceDetails .= '<b> ['.$this->lang['index']['form']['compensate'].' '.$this->values['compensatedValues'][$idInvoice].' '.$invoiceEnt->currency.']</b>';
				}
				$possibleValues[$idInvoice] = '<span class="remoteLoadLink" onclick="RemoteLoad.loadEntity(this, \'supplierInvoice\', '.$idInvoice.')">#'.$idInvoice.'</span>: '.$invoiceDetails;
			}

			$invoicesList = new \Engine\Form\Input\Checkboxes($this->lang['index']['form']['invoicesList'], 'invoicesList', $this->values['invoicesList']);
			$invoicesList->setPossibleValues($possibleValues);
			$invoicesList->setStyle('renderMode', 'block');
			$paymentDocDetails->addElement($invoicesList);
		}

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

		return $form;
	}
}