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


/*PAGE TITLE*/
$page_title = "Комиссии";

$header = "Комиссии";

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

if(!is_numeric($ex_id)){
	$ex_id = -1;
}
$row = mysql_fetch_object(mysql_query("select * from ex_currencies where id=$ex_id"));
if($row->id==""){
	$row = mysql_fetch_row(mysql_query("select min(id) from ex_currencies"));
	if($row[0]==""){
		$_SESSION['ERROR_MSG'] = "Ошибка при работе с комиссиями валют!";
		header("Location: $SITE_URL/$ADMIN_DIR/index.php");
		exit;
	}
	$ex_id = $row[0];
}

$table = "ex_currency_comissions";
/* pager start */
$params['query'] = "select count(*) from $table where id=$ex_id";
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$pager = GetPagerParams($params);
/* pager end */

$curr_row = mysql_fetch_object(mysql_query("select ex_currencies.*, currencies.code as real_curr_code from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id where ex_currencies.id=$ex_id"));
$smarty->assign("ex_curr_name", $curr_row->name);
$smarty->assign("ex_curr_code", $curr_row->code);

$ex_curr_res = mysql_query("select ex_currencies.*, currencies.code as real_curr_code from ex_currencies inner join currencies on currencies.id = ex_currencies.currency_id");
while ($ex_curr_row = mysql_fetch_object($ex_curr_res))
{
	$ex_currs[] = array("id" => $ex_curr_row->id,
						"name" => $ex_curr_row->name,
						"selected" => ($ex_id==$ex_curr_row->id)?"selected=\"selected\"":"",
						);
}
$smarty->assign("ex_currs", $ex_currs);

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

$table_headers = Array(
	"itter"=>Array("№","width=20 align=center"),
	"amount_from"=>Array("<a href='".$pager['new_sort']."$table".".amount_from' title='Sort'>Сумма от</a>",""),
	"amount_to"=>Array("<a href='".$pager['new_sort']."$table".".amount_to' title='Sort'>Сумма до</a>",""),
	"perc"=>Array("<a href='".$pager['new_sort']."$table".".perc' title='Sort'>Процент</a>",""),
	"fix"=>Array("<a href='".$pager['new_sort']."$table".".fix' 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_com'>-Удалить</select>"));

$search_string = "ex_currency_id=$ex_id";
$query = "
  select * from ex_currency_comissions".
  ($search_string?" where $search_string":"")
  ." ORDER BY ".$pager['sort_by']." ".$pager['sort_ord']
  ." LIMIT ".$SETTINGS['admin_items_per_page']." OFFSET ".($pager['start_item']-1);
  

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

while ($row = @mysql_fetch_object($res)){
	switch($row->status){
		case 0:
			$s= $ADMIN_ICONS[status_off];
		break;
		case 1:
			$s= $ADMIN_ICONS[status_on];
		break;
	}
	if(file_exists($_SERVER["DOCUMENT_ROOT"].$COUNTRY_IMG_PATH."/$row->country_id.gif")){
		$c_img = "<img src='$COUNTRY_IMG_PATH/$row->country_id.gif'>";
	}
	else{
		$c_img = "&nbsp;";
	}
	if(file_exists($_SERVER["DOCUMENT_ROOT"].$CURR_IMG_PATH."/$row->id.gif")){
		$ex_img = "<img src='$CURR_IMG_PATH/$row->id.gif'>";
	}
	else{
		$ex_img = "&nbsp;";
	}

	if($row->metal_id>0){
		$rest = "<a href=\"./balance_hist.php?id=$row->id\" title=\"Посмотреть историю\"><nobr>". EGoldGetCurrencyValue($row->metal_id,$row->amount)." USD</nobr></a><br><small>".($row->amount+0)." гр.</small>";
	}
	else{
		$rest = "<a href=\"./balance_hist.php?id=$row->id\" title=\"Посмотреть историю\"><nobr>".($row->amount+0)." $row->curr_code</nobr>";
	}
	
	$table_items[$itter] = Array(
		"id"=>array($row->id,""),
		"itter"=>array($itter,"align=center"),
		"amount_from"=>array(floatval($row->amount_from), ""),
		"amount_to"=>array(floatval($row->amount_to), ""),
		"amount_fix"=>array(floatval($row->fix), ""),
		"perc"=>array(floatval($row->perc)*100, ""),
		"fix"=>array(floatval($row->fix), ""),
		"funcs"=>Array("<nobr><a href='./add_com.php?id=$row->id' title='Редактировать'>$ADMIN_ICONS[edit]</a>&nbsp;<a href='#' onClick=\"if(confirm('Удалить??')){document.location.href='./action.php?action=delete_com&id=$row->id';}return false;\" title='Удалить'>$ADMIN_ICONS[del]</a></nobr>","align=center")
		);
	$itter++;
}

$smarty->assign('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_comissions_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');
?>