<?php

namespace Modules\Admin\Config\ClientInvoices\General;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

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

		$label = new \Engine\Form\Input\Label();
		$label->setDetails($this->lang['index']['description']);
		$form->addElement($label);

		// add currency fieldset
		$fieldsetCurrencies = \Engine\Form\Container\Fieldset::create($this->lang['currency']['legend']);
		// add form elements for invoice due dates
		$this->currencyForm($fieldsetCurrencies);
		// add fieldset for invoice due dates
		$form->addContainer($fieldsetCurrencies);

		// add due dates fieldset
		$fieldsetDueDates = \Engine\Form\Container\Fieldset::create($this->lang['dueDates']['legends']['dueDates']);
		// add form elements for invoice due dates
		$this->dueDatesForm($fieldsetDueDates);
		// add fieldset for invoice due dates
		$form->addContainer($fieldsetDueDates);

		// add invoice voiding fieldset
		$fieldsetInvoiceVoiding = \Engine\Form\Container\Fieldset::create($this->lang['invoiceVoiding']['legends']['voidInvoice']);
		// add form elements for invoice voiding
		$this->invoiceVoidingForm($fieldsetInvoiceVoiding);
		// add fieldset for invoice voiding
		$form->addContainer($fieldsetInvoiceVoiding);

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

		return $form;
	}


	/**
	 * Populating the form with the currency fields
	 */
	private function currencyForm(\Engine\Form\Container\Fieldset $fieldset)
	{
		// create input for  subagent categories
		$inputInvDefCurrency = new \App\Form\Input\Select_Currency($this->lang['currency']['formRow']['defaultCurrency'], 'defaultCurrency', $this->values['defaultCurrency']);
		$inputInvDefCurrency->showDefaultOption();
		$inputInvDefCurrency->setTooltip($this->lang['currency']['formRow']['defaultCurrencyTooltip']);
		$fieldset->addElement($inputInvDefCurrency);
	}


	/**
	 * Populating the form with the due dates fields
	 */
	private function dueDatesForm(\Engine\Form\Container\Fieldset $fieldset)
	{
		// details label for due dates
		$fld = new \Engine\Form\Input\Label();
		$fld->setDetails($this->lang['dueDates']['description']);
		$fld->setStyle("noLabel", true);
		$fieldset->addElement($fld);

		// create input for corporate categories
		$inputCorporate	= new \Engine\Form\Input\Text($this->lang['dueDates']['formRow']['corporate'], 'dueDates[corporate]', $this->values['dueDates']['corporate']);
		$inputCorporate->setStyle('width', 40);
		$inputCorporate->setCustomHTML($this->lang['dueDates']['texts']['days']);
		$inputCorporate->setValidatorParam('type', 'int')->setValidatorParam('minValue', 0)->setValidatorParam('maxValue', 366);
		$fieldset->addElement($inputCorporate);

		// create input for  subagent categories
		$inputSubagent = new \Engine\Form\Input\Text($this->lang['dueDates']['formRow']['subagent'], 'dueDates[subagent]', $this->values['dueDates']['subagent']);
		$inputSubagent->setStyle('width', 40);
		$inputSubagent->setCustomHTML($this->lang['dueDates']['texts']['days']);
		$inputSubagent->setValidatorParam('type', 'int')->setValidatorParam('minValue', 0)->setValidatorParam('maxValue', 366);
		$fieldset->addElement($inputSubagent);

		// create input for  customer categories
		$inputCustomer = new \Engine\Form\Input\Text($this->lang['dueDates']['formRow']['customer'], 'dueDates[customer]', $this->values['dueDates']['customer']);
		$inputCustomer->setStyle('width', 40);
		$inputCustomer->setCustomHTML($this->lang['dueDates']['texts']['days']);
		$inputCustomer->setValidatorParam('type', 'int')->setValidatorParam('minValue', 0)->setValidatorParam('maxValue', 366);
		$fieldset->addElement($inputCustomer);
	}

	/**
	 * Populating the form with the Invoice voiding fields
	 */
	private function invoiceVoidingForm(\Engine\Form\Container\Fieldset $fieldset)
	{
		// details label for void issued
		$fld = new \Engine\Form\Input\Label();
		$fld->setDetails($this->lang['invoiceVoiding']['Description']);
		$fld->setStyle("noLabel", true);
		$fieldset->addElement($fld);

		$_LANG = \App\Loader_Lang::getInstance()->getAll();
		// create input AllowVoidIssue
		$inputAllowVoidIssue = new \Engine\Form\Input\Radio($this->lang['invoiceVoiding']['formFields']['allowVoidIssue'], 'voidIssuedInvoices[allow]', $this->values['voidIssuedInvoices']['allow']);
		$inputAllowVoidIssue->setPossibleValues([true => $_LANG['yes'], false => $_LANG['no']]);
		$inputAllowVoidIssue->setTooltip($this->lang['invoiceVoiding']['tooltip']['allow']);
		$fieldset->addElement($inputAllowVoidIssue);

		// create input for voiding invoice according to the current month
		$inputChangeMonth = new \Engine\Form\Input\Radio($this->lang['invoiceVoiding']['formFields']['changeMonth'], 'voidIssuedInvoices[sameMonth]', $this->values['voidIssuedInvoices']['sameMonth']);
		$inputChangeMonth->setPossibleValues([
			true => $this->lang['invoiceVoiding']['formFields']['sameMonth'],
			false => $this->lang['invoiceVoiding']['formFields']['otherMonth']]);
		$inputChangeMonth->setTooltip($this->lang['invoiceVoiding']['tooltip']['changeMonth']);
		$fieldset->addElement($inputChangeMonth);

		// create input for fixed days
		$inputFixedDays = new \Engine\Form\Input\Text($this->lang['invoiceVoiding']['formFields']['fixedDays'], 'voidIssuedInvoices[fixedDays]', $this->values['voidIssuedInvoices']['fixedDays']);
		$inputFixedDays->setTooltip($this->lang['invoiceVoiding']['formFields']['fixedDaysExplanations']);
		$inputFixedDays->setValidatorParam('type', 'int')->setValidatorParam('minValue', 0);
		$inputFixedDays->setTooltip($this->lang['invoiceVoiding']['tooltip']['fixedDays']);
		$fieldset->addElement($inputFixedDays);

		$inputAllowVoidIssue->setControlledElements([true=> [$inputChangeMonth,$inputFixedDays]], 'hide');
	}
}