<?php
if( $a == 'form' ){
	if( isset( $_POST['sOption'] ) ){
		$iStore = saveStore( $_POST );
		$oProduct->sendAvailNewsletter();
		header( 'Location: admin.php?p=stores-form&iStore=' . $iStore . '&sOption=save' );
		exit;
	}
	if( isset( $_GET['sOption'] ) ){
		$content .= $oTpl->parse( 'message_done.tpl' );
	}
	if( isset( $_GET['iStore'] ) && is_numeric( $_GET['iStore'] ) ){
		$aStore = getStore( $_GET['iStore'] );
	} else{
		$aStore = Array(
			'iStore' => null,
			'sName' => '',
			'iSupplier' => null,
			'iPriority' => 0,
			'iAvailableOnStock' => null,
			'iAvailableOutOfStock' => null
		);
	}
	$oTpl->assign( 'aAvailabilities', $oProduct->throwAvailabilityForSelect( ) );
	$oTpl->assign( 'aAvailabilitiesOnStock', $oProduct->throwAvailabilityForSelectOnStock( ) );
	$oTpl->assign( 'aStore', $aStore );
	$content .= $oTpl->parse( 'stores_form.tpl' );
} elseif( $a == 'delete' && !empty( $_GET['iStore'] ) ){
	$iStore = intval( $_GET['iStore'] );
	if( $oDB->executeSQL( 'SELECT COUNT( `iStore` ) FROM `' . $oDB->throwTableName( DB_STORES ) . '` WHERE `iStore` = ' . $iStore, true, DataBase::SINGLE_VALUE ) ){
		if( $oDB->executeSQL( 'SELECT COUNT( `iStore` ) FROM `' . $oDB->throwTableName( DB_STORES ) . '` WHERE `iStore` != ' . $iStore, true, DataBase::SINGLE_VALUE ) ){
			$oDB->deleteInFile( DB_STORES, $iStore, 'iStore' );
			$content .= $oTpl->parse( 'message_done.tpl' );
		} else{
			$oTpl->assign( 'sErrorMessage', $lang['Store_only_cant_delete'] );
			$content .= $oTpl->parse( 'message_other_error.tpl' );
		}
	} else{
		$content .= $oTpl->parse( 'message_empty.html' );
	}
}