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


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

$dbf = new dbform('export_targets','export_targets_id',$conn);

$export_targets = new export_targets;

// bei edit die id angeben
$dbf->select_id = xtc_db_input($_GET['id']);
if(xtc_not_null($dbf->select_id)){
	$mode = 'edit';
	$export_target = $export_targets->get_export_target($dbf->select_id);
	$invoice_recipients = array(0=>array('id'=>$export_target->data->invoice_recipient_id,'text'=>yes_get_companies_name($export_target->data->invoice_recipient_id) ) );
}else{
	$smarty->assign('PAGE_TITLE',HEADING_TITLE_NEW);
	$mode = 'add';
	$invoice_recipients = array(0=>array('id'=>'','text'=>TEXT_SELECT) );
}


$dbf->fields['invoice_recipient_id']->set_title(ENTRY_INVOICE_RECIPIENT,20);
$dbf->fields['contact_id']->set_title(ENTRY_CONTACT,5);
if($mode == 'edit'){
	$ships_id = $export_target->data->ships_id;
	$ship_name = yes_get_ships_name($ships_id);
	$ship_company_id = yes_get_ship_companies_id_by_role($ships_id,'owner');
	$ship_owner_name = yes_get_companies_name($ship_company_id);
	$ship_invoice_recipients_id = yes_get_ship_companies_id_by_role($ships_id,'invoice');
	$dbf->fields['contact_id']->set_options(yes_get_ship_contacts($ships_id,'owner'),'');
	$dbf->fields['invoice_recipient_id']->set_options($invoice_recipients,$ship_invoice_recipients_id);
	$smarty->assign('PAGE_TITLE',sprintf(HEADING_TITLE,$ship_name));
}else{
	$exports_id = ($_GET['eID']) ? xtc_db_input($_GET['eID']) : xtc_db_input($_POST['exports_id']);
	$dbf->add_hidden_field('exports_id',$exports_id);
	$dbf->fields['ships_id']->set_title(ENTRY_SHIP,0);
	$dbf->fields['ships_id']->set_options($export_targets->get_available_targets_array($exports_id),'');
	$dbf->fields['ships_id']->set_required(1);
	$dbf->fields['ships_id']->set_params('onchange="ajax_update_dropdown(this,\'dbfdivfieldcontact_id\',\'contact_id\',\'export_targets\',\'yes_get_ship_contacts\');set_ship_invoice_recipient_default(val=this.value);"');
	$dbf->fields['contact_id']->set_options(yes_get_company_customers(0),'');
	$dbf->fields['invoice_recipient_id']->set_options($invoice_recipients,0);
}
$dbf->fields['invoice_recipient_id']->set_ajax_function('getCompaniesByLetters');

	
$dbf->fields['eta']->set_title(ENTRY_ETA,10);
$dbf->fields['ets']->set_title(ENTRY_ETS,15);
	

$conditions_array = array(0=>array('id'=>'pp','text'=>ENTRY_DELIVERY_CONDITION_PP),1=>array('id'=>'cc','text'=>ENTRY_DELIVERY_CONDITION_CC));
$dbf->fields['delivery_conditions']->set_title(ENTRY_DELIVERY_CONDITIONS,25);
$dbf->fields['delivery_conditions']->set_options($conditions_array,'');

// 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='.$export_target->data->exports_id),$access='exports');
$smarty->assign('PAGE_MENU',$menu);

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

			xtc_db_perform($dbf->table,$sql_update_array,'update',$dbf->index_field."='".$dbf->select_id."'");

			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_UPDATED,$ship_name),$export_target->data->exports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_UPDATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'id='.$export_target->data->exports_id.'&action=overview'));
			break;
		case 'add':
			$sql_insert_array = $dbf->get_sql_array($_POST);

			xtc_db_perform($dbf->table,$sql_insert_array);
			$id = $conn->Insert_ID();

			$ship_name = yes_get_ships_name($_POST['ships_id']);
			$YES_HISTORY->add('exports',sprintf(HISTORY_EXPORT_TARGET_ADDED,$ship_name),$export_target->data->exports_id);
			
			$messageStack->add_session(MESSAGE_SUCCESS_CREATE,'success');
			xtc_redirect(xtc_href_link(FILENAME_EXPORTS,'id='.$_POST['exports_id'].'&action=overview'));
			break;
	}
}
include(DIR_WS_INCLUDES.'header.php');
echo $result;

include('includes/footer.php');
