<?php
include('includes/application_top.php');
require_once(DIR_WS_CLASSES.'import_ship_costs.php');
require_once(DIR_WS_CLASSES.'import_ships.php');

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

$dbf = new dbform('import_ship_costs','import_ship_costs_id',$conn);

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

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$import_ship_costs = new import_ship_costs;
	$import_ship_cost = $import_ship_costs->get_import_ship_cost($dbf->select_id);
	$ships_id = $import_ship_cost->get_ships_id();
	$imports_id = $import_ship_cost->get_imports_id();
	$companies = array(0=>array('id'=>$import_ship_cost->data->companies_id,'text'=>yes_get_companies_name($import_ship_cost->data->companies_id)));
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$imports_id = ($_GET['iID']) ? xtc_db_input($_GET['iID']) : xtc_db_input($_POST['imports_id']);
	$dbf->add_hidden_field('imports_id',$imports_id,$extern_table=true);
	$import_ships_id = ($_GET['isID']) ? xtc_db_input($_GET['isID']) : xtc_db_input($_POST['import_ships_id']);
	$dbf->add_hidden_field('import_ships_id',$import_ships_id);
	$import_ships = new import_ships;
	$import_ship = $import_ships->get_import_ship($import_ships_id);
	$ships_id = $import_ship->data->orderer_ships_id;
	$companies = array(0=>array('id'=>'','text'=>TEXT_SELECT));
	$mode = 'add';
}
$dbf->fields['companies_id']->set_title(ENTRY_COSTS_POSITION,4);
$dbf->fields['companies_id']->set_options($companies,'');
$dbf->fields['companies_id']->set_ajax_function('getCompaniesByLetters');



// POST DATA ABFANGEN BEIM SPEICHERN
$mode = ($_POST['action'] == 'edit' or $_POST['action'] == 'add') ? $_POST['action'] : $mode; 

$menu = yes_draw_href_button(IMAGE_BACK,xtc_href_link(FILENAME_IMPORTS,'action=overview&id='.$imports_id),$access='imports');
$smarty->assign('PAGE_MENU',$menu);

$ship_name = yes_get_ships_name($ships_id);
$heading_title_param = $ship_name;

$result = $dbf->get_form($form_name = 'import_ship_costs',$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);

			$company_name = yes_get_companies_name($_POST['companies_id']);
			$sql_update_array['costs_text'] = $company_name;
			
			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_COST_UPDATED,$company_name,$ship_name),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'id='.$imports_id.'&action=overview'));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);
			$company_name = yes_get_companies_name($_POST['companies_id']);
			$sql_insert_array['costs_text'] = $company_name;
			
			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_COST_ADDED,$company_name,$ship_name),$imports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_IMPORTS,'id='.$imports_id.'&action=overview'));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
