<?php
include('includes/application_top.php');
include(DIR_WS_CLASSES.'export_targets.php');
require_once(DIR_WS_CLASSES.'export_target_import_ships.php');
require_once(DIR_WS_CLASSES.'imports.php');
require_once(DIR_WS_CLASSES.'exports.php');

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

$dbf = new dbform('export_target_import_ships','export_target_import_ships_id',$conn);


// bei edit die id angeben
// haben hier kein edit, nur delete - also egal
$dbf->select_id = xtc_db_input($_GET['id']);

if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
}else{
	$etID = ($_GET['etID']) ? xtc_db_input($_GET['etID']) : xtc_db_input($_POST['export_targets_id']);
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$export_targets = new export_targets;
	$export_target = $export_targets->get_export_target($etID);
	$ship_name = yes_get_ships_name(yes_get_ships_id_from_export_targets_id($etID));
	$exports_id = $export_target->data->exports_id;
	$dbf->add_hidden_field('export_targets_id',$etID);
	$dbf->fields['import_ships_id']->set_title(ENTRY_IMPORT_NUMBER,0);
	$dbf->fields['import_ships_id']->set_options($export_targets->get_available_import_ships_array($etID),'');
	$dbf->fields['import_ships_id']->set_required(1);
	$mode = 'add';
}
$heading_title_param = $ship_name; // wird in der header.php eingesetzt


// 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_EXPORTS,'action=overview&id='.$exports_id),$access='export_target_imports');
$smarty->assign('PAGE_MENU',$menu);

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

if( $result == 'save' ){
	// SAVE HANDLER
	switch($mode){
		case 'add':
			$import_number = yes_get_import_number_from_import_ships_id(xtc_db_input($_POST['import_ships_id']));
			$sql_insert_array = $dbf->get_sql_array($_POST);
			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();
			$import_number = yes_get_import_number_from_import_ships_id(xtc_db_input($_POST['import_ships_id']));
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_IMPORT_SHIP_ADDED,$import_number,$ship_name),$exports_id);

			// IMPORT_SHIPS STATUS SETZEN
			$import_ships = new import_ships;
			$import_ships->update_status(xtc_db_input($_POST['import_ships_id']),1);
			$import_ship = $import_ships->get_import_ship(xtc_db_input($_POST['import_ships_id']));
			$exports = new exports;
			$export = $exports->get_export($exports_id);
			$YES_HISTORY->add('imports',sprintf(HISTORY_IMPORT_SHIP_EXPORTED,$ship_name,$export->data->export_number),$import_ship->data->imports_id);
			$imports_id = $import_ship->data->imports_id;
				
			// PRUEFEN OB IMPORT NOCH OFFENE SCHIFFE ENTHAELT
			$imports = new imports;
			if($imports->check_update_status($imports_id)){
				$imports->update_status($imports_id,1);
				$YES_HISTORY->add('imports',HISTORY_IMPORT_EXPORTED,$imports_id);
			}
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'id='.$exports_id.'&action=overview'));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
