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


/*PAGE TITLE*/
$page_title = "Эл. валюты";

$header = "Эл. валюты";

$table = "ex_currencies";
/* 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>
                      <option value='./action.php?action=activate'> - Активировать</option>
                      <option value='./action.php?action=unactivate'> - Деактивировать</option>");



$table_headers = Array(
"itter"=>Array("№","width=20 align=center"),
"c_img"=>Array("",""),
"logo"=>Array("Лого",""),
"name"=>Array("<a href='".$pager['new_sort']."$table".".name' title='Sort'>Имя</a>","align=center"),
"code"=>Array("<a href='".$pager['new_sort']."$table".".code' title='Sort'>Код</a>","align=center"),
"currency"=>Array("<a href='".$pager['new_sort']."$table".".currency_id' title='Sort'>Валюта</a>","align=center"),
"metal"=>Array("<a href='".$pager['new_sort']."$table".".metal_id' title='Sort'>Металл</a>","align=center"),
"type"=>Array("<a href='".$pager['new_sort']."$table".".type' title='Sort'>Тип</a>","align=center"),
"transfare_type"=>Array("<a href='".$pager['new_sort']."$table".".transfare_type' title='Sort'>Передача</a>","align=center"),
"status"=>Array("<a href='".$pager['new_sort']."$table".".status' title='Sort'>Статус</a>","align=center width=60"),
"amount"=>Array("<a href='".$pager['new_sort']."$table".".amount' title='Sort'>Остаток</a>","align=center"),
"funcs"=>Array("Операции","width=5% align=center"),
"group_funcs"=>Array("<select name=act_sel onChange='SubmitForm();'><option>Выберите действие<option value='./action.php?action=delete'>-Удалить<option value='./action.php?action=activate'>-Активировать<option value='./action.php?action=unactivate'>-Деактивировать</select>"));

$query = "
  select 
ex_currencies.*, currencies.name as curr,currencies.code as curr_code, count(pincodes.id) as pincount, 
metals.name as metal, pincode_collections.name as collection_name, pincode_collections.sell_price, pincurrencies.name as pincode_curr, pincurrencies.code as pincode_currcode
from 
(ex_currencies left outer join currencies on currencies.id = ex_currencies.currency_id) 
left outer join metals on metals.id = ex_currencies.metal_id ".
"left join pincode_collections on pincode_collections.id=ex_currencies.pincode_collection_id ".
"left join currencies as pincurrencies on pincurrencies.id=pincode_collections.currency_id ".
"left join pincodes on (pincodes.collection_id=ex_currencies.pincode_collection_id and pincodes.status='valid') "
.($search_string?" where $search_string":"")
." GROUP BY ex_currencies.id"
." 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->type == "pincode")
	{
		$rest = "<a href=\"./balance_hist.php?id=$row->id\" title=\"Посмотреть историю\"><nobr>".((integer)$row->pincount * (double)$row->sell_price)." ".$row->pincode_currcode . "</nobr></a>";
	}
	else
	{
		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></a>";
		}
	}

	$table_items[$itter] = Array(
	"itter"=>array($itter,"align=center"),
	"id"=>Array($row->id,"align=center"),
	"c_img"=>Array($c_img,"align=center"),
	"logo"=>Array($ex_img,"align=center"),
	"name"=>Array("<a href=\"$row->url\">$row->name</a>"),
	"code"=>Array($row->code, ""),
	"currency"=>array($row->type == "pincode" ? $row->pincode_curr : $row->curr, ""),
	"metal"=>array($row->metal, ""),
	"transfare_type"=>array($row->transfare_type == 'manual' ? "Вручную". " (Реквизиты: " . $row->manual_properties . ")" : ($row->transfare_type == 'auto' ? "Автоматически" : "ПИН-код"), "align=center"),
	"type"=>array($row->type == 'ordinary' ? "Обычная" : "ПИН-код" . "<br>(" . $row->collection_name . ")", "align=center nowrap"),
	"amount"=>array($rest . ($row->type == 'pincode' ? "<BR>".$row->pincount." шт." : ""), "align=center"),
	"status"=>Array($s,"align=center"),
	"funcs"=>Array("<nobr><a href='./add.php?id=$row->id' title='Редактировать'>$ADMIN_ICONS[edit]</a>&nbsp;<a href='#' onClick=\"if(confirm('Удалить??')){document.location.href='./action.php?action=delete&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_excurr_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');
?>