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

$table = "ex_currencies";

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



$CheckObj=new TCheckForm;

//$res=pg_query($DB_LINK, );

/*getting row*/
if($id>0)
{
	
}
else
{
	$id=-1;
}

/* generating fields */
$fields=Array();
if ($id>0) 
{
	$res = mysql_query("select * from metals");
	while($row=mysql_fetch_object($res))
	{
		$tmp_row = mysql_fetch_object(mysql_query("select * from metal_prices where date=$id and metal_id=$row->id"));
		
		$var = $row->code."_num";
		$$var = $tmp_row->val1_number;
		$var = $row->code;
		$$var = $tmp_row->price+0;
	}
}

$res = mysql_query("select * from metals");
while($row=mysql_fetch_object($res))
{
	$var = $row->code."_num";
	$num_val = $$var;
	$var = $row->code;
	$c_val = $$var;
	if(!is_numeric($num_val))
	{
		$num_val = 1;
	}
	$fields[$row->code."_num"] = array($row->code."_num", $num_val, $row->code, $c_val);
	
}

if(isset($_POST['subm'])){
	reset ($_POST);
	$flag = false;
	while (list($key, $value) = each ($_POST)) 
	{
		$$key = str_replace(",",".",htmlspecialchars(trim(substr($value,0,255))));
		if(((!is_numeric($$key)) || 0 >= $$key)&&(!eregi("_num",$key) && $key != "id" && $key!="subm"&&$key!="date"&&$key!="action"))
		{
			$error .="Стоимость $key указана неверно!<br>\n";
		}
		/*elseif(eregi("_num",$key)&&(!is_numeric($value)||!($value>0)))
		{
			if (!$flag)
			{
				$error .= "Какое-то количество указано неверно!<br>\n";
				$flag=true;
			}
		}*/
	}
	if($error==""){
		
		$res = mysql_query("select * from metals");
		$val1_row = mysql_fetch_object(mysql_query("select * from currencies where code='USD'"));
		$time = time();
		while($row=mysql_fetch_object($res))
		{
			$var = $row->code;
			$value = $$var;
			$var = $row->code."_num";
			$nominal = $$var;
			
			if (0 < $id)
			{
				$query = "UPDATE `metal_prices` set `price` = '$value' where `date` = $id and `metal_id`=$row->id";				
				//$query = "UPDATE `courses` set `course` = '$value', `back_course` = '".round(1/$value,6)."', `val1_number` = '$nominal' where `val2_id` = '".$val1_row->id."' and `val1_id` = '".($row->id)."' and `date` = $id";
			}
			else
			{
				$query = "INSERT INTO `metal_prices` (`metal_id` , `currency_id` , `price` , `date` ) VALUES 	('$row->id', '$val1_row->id', '$value', '".$time."');";
				
			}
			mysql_query($query);
			
		}
		
		$_SESSION['STATUS_MSG'] = "Информация успешно сохранена!";
		header("Location: ./index.php");
		exit;
	}
}

foreach($fields as $key=>$value)
{
	$fields[$key] = array($value[0], $$value[0], $value[2], $$value[2]);
//	$smarty->assign($value[0],$$value[0]);
//	$smarty->assign($value[2],$$value[2]);
}

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

$page_title = $id>0?"Редактирование курса на ".strftime("%d %h %Y", $id):"Добавление стоимости металла";
$smarty->assign('index_page','./index.php?l_pager');
$smarty->assign('page_title',$page_title);
$smarty->assign('top_menu_tpl',"admin_metals_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_metal_prices_form.tpl");
/* display content */
$smarty->display('admin_index.tpl');

?>