<?
define("ADMIN_AUTHORIZATION",1);
include("../../../includes/merchant/common.php");
include("../../../includes/merchant/merchant_user_class.php");

$table = "merchant_users";

$id = trim($_GET['id']=="")?trim($_POST['id']):trim($_GET['id']);



$CheckObj=new TCheckForm;

$merchant_user = new MerchantUser($db);

if($id>0)
{
	$merchant_user->loadFromDb($id);
}
else{
	$id=-1;
}


$ex_currencies_opts = "";
$db->query("SELECT * FROM `ex_currencies` 
	WHERE 1=status AND `code` IN ('WMR', 'WMZ', 'WMU', 'WME', 'EGOLD', 'ESILVER', 'EPLATINUM', 'EPALLADIUM')");
$tmp_ex_currencies = $db->getAllRows();
$ex_currencies = array(0=>"Выберите");
foreach ($tmp_ex_currencies as $k=>$v)
{
	$ex_currencies_opts .= "<option value=\"$v->id\">".stripslashes($v->name)."</option>\n";
}

/* generating fields */
$fields=Array();
if ($id>0) {
    $fields[id]=Array($id,'ID:');
}

$fields = Array_Merge($fields,Array(
	"last_name" => Array('<input type="text" size=60 class="text_input">','Фамииля:'),
	"name" => Array('<input type="text" size=60 class="text_input">','Имя:'),
	"p_name" => Array('<input type="text" size=60 class="text_input">','Отчество:'),
	"email" => Array('<input type="text" size=60 class="text_input">','E-mail:'),
	"password" => Array('<input type="password" size=20 class="text_input" maxlength=20>','Пароль:'),
	"password_confirm" => Array('<input type="password" size=20 class="text_input" maxlength=20>','<nobr>Подтверждение пароля:</nobr>'),
	"passport_number" => Array('<input type="text" size=20 class="text_input">','<nobr>Номер паспорта:</nobr>'),
	"passport_series" => Array('<input type="text" size=20 class="text_input">','<nobr>Серия паспорта:</nobr>'),
	"site_name" => Array('<input type="text" size=60 class="text_input">','<nobr>Название сайта:</nobr>'),
	"site_url" => Array('<input type="text" size=60 class="text_input">','<nobr>URL сайта:</nobr>'),
	"ex_currency_id" => Array('<select class="text_input">
		'.$ex_currencies_opts.'
		</select>','Валюта'),
	"purse" => Array('<input type="text" size=60 class="text_input">','Номер кошелька'),
	"purse_serial" => Array('<input type="text" size=60 class="text_input">','Номер аккаунта (WMID, для WebMoney)'),
	"secret_word" => Array('<input type="text" size=60 class="text_input">','Секретное слово'),	
	"notification_url" => Array('<input type="text" size=60 class="text_input">','URL оповещения'),	
	"notification_method" => Array(
		'<select class="text_input">
		<option value="'.METHOD_GET.'">GET</option>
		<option value="'.METHOD_POST.'">POST</option>
		<option value="'.METHOD_EMAIL.'">E-MAIL</option>
		</select>',
		'Метод оповещения'),
			
	"success_url" => Array('<input type="text" size=60 class="text_input">','URL возврата в случае успеха'),	
	"success_method" => Array(
		'<select class="text_input">
		<option value="'.METHOD_GET.'">GET</option>
		<option value="'.METHOD_POST.'">POST</option>
		</select>',
		'Метод отсылки в URL возврата в случае успеха'),	
		
	"failure_url" => Array('<input type="text" size=60 class="text_input">','URL возврата в случае неудачи'),	
	"failure_method" => Array(
		'<select class="text_input">
		<option value="'.METHOD_GET.'">GET</option>
		<option value="'.METHOD_POST.'">POST</option>
		</select>',
		'Метод отсылки в URL возврата в случае неудачи'),	
	"stat" => Array('<select><option value="1">Активный<option value="0">Не активный</select>','Статус:','Укажите статус, пожалуйста.',"^0|1$")
));

if(isset($_POST['subm']))
{
	$merchant_user->email 				= $_POST['email'];
	$merchant_user->password 			= $_POST['password'];
	$merchant_user->password_confirm	= $_POST['password_confirm'];
	$merchant_user->name 				= $_POST['name'];
	$merchant_user->last_name 			= $_POST['last_name'];
	$merchant_user->p_name 				= $_POST['p_name'];
	$merchant_user->passport_series 	= $_POST['passport_series'];
	$merchant_user->passport_number 	= $_POST['passport_number'];
	$merchant_user->site_name 			= $_POST['site_name'];
	$merchant_user->site_url 			= $_POST['site_url'];
	$merchant_user->ex_currency_id 		= $_POST['ex_currency_id'];
	$merchant_user->purse 				= $_POST['purse'];
	$merchant_user->purse_serial		= $_POST['purse_serial'];
	$merchant_user->secret_word 		= $_POST['secret_word'];
	$merchant_user->notification_url 	= $_POST['notification_url'];
	$merchant_user->notification_method = $_POST['notification_method'];
	$merchant_user->success_url 		= $_POST['success_url'];
	$merchant_user->success_method 		= $_POST['success_method'];
	$merchant_user->failure_url			= $_POST['failure_url'];	
	$merchant_user->failure_method 		= $_POST['failure_method'];
	$merchant_user->status 				= $_POST['stat'];
	
	$valid = $merchant_user->validate();
	
	if(TRUE === $valid)
	{
		if($merchant_user->save())
		{
			$_SESSION['STATUS_MSG'] = "Информация успешно сохранена!";
			header("Location: ./merchants.php");
			exit;
		}
		else
		{
			$_SESSION['ERROR_MSG'] = "Ошибка при работе с базой данных.";
			header("Location: ./merchants.php");
			exit;
		}
	}
	else
	{
		$error = "";
		foreach ($valid as $v)
		{
			$error .= $v."<br>\n";
		}
	}
}	
$merchant_user->password = "";
$merchant_user->password_confirm = "";
$merchant_user->stat = $merchant_user->status;
foreach($fields as $key=>$value){
	$$key=htmlspecialchars(stripslashes($merchant_user->{$key}));
	$smarty->assign($key,$$key);
}

/* smarty */
$smarty->assign('id',$id);

$page_title = $id>0?"Изменение данных продавца":"Добавление нового продавца";
$smarty->assign('index_page','./index.php?l_pager');
$smarty->assign('page_title',$page_title);
$smarty->assign('top_menu_tpl',"merchant/admin_merchant_menu.tpl");

$smarty->assign('error',$error);
$smarty->assign('status',$status);

/* table template */
$smarty->assign('fields',$fields);
$smarty->assign('table_width',"95%");
$smarty->assign('main_tpl',"admin_add_form.tpl");
/* display content */
$smarty->display('admin_index.tpl');

?>