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

/*PAGE TITLE*/
$page_title = "Список обменов";

$header = "Список обменов";

$table = "orders";
/* pager start */
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$params['query'] = "select count(*) from $table where partner_id=$PARTNER_ID";

$pager = GetPagerParams($params);
/* pager end */


$table_headers = Array(
	"itter"=>Array("№","width=20 align=center"),
	"id"=>Array("<a href='".$pager['new_sort']."$table".".id' title='Sort'><nobr>ID обмена</nobr></a>","align=center width=40"),
	"from_amount"=>Array("<a href='".$pager['new_sort']."$table".".amount_from ' title='Sort'>Отдал</a>","align=center"),
	"to_amount"=>Array("<a href='".$pager['new_sort']."$table".".amount_to' title='Sort'>Получил</a>","align=center"),
	"system_amount"=>Array("<a href='".$pager['new_sort']."$table".".partner_amount' title='Sort'>Системе</a>","align=center"),
	"p_amount"=>Array("<a href='".$pager['new_sort']."$table".".partner_amount' title='Sort'>Партнеру</a>","align=center"),
	"status"=>Array("<a href='".$pager['new_sort']."$table".".status' title='Sort'>Статус</a>","align=center"),
	"date"=>Array("<a href='".$pager['new_sort']."$table".".regdate' title='Sort'>Дата</a>","align=center"),
);

$query = "select $table.*, currencies.symbol as symbol from $table inner join currencies on currencies.id = $table.partner_currency_id  where $table.partner_id=$PARTNER_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;
//echo $query;
//echo "<BR><BR>";
//echo mysql_error();
while ($row = mysql_fetch_object($res)){

	switch($row->status){
		case 0:
			$s= $ADMIN_ICONS[status_progress];
		break;
		case 1:
			$s= $ADMIN_ICONS[status_on];
		break;
		case 2:
			$s= $ADMIN_ICONS[status_error];
		break;
		case 3:
			$s= $ADMIN_ICONS[status_refund];
		break;
		case 4:
			$s= $ADMIN_ICONS[status_chargeback];
		break;
	}

   	  $amount = MoneyToStr($row->amount);
	  $amount = ($row->amount>0?"<font color=green>+":"<font color=red>").$amount." ".$row->currency_symbol."</font>";
	  $rest = ($row->rest_amount>0?"<font color=green>+":"<font color=red>").MoneyToStr(round($row->rest_amount,2))." ".$row->rest_curr_symbol."</font>";
	  
	  if($row->p_click_id>0){
	  	$click = "<a href='#' title='Просмотреть инфо' onClick='ip_info(\"$row->ip\");return false;'>$row->ip</a>"."<br>Дата: ".strftime("%d %b %Y  %H:%M:%S",$row->cl_date);
	  }
	  else{
	  	$click = "<center>-</center>";
	  }

	
	$query = "SELECT name FROM ex_currencies WHERE id = $row->ex_currency_from_id";
	$from_row = mysql_fetch_object(mysql_query($query)); 
	$query = "SELECT name FROM ex_currencies WHERE id = $row->ex_currency_to_id";
	$to_row = mysql_fetch_object(mysql_query($query)); 
	
	/*$values_arr = CountOut($row->amount_from,$row->ex_currency_from_id,	
																 $row->ex_currency_to_id);
																 
	$system_amount = $values_arr['ex_from_com'];	 */ 

	$table_items[$itter] = Array(
		"id"=>array($row->id,"align=center"),
		"itter"=>array($itter,"align=center"),
		"click"=>Array($click),
		"from_amount"=>Array("<nobr>".round($row->amount_from,2)." ".$from_row->name, "align=center"),
		"to_amount"=>Array("<nobr>".round($row->amount_to,2)." ".$to_row->name, "align=center"),
		"system_amount"=>Array("<nobr>".round($row->sys_comission,2)." ".$from_row->name, "align=center"),
		"order"=>Array($row->order_id>0?$row->order_id:"-","align=center"),
		"p_amount"=>Array($row->partner_amount." ".$row->symbol,"align=center"),
		"status"=>Array($s,"align=center"),
		"date"=>Array(strftime("%d %b %Y<br><small>%H:%M:%S</small>",$row->regdate),"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',"90%");
$smarty->assign('top_menu_tpl',"partner_orders_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('partner_index.tpl');
?>