<?php

// SET THE DEFAULT VALUES FOR THE FORM ////////////////////////////////////////////////////////////
if (!isset($R['status'])) $R['status']=$CFG['createInvoiceBalance']['fieldsDefaults']['status'];
if (!isset($R['idUser'])) $R['idUser']['id']=$CFG['createInvoiceBalance']['fieldsDefaults']['idUser'];


// drop plugin trigger
\Engine\Plugins\Manager::getInstance()->executeEvent("invoices-balances-createBalance-beforeFieldsDefinition", array());
// TODO: refactor the plugin

// NEW FORM ///////////////////////////////////////////////////////////////////////////////////////
$form = \Engine\Form\General::create();
$form->setStyle('width', 350);

// NEW CONTAINER ----------------------------------------------------------------------------------
$formCont=\Engine\Form\Container\Fieldset::create($_LANG['invoices']['balanceCreate']['tpls']['detailsLegend']);
$form->addContainer($formCont);


// BALANCE STATUS
$fld=new \Engine\Form\Input\Radio($_LANG['invoices']['balanceCreate']['formRowDefinitions']['status'], 'status', $R['status']);
$fld->setPossibleValues(array('partial'=>$_LANG['balanceInvoices']['balanceDetails']['status']['partial'], 'final'=>$_LANG['balanceInvoices']['balanceDetails']['status']['final']));
$fld->importFromConfig($CFG['globalEntity']['::Balance']['balanceStatus']);
$formCont->addElement($fld);

// BALANCE NAME
$fld=new \Engine\Form\Input\Text($_LANG['invoices']['balanceCreate']['formRowDefinitions']['balanceName'], 'name', $R['name']);
$fld->importFromConfig($CFG['globalEntity']['::Balance']['balanceName']);
$formCont->addElement($fld);

// BALANCE CODE
$fld=new \Engine\Form\Input\Text($_LANG['invoices']['balanceCreate']['formRowDefinitions']['balanceCode'], 'code', $R['code']);
$fld->importFromConfig($CFG['globalEntity']['::Balance']['balanceCode']);
$formCont->addElement($fld);

// BALANCE RESPONSIBLE
$fld=new \App\Form\Input\Autocomplete_User($_LANG['invoices']['balanceCreate']['formRowDefinitions']['responsiblePerson'], 'idUser', $R['idUser']);
$fld->importFromConfig($CFG['globalEntity']['::Balance']['idUser']);
$formCont->addElement($fld);

// REMARKS
$fld_remarks = new \App\Form\Input\Remarks($_LANG['invoices']['balanceCreate']['formRowDefinitions']['remarks'], 'remarks', $R['remarks']);
$fld_remarks->setRemarksType('balance');
$formCont->addElement($fld_remarks);

// BALANCE COMMENTS
$fld=new \Engine\Form\Input\Textarea($_LANG['invoices']['balanceCreate']['formRowDefinitions']['balanceComments'], 'comments', $R['comments']);
$fld->importFromConfig($CFG['globalEntity']['::Balance']['comments']);
$formCont->addElement($fld);

// balance source
$formCont->addElement(new \Engine\Form\Input\Hidden('', 'src', $R['src']));
// parent
$formCont->addElement(new \Engine\Form\Input\Hidden('', 'parent', $R['parent']));
// balance data
$formCont->addElement(new \Engine\Form\Input\Hidden('', 'balanceData', $R['balanceData']));

// if src = request - add balance components
if ($R['src']=='req') {
	foreach($supplierRowsIds as $idRow=>$nothing) {
		$formCont->addElement(new \Engine\Form\Input\Hidden('', 's['.$idRow.']', 1));
	}
	foreach($clientServicesIds as $idServ=>$nothing) {
		$formCont->addElement(new \Engine\Form\Input\Hidden('', 'c['.$idServ.']', 1));
	}
}

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

