<?php

namespace Modules\Orders\OrderForm\ServiceExtraInfo;

class View_Utils_FormAdmin extends \App\Mvc\View_Utils_FormAbstract
{

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

		// determine service code
		$serviceCode = null;
		$orderFormHandler = \Modules\Orders\OrderForm\Model_Handler::getInstance();
		$offersDetails = $orderFormHandler->getServiceOffers($this->values['id']);
		// determine service code
		if ($offersDetails['list']) {
			$offerEnt = $offersDetails['list'][$offersDetails['chosenOfferIndex']];
			$serviceCode = $offerEnt->getField('serviceCode');
		}

		// get service details
		$serviceEnt = $orderFormHandler->getServiceByIndex($this->values['id']);


		$CFG = \Engine\Loader_Cfg::getInstance()->getGlobal();

		// load service offer configs
		$offCfg = \App\Config\Services_OfferFields::getInstance()->get($serviceCode);

		// DRAW THE FORM //////////////////////////////////////////////////////////////////////////
		$form = \Engine\Form\General::create();

		// FORM OF PAYMENT FIELDS /////////////////////////////////////////////////////////////////
		if ($offCfg['formOfPayment']!='invisible') {

			$fieldsetFop = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['fopLegend']);
			$form->addContainer($fieldsetFop);

			// fop
			$fopFld = new \App\Form\Input\Select_OfferFormOfPayment($this->lang['index']['form']['fop'], "formOfPayment", $this->values['formOfPayment']);
			$fopFld->showDefaultOption();
			if ($offCfg['formOfPayment']=='mandatory') {
				$fopFld->setMandatory();
			}
			$fieldsetFop->addElement($fopFld);

			// fop type
			$fopCcTypeFld = new \App\Form\Input\Select_CreditCards($this->lang['index']['form']['fopCcType'], "formOfPaymentCcType", $this->values['formOfPaymentCcType']);
			$fopCcTypeFld->addAgencyCards(true);
			$fopCcTypeFld->addCorporatesCards($orderFormHandler->getCorporates());
			$fopCcTypeFld->addCustomersCards($serviceEnt->getField('idCustomers'));
			list($fopCcEntType, $fopCcEntId) = explode('_', (string)$this->values['formOfPaymentCcType']);
			if ($fopCcEntType=='manual' and $fopCcEntId) {
				$fopCcTypeFld->addManualCards( [ $fopCcEntId ] );
			}
			$fopCcTypeFld->addNewCard(true);
			$fopCcTypeFld->showDefaultOption();
			$fieldsetFop->addElement($fopCcTypeFld);

			// form of payment CC details
			$fopCcDetFld = new \App\Form\Input\CreditCardDetails($this->lang['index']['form']['fopCcDet'], "formOfPaymentCcDet", $this->values['formOfPaymentCcDet']);
			$fieldsetFop->addElement($fopCcDetFld);
			$fopCcTypeFld->setControlledElements( [ 'new'=>[$fopCcDetFld] ], 'disable' );

			// FOP approval code
			$fopCcApprFld = new \Engine\Form\Input\Text($this->lang['index']['form']['fopCcAppr'], "formOfPaymentCcApprovalCode", $this->values['formOfPaymentCcApprovalCode']);
			$fieldsetFop->addElement($fopCcApprFld);

			// FOP value
			$fopCcValFld = new \App\Form\Input\Price($this->lang['index']['form']['fopCcVal'], "formOfPaymentCcPay", $this->values['formOfPaymentCcPay']);
			$fieldsetFop->addElement($fopCcValFld);

			// control visibility of CC fields
			$ctrlElem = [];
			foreach ($CFG['offers']['formOfPaymentWithCardDetails'] as $fopNeedsCc) {
				$ctrlElem[$fopNeedsCc] = [$fopCcTypeFld, $fopCcDetFld, $fopCcApprFld, $fopCcValFld];
			}
			$fopFld->setControlledElements( $ctrlElem );

		}


		// FORM OF CASHING FIELDS /////////////////////////////////////////////////////////////////
		if ($offCfg['formOfCashing']!='invisible') {

			$fieldsetFoc = \Engine\Form\Container\Fieldset::create($this->lang['index']['form']['focLegend']);
			$form->addContainer($fieldsetFoc);

			// foc
			$focFld = new \App\Form\Input\Select_OfferFormOfCashing($this->lang['index']['form']['foc'], "formOfCashing", $this->values['formOfCashing']);
			$focFld->showDefaultOption();
			if ($offCfg['formOfCashing']=='mandatory') {
				$focFld->setMandatory();
			}
			$fieldsetFoc->addElement($focFld);

			// foc type
			$focCcTypeFld = new \App\Form\Input\Select_CreditCards($this->lang['index']['form']['focCcType'], "formOfCashingCcType", $this->values['formOfCashingCcType']);
			$focCcTypeFld->addCorporatesCards($orderFormHandler->getCorporates());
			$focCcTypeFld->addCustomersCards($serviceEnt->getField('idCustomers'));
			list($focCcEntType, $focCcEntId) = explode('_', (string)$this->values['formOfCashingCcType']);
			if ($focCcEntType=='manual' and $focCcEntId) {
				$focCcTypeFld->addManualCards( [ $focCcEntId ] );
			}
			$focCcTypeFld->addNewCard(true);
			$focCcTypeFld->showDefaultOption();
			$fieldsetFoc->addElement($focCcTypeFld);

			// form of cashing CC details
			$focCcDetFld = new \App\Form\Input\CreditCardDetails($this->lang['index']['form']['focCcDet'], "formOfCashingCcDet", $this->values['formOfCashingCcDet']);
			$fieldsetFoc->addElement($focCcDetFld);
			$focCcTypeFld->setControlledElements( [ 'new'=>[$focCcDetFld] ], 'disable' );

			// FOC approval code
			$focCcApprFld = new \Engine\Form\Input\Text($this->lang['index']['form']['focCcAppr'], "formOfCashingCcApprovalCode", $this->values['formOfCashingCcApprovalCode']);
			$fieldsetFoc->addElement($focCcApprFld);

			// control visibility of CC fields
			$ctrlElem = [];
			foreach ($CFG['offers']['formOfCashingWithCardDetails'] as $focNeedsCc) {
				$ctrlElem[$focNeedsCc] = [$focCcTypeFld, $focCcDetFld, $focCcApprFld];
			}
			$focFld->setControlledElements( $ctrlElem );

		}


		$form->addElement(\Engine\Form\Input\Hidden::create(null, $this->submitButtonName, 1));

		return $form;
	}

}