<?php
if( $a == 'list' ){
	if( isset( $sOption ) ){
		$content.= $oTpl->parse( 'message_done.tpl' );
	}
	if( isset( $_POST['sOption'] ) ){
		saveVariables( $_POST, DB_CONFIG );
		saveVariables( $_POST, DB_CONFIG_LANG );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save' . '&activeTabId=' . $_POST['activeTabId'] );
		exit;
	}
	if( !empty( $config['enadawca_user'] ) && !empty( $config['enadawca_password'] ) ){
		try{
			require_once DIR_LIBRARIES . 'ENadawca/ElektronicznyNadawca.php';
			$sApiURL = $config['enadawca_test'] ? 'libraries/ENadawca/en-test.wsdl' : 'libraries/ENadawca/en.wsdl';
			$E = new ElektronicznyNadawca($sApiURL);
			$GUD = new GetUrzedyNadania();
			$resp = $E->getUrzedyNadania( $GUD );
			$UN = $resp->urzedyNadania;
			$aUrzedyNadania = Array();
			foreach( $UN as $un ){
				$aUrzedyNadania[$un->urzadNadania] = $un->opis;
			}
			$oTpl->assign( 'aUrzedyNadania', $aUrzedyNadania );
		} catch( Exception $e ){
			$oTpl->assign( 'sErrorMessage', $lang['enadawca_soap_error'] . $e->getMessage() );
			$content .= $oTpl->parse( 'message_other_error.tpl' );
		}
	}
	$aCarriers = $oOrder->listCarriersAdmin( );
	$oTpl->assign( 'aCarriers', $aCarriers );
	$content.= $oTpl->parse( 'carriers_list.tpl' );
} elseif( $a == 'delete' && isset( $iCarrier ) && is_numeric( $iCarrier ) ){
	$oOrder->deleteCarrier( $iCarrier );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=del' );
	exit;
} elseif( $a == 'form' ){
	if( isset( $_POST['sOption'] ) ){
		if( !is_array( $_POST['aSummaryLimits'] ) ) $_POST['aSummaryLimits'] = Array();
		if( !is_array( $_POST['aWeightLimits'] ) ) $_POST['aWeightLimits'] = Array();
		array_unshift( $_POST['aSummaryLimits'], 0 );
		array_unshift( $_POST['aWeightLimits'], 0 );
		$aPricesTmp = Array();
		foreach( $_POST['aWeightLimits'] as $fWeightLimit ){
			foreach( $_POST['aSummaryLimits'] as $fSummaryLimit ){
				$aPricesTmp[$fWeightLimit][$fSummaryLimit] = array_shift( $_POST['aPrices'] );
			}
		}
		$_POST['aPrices'] = $aPricesTmp;
		if( !isset( $_POST['iStatus'] ) ) $_POST['iStatus'] = 0;
		$iCarrier = $oOrder->saveCarrier( $_POST );
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save&iCarrier=' . $iCarrier );
		exit;
	}
	if( isset( $iCarrier ) && is_numeric( $iCarrier ) ){
		$aCarrier = $oOrder->throwCarrier( $iCarrier );
	}
	if( !isset( $aCarrier ) ){
		$iCarrier = null;
	}
	$aVats = throwVatsForSelect( );
	$aCountries = $oDB->executeSQL( 'SELECT DISTINCT `iCountry`, `sCountry`, IF( `iRegion` IS NOT NULL, 0, `iContinent` ) AS `iContinent` FROM `' . $oDB->throwTableName( DB_COUNTRIES ) . '` `c` LEFT JOIN `' . $oDB->throwTableName( DB_REGIONS ) . '` `r` USING( `iCountry` ) ORDER BY IF( `iRegion` IS NOT NULL, 0, `iContinent` ) ASC, `sCountry` ASC', true, DataBase::ASSOCIATIVE, 'iCountry' ); //$oOrder->throwCountriesArray( 'iCountry', 'sCountry', true );
	foreach( $aCountries as &$aCountry ){
		if( $aCountry['iContinent'] > 0 ) break; //Tylko dla iContinent = 0, a jeśli jest większy - to dalej już nie będzie - sortowanie
		if( $aCountry['iContinent'] == 0 ){
			$aCountry['aRegions'] = $oDB->executeSQL( 'SELECT `iRegion`, `sRegion` FROM `' . $oDB->throwTableName( DB_REGIONS ) . '` WHERE `iCountry` = ' . $aCountry['iCountry'] . ' ORDER BY `sRegion`', true, DataBase::ASSOCIATIVE );
		}
	}
	$aCarrier['aPayments'] = $oOrder->listPaymentsCarriersAdmin( $iCarrier );
	$aCarrier['aCountries'] = $oOrder->throwCarrierCountriesArray( $iCarrier );
	$aCarrier['aRegions'] = $oOrder->throwCarrierRegionsArray( $iCarrier );
	$aPricesTMP = $oDB->executeSQL( 'SELECT `fMinWeight`, `fMinSummary`, `fPrice` FROM `' . $oDB->throwTableName( DB_CARRIERS_PRICES ) . '` WHERE `iCarrier` = ' . intval( $iCarrier ) . ' ORDER BY `fMinWeight` ASC, `fMinSummary` ASC', true );
	$aCarrier['aPrices'] = Array();
	foreach( $aPricesTMP as $aPriceTMP ){
		if( $aPriceTMP['fMinWeight'] > 0 ) $aWeightLimits[$aPriceTMP['fMinWeight']] = true;
		if( $aPriceTMP['fMinSummary'] > 0 ) $aSummaryLimits[$aPriceTMP['fMinSummary']] = true;
		$aCarrier['aPrices'][$aPriceTMP['fMinWeight']][$aPriceTMP['fMinSummary']] = $aPriceTMP['fPrice'];
	}
	$aCarrier['aPrices']['aWeightLimits'] = array_keys( $aWeightLimits );
	$aCarrier['aPrices']['aSummaryLimits'] = array_keys( $aSummaryLimits );
	$oTpl->assign( 'aCarrier', $aCarrier );
	$oTpl->assign( 'aVats', $aVats );
	$oTpl->assign( 'aCountries', $aCountries );
	$content.= $oTpl->parse( 'carriers_form.tpl' );
}
?>