<?php
namespace Modules\Admin\Config\Services\Advanced\Invoice;

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();

		// VAT PERCENT FIELDSET ///////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['index']['fieldsets']['percent']);
		$form->addContainer($fieldset);

		$label=new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['labels']['percent']);
		$label->setStyle("noLabel", true);
		$fieldset->addElement($label);

		$offerVat=\App\Config\Services_Calculations_OfferVat::getAdmin()->get($this->values['calculationCode']);
		$offerVat = \App\Config\Services_Calculations_OfferVat::getAdmin()->getCalculationDetails($this->values['serviceCode'], $this->values['location'], $this->values['calculation']);
		foreach (['price', 'commission', 'discount', 'serviceFee', 'tax', 'penalty', 'advance'] as $invoiceRow) {
			$text = new \Engine\Form\Input\Text($this->lang['index']['formFields'][$invoiceRow], $invoiceRow.'[vatPercent]', $this->values[$invoiceRow]['vatPercent']);
			$text->setMandatory();
			$text->setStyle('width', 50);

			// vat percent from offer
			if ($invoiceRow == 'penalty' or $invoiceRow == 'advance') {
				$offerVatPrice='';
			}
			elseif ($invoiceRow=='price') {
				$offerVatPrice=sprintf( $this->lang['index']['details']['servicePriceValueDescr'], $offerVat['supplier']['percent'].'%', $offerVat['commission']['percent'].'%' );
			}
			else {
				$offerVatPrice=sprintf($this->lang['index']['details']['offerValueDescr'], $offerVat[$invoiceRow]['percent'].'%');
			}

			$text->setCustomHTML('% '. $offerVatPrice);

			// validations
			$text->setValidatorParam('type', 'float')
				->setValidatorParam('maxValue', 100)
				->setValidatorParam('allowedValues', ['0']);

			if ($invoiceRow=='price' or $invoiceRow=='tax') {
				$text->setValidatorParam('allowedValues', ['0','-1']);
			}

			// tooltip
			if ($invoiceRow=='tax') {
				$text->setTooltip($this->lang['index']['tooltips']['taxManualVat']);
			}
			if ($invoiceRow=='price') {
				$text->setTooltip($this->lang['index']['tooltips']['hiddenRow']);
			}

			$fieldset->addElement($text);
		}


		// VAT FIELDSET ///////////////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['index']['fieldsets']['vat']);
		$form->addContainer($fieldset);

		$label=new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['labels']['vat']);
		$label->setStyle("noLabel", true);
		$fieldset->addElement($label);

		foreach (['price', 'commission', 'discount', 'serviceFee', 'tax', 'penalty', 'advance'] as $invoiceRow) {
			$text = new \Engine\Form\Input\Text($this->lang['index']['formFields'][$invoiceRow], $invoiceRow.'[vatText]', $this->values[$invoiceRow]['vatText'] );
			$text->setValidatorParam('maxLength', 50);
			$fieldset->addElement($text);
		}


		// REMARKS FIELDSET ///////////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::Create($this->lang['index']['fieldsets']['remarks']);
		$form->addContainer($fieldset);

		$label=new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['labels']['remarks']);
		$label->setStyle("noLabel", true);
		$fieldset->addElement($label);

		foreach (['price', 'commission', 'discount', 'serviceFee', 'tax', 'penalty', 'advance'] as $invoiceRow) {
			$text=new \Engine\Form\Input\Text($this->lang['index']['formFields'][$invoiceRow], $invoiceRow.'[vatRemark]', $this->values[$invoiceRow]['vatRemark'] );
			$text->setValidatorParam('maxLength', 300);
			$fieldset->addElement($text);
		}


		// HIDDEN ROWS FIELDSET ///////////////////////////////////////////////////////////////////
		$fieldset = \Engine\Form\Container\Fieldset::create($this->lang['index']['fieldsets']['hiddenRows']);
		$form->addContainer($fieldset);

		$label=new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['labels']['hiddenRows']);
		$label->setStyle("noLabel", true);
		$fieldset->addElement($label);

		foreach (['discount', 'serviceFee', 'tax', 'advance'] as $invoiceRow) {
			$select = new \Engine\Form\Input\Select($this->lang['index']['formFields'][$invoiceRow], $invoiceRow.'[hidden]', $this->values[$invoiceRow]['hidden']);
			$select->setPossibleValues([
				'0' => $this->lang['index']['titles']['defaultVisible'],
				'1' => $this->lang['index']['titles']['defaultHidden'],
			]);
			$fieldset->addElement($select);

		}


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

		return $form;
	}

}