<?
define("ADMIN_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;
$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"),
	"from_to"=>Array("&nbsp;","width=20 align=center"),
	"num"=>Array("#","width=20 align=center"),
	"name"=>Array("<a href='".$pager['new_sort']."$table".".name' title='Sort'>Валюта</a>",""),
	"code"=>Array("<a href='".$pager['new_sort']."$table".".from_code' title='Sort'>Код</a>",""),
	"amount_from"=>Array("<a href='".$pager['new_sort']."$table".".from_name' title='Sort'>Перев</a>",""),
	"amount_to"=>Array("Получ",""),
	"comission"=>Array("Ком-я",""),
	"sys"=>Array("Сис",""),
	"batch"=>Array("<a href='".$pager['new_sort']."$table".".name' title='Sort'>Batch</a>",""),
	"status_fromto"=>Array("Статус",""),
	"date"=>Array("<a href='".$pager['new_sort']."$table".".regdate' title='Sort'>Дата</a>","align=center width=60"),	
	"status"=>Array("<a href='".$pager['new_sort']."$table".".status' title='Sort'>Статус</a>","align=center width=60"),
	//"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 orders.*, 
ex_currencies.code as from_code, curr2.code as to_code, 
ex_currencies.name as from_name, curr2.name as to_name 
from 
(orders left outer join ex_currencies on ex_currencies.id = orders.ex_currency_from_id) 
left outer join ex_currencies curr2 on curr2.id = orders.ex_currency_to_id ".
  ($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:
			$status = "<font color=green>OK</font>";
			break;
		case 1:
			$status = "<font color=blue><b>В процессе</b></font>";
			break;
		case 2:
			$status = "<font color=red>Ошибка</font>";
			break;
		default:
			$status = "неизвестно";
			break;
	}
	switch($row->from_status){
		case 0:
			$f_status = "<font color=green>OK</font>";
			break;
		case 1:
			$f_status = "<font color=blue><b>В процессе</b></font><br><small><a href='./check.php?id=$row->id'>проверить</a></small>";
			break;
		case 2:
			$f_status = "<font color=red>Ошибка</font>";
			break;
		default:
			$f_status = "неизвестно";
			break;
	}

	switch($row->to_status){
		case 0:
			$to_status = "<font color=green>OK</font>";
			break;
		case 1:
			$to_status = "<font color=blue><b>В процессе</b></font>";
			break;
		case 2:
			$to_status = "<font color=red>Ошибка</font>";
			break;
		default:
			$to_status = "неизвестно";
			break;
	}

	$date = getdate($row->regdate);
	$day = $date['mday'];
	$month = $date['mon'];
	$year = $date['year'];
	$h = $date['hours'];
	$m = $date['minutes'];
	$s = $date['seconds'];
	$year = $date['year'];
	$date = $h.":".$m.":".$s."<br>".$day." ".$MONTH_RUS[$month-1]." ".$year;
	
	/*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;
	}
	*/
	$table_items[$itter] = Array(		
		"itter"=>array($itter,"align=center"),
		"from_to"=>array(from_to("<B>Из</B>", "<B>В</B>"), ""),
		"num" => array(from_to(
						"<a href=\"#\" onClick=\"order_info('$row->from_order_id','$row->from_code','$row->id'); return false;\">$row->from_order_id
						</a>","
						<a href=\"#\" onClick=\"order_info('$row->to_order_id','$row->to_code','$row->id'); return false;\">$row->to_order_id</a>"),
						""),
		"name" => array(from_to($row->from_name, $row->to_name),""),		
		"code" => array(from_to($row->from_code, $row->to_code), ""),
		"amount_from" => array(from_to(floatval($row->amount_from).($row->metal_amount_from > 0 ? "<br><small>$row->metal_amount_from гр.</small>":""),
							           ($row->amount_to+$row->ex_currency_to_comission+0).($row->metal_amount_to+$row->metal_ex_currency_to_comission > 0 ? "<br><small>".($row->metal_amount_to+$row->metal_ex_currency_to_comission+0)." гр.</small>":"")),""),
		"amount_to" => array(from_to(floatval($row->amount_from-$row->ex_currency_from_comission).($row->metal_amount_from > 0 ? "<br><small>".($row->metal_amount_from-$row->metal_ex_currency_from_comission)." гр.</small>":""),
									($row->amount_to+0).($row->metal_amount_to > 0 ? "<br><small>$row->metal_amount_to гр.</small>":"")), ""),									
		"comission" => array(from_to(floatval($row->ex_currency_from_comission).($row->metal_ex_currency_from_comission > 0 ? "<br><small>$row->metal_ex_currency_from_comission гр.</small>":""), 
									 floatval($row->ex_currency_to_comission).($row->metal_ex_currency_to_comission > 0 ? "<br><small>$row->metal_ex_currency_to_comission гр.</small>":"")), ""),
		"sys" => array(from_to(($row->sys_comission+0).($row->metal_sys_comission > 0 ? "<br><small>$row->metal_sys_comission гр.</small>":"")."<br><small>".($row->amount_from-$row->ex_currency_from_comission>0?round($row->sys_comission/($row->amount_from-$row->ex_currency_from_comission)*100,3):0)."%</small>",
								0), ""),
		"batch" => array(from_to($row->batch_from==""?"-":$row->batch_from, 
						($row->batch_to==""?($row->to_status==1&&$row->from_status==0?"<a href=\"#\" onClick=\"pay('$row->id'); return false;\" title=\"Перевести деньги\">заплатить</a>":"-"):$row->batch_to)), ""),
		"status_fromto" => array(from_to($f_status, $to_status),
						""),
		"date" => array($date."<br><a href=\"#\" onClick=\"ip_info('$row->ip'); return false;\"><small>$row->ip</small></a>", "",),
		"status"=>Array($status,"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++;
}

function from_to($from, $to)
{
	return "
	<table height=\"60px\" >
		<tr>
			<td>
				$from
			</td>
		</tr>
		<tr>
			<td>
				$to
			</td>
		</tr>
	</table>";
}

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

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

//$smarty->assign('status',$status);

/* 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_orders_menu.tpl");
$smarty->assign('main_tpl',"admin_orders.tpl");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

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