<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'export_costs.php');

include(DIR_WS_CLASSES.'class.dbform.php');
$smarty = new yesSmarty();
$smarty->compile_dir = DIR_FS_CATALOG.'templates_c';
$export_costs = new export_costs;
$dbf = new dbform('export_costs','export_costs_id',$conn);

$dbf->add_hidden_field('exports_id','');

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);
if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$export_cost = $export_costs->get_export_cost($dbf->select_id);
	$companies = array(0=>array('id'=>$export_cost->data->companies_id,'text'=>yes_get_companies_name($export_cost->data->companies_id)));
}else{
	$dbf->fields['exports_id']->set_default_value($_GET['eID']);
	$companies = array(0=>array('id'=>'','text'=>TEXT_SELECT));
}

$dbf->fields['companies_id']->set_title(ENTRY_COSTS_POSITION,0);
$dbf->fields['companies_id']->set_required(1);
$dbf->fields['companies_id']->set_options($companies,'');
$dbf->fields['companies_id']->set_ajax_function('getCompaniesByLetters');

$dbf->fields['costs']->set_title(ENTRY_COSTS,2);
$dbf->fields['costs']->set_required(1,'float');
$dbf->fields['costs']->set_comments(TEXT_CURRENCY_FIELD);


if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
}

// POST DATA ABFANGEN BEIM SPEICHERN
$mode = ($_POST['action'] == 'edit' or $_POST['action'] == 'add') ? $_POST['action'] : $mode;
if($mode == 'edit'){ 
	$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$export_cost->data->exports_id),$access='exports');
}else{
	$eID = ($_GET['eID']) ? $_GET['eID'] : $_POST['exports_id'];
	$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$eID),$access='exports');
}
$smarty->assign('PAGE_MENU',$menu);
$smarty->assign('TEXT_ADD_ENTRY',TEXT_ADD_ENTRY);
$smarty->assign('TEXT_REMOVE_ENTRY',TEXT_REMOVE_ENTRY);

$result = $dbf->get_form($form_name = 'export_collies',$page = yes_get_php_self(),$mode,$template = DIR_FS_ADMIN.'templates/formular.tpl',$smarty);

if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'edit':
			$sql_update_array = $dbf->get_sql_array($_POST);
			$sql_update_array['costs_text'] = yes_get_companies_name($_POST['companies_id']);
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_COSTS_UPDATED,yes_get_companies_name($_POST['companies_id'])),$export_cost->data->exports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$export_cost->data->exports_id));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$sql_insert_array['costs_text'] = yes_get_companies_name($_POST['companies_id']);
			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_COSTS_ADDED,yes_get_companies_name($_POST['companies_id'])),$_POST['exports_id']);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'action=overview&id='.$_POST['exports_id']));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
