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

setlocale(LC_ALL, "ru_RU.CP1251");
/*PAGE TITLE*/
$page_title = "Стоимость металлов";

$header = "Стоимость металлов";


$val = $_GET['val']==""?$_POST['val']:$_GET['val'];

if(!is_numeric($val)){
	$val = -1;
}
$row = mysql_fetch_object(mysql_query("select * from currencies where id=$val"));
if($row->id==""){
	$row = mysql_fetch_object(mysql_query("select * from currencies where code ='USD'"));
	if($row->id==""){
		$error = base64_encode("Ошибка при работе с курсами валют!");
		header("Location: $SITE_URL/$ADMIN_DIR/error.php?error=$error");
		exit;
	}
	$val = $row->id;
}
$currs = array();
$res = mysql_query("select * from currencies order by id");
while($row = mysql_fetch_object($res))
{
	$currs[] = array(
		'id' => $row->id,
		'name' => stripslashes($row->name),
		'selected' => ($val2 == $row->id)?"selected":"",
		);
}
$smarty->assign("currs", $currs);

$table = "metals";
/* pager start */
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$pager = GetPagerParams($params);
/* pager end */

$group_funcs_options=("<option >Выберите действие<option value='./action.php?action=delete'> - Удалить</option>
                      ");

$dates = array();
$tmp_res = mysql_query("select distinct(date) from metal_prices order by date desc limit ".($pager['start_item'] - 1).",".$SETTINGS['admin_items_per_page']);
while($tmp_row=mysql_fetch_object($tmp_res))
{
	array_push($dates,$tmp_row->date);
}

$table_headers = Array();
$table_items=Array();
$currs = array();
$value_arr = array();
$res = mysql_query("select * from metals order by id");
$value_arr = array();
while($row=mysql_fetch_object($res))
{
	$table_headers["curr".$row->id] = Array(addslashes($row->name), "align=center");
	$tmp_res = mysql_query("select * from metal_prices where currency_id=$val and metal_id=$row->id and date >= ".min($dates)." order by date desc");
	while($tmp_row = mysql_fetch_object($tmp_res))
	{
		$value_arr[$row->id][$tmp_row->date] = $tmp_row->price+0;//($tmp_row->val1_number > 1?($tmp_row->val1_number." за "):"").($tmp_row->course+0);
	}
	array_push($currs,$row->id);
}

$table_headers = array_merge($table_headers, Array(
	"date"=>Array("<a href='".$pager['new_sort']."$table".".date' title='Sort'>Дата</a>",""),
	"funcs"=>Array("Операции","width=5% align=center"),
	"group_funcs"=>Array("<select name=act_sel onChange='SubmitForm();'><option>Выберите действие<option value='./action.php?action=delete'>-Удалить</select>"))
);

$res = mysql_query($query);
$itter = $pager['start_item'];
$i=0;

for($i=0;$i < sizeof($dates);$i++)
{
	$table_items[$itter] = array();
	for($j=0;$j < sizeof($currs);$j++)
	{
		$table_items[$itter] = array_merge($table_items[$itter], array
		(
			"curr".$currs[$j] => array($value_arr[$currs[$j]][$dates[$i]], "align=center")
		));
	}
	$table_items[$itter] = array_merge($table_items[$itter], 
	Array(
		"id" => array($dates[$i], ""),
		"date"=>array(strftime("%d %h %Y<br>%H:%M:%S", $dates[$i])),
		"funcs"=>Array("<nobr><a href='./add.php?id=$dates[$i]' title='Редактировать'>$ADMIN_ICONS[edit]</a>&nbsp;<a href='#' onClick=\"if(confirm('Удалить??')){document.location.href='./action.php?action=delete&id=$dates[$i]';}return false;\" title='Удалить'>$ADMIN_ICONS[del]</a></nobr>
			","align=center")
		)
		);
	$itter++;
}


//echo "<pre>".print_r($table_items, 1)."</pre>";

$smarty->assign('page_title', $page_title);

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

/* table template */
$smarty->assign('pager',$pager);
$smarty->assign('table_headers',$table_headers);
$smarty->assign('table_items',$table_items);
$smarty->assign('table_width',"100%");
$smarty->assign('top_menu_tpl',"admin_metals_menu.tpl");
$smarty->assign('main_tpl',"admin_list_table.tpl");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

/* display content */
$smarty->display('admin_index.tpl');
?>