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



/*PAGE TITLE*/
$page_title = "Направления обмена";

$header = "Направления обмена";

$table = "ex_exchanges";

$ex1_id = intval($_GET['ex1_id']==""?$_POST['ex1_id']:$_GET['ex1_id']);
$ex2_id = intval($_GET['ex2_id']==""?$_POST['ex2_id']:$_GET['ex2_id']);

$search_string = "";
if (0 < $ex1_id)
{
	$search_string = " ex_exchanges.ex_currency1_id = $ex1_id ";
}
if (0 < $ex2_id)
{
	if ("" != $search_string)
	{
		$search_string .= " AND ";
	}
	$search_string .= " ex_exchanges.ex_currency2_id = $ex2_id ";
}

$ex_currs1 = array(array("id" => 0,
						"name" => "Все",
						"selected" => ($ex1_id==0)?"selected=\"selected\"":"",
						));
$ex_currs2 = array(array("id" => 0,
						"name" => "Все",
						"selected" => ($ex2_id==0)?"selected=\"selected\"":"",
						));

$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 order by ex_currencies.name");
while ($ex_curr_row = mysql_fetch_object($ex_curr_res))
{
	$ex_currs1[] = array("id" => $ex_curr_row->id,
						"name" => $ex_curr_row->name,
						"selected" => ($ex1_id==$ex_curr_row->id)?"selected=\"selected\"":"",
						);
	
	$ex_currs2[] = array("id" => $ex_curr_row->id,
						"name" => $ex_curr_row->name,
						"selected" => ($ex2_id==$ex_curr_row->id)?"selected=\"selected\"":"",
						);
}
$smarty->assign("ex_currs1", $ex_currs1);
$smarty->assign("ex_currs2", $ex_currs2);

/* pager start */
$params['query']=
"select count(*)  
from 
(ex_exchanges left outer join ex_currencies curr on curr.id = ex_exchanges.ex_currency1_id) 
left outer join ex_currencies on ex_currencies.id = ex_exchanges.ex_currency2_id  ".
	($search_string?" where $search_string":"");
$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"),
	"ex_currency1_id"=>Array("<a href='".$pager['new_sort']."$table".".ex_currency1_id' title='Sort'>Из</a>",""),
	"ex_currency2_id"=>Array("<a href='".$pager['new_sort']."$table".".ex_currency2_id' title='Sort'>В</a>",""),
	"course"=>Array("<a href='".$pager['new_sort']."$table".".course' title='Sort'>Курс</a>",""),
	"course_up_date"=>Array("<a href='".$pager['new_sort']."$table".".course_up_date' title='Sort'>Обновлен</a>",""),
	"log"=>Array("<a href='".$pager['new_sort']."$table".".course_up_method' title='Sort'>Лог</a>",""),
	"min_amount"=>Array("<a href='".$pager['new_sort']."$table".".min_amount' title='Sort'>Мин. сумма</a>",""),
	"max_amount"=>Array("<a href='".$pager['new_sort']."$table".".max_amount' title='Sort'>Макс. сумма</a>",""),
	"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 ex_exchanges.*, ex_currencies.name as curr2, 
curr.name as curr1  
from 
(ex_exchanges left outer join ex_currencies curr on curr.id = ex_exchanges.ex_currency1_id) 
left outer join ex_currencies on ex_currencies.id = ex_exchanges.ex_currency2_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:
			$s= $ADMIN_ICONS[status_off];
		break;
		case 1:
			$s= $ADMIN_ICONS[status_on];
		break;
	}
	$table_items[$itter] = Array(
		"itter"=>array($itter,"align=center"),
		"id"=>Array($row->id,"align=center"),
		"ex_currency1_id"=>array($row->curr1, "align=center"),
		"ex_currency2_id"=>array($row->curr2, "align=center"),
		"course"=>array(floatval($row->course), "align=center"),
		"course_up_date"=>array(strftime("%d %b %Y<br>%H:%M",$row->course_up_date), "align=center"),
		"log"=>array(stripslashes($row->course_up_method), "align=left"),
		"min_amount"=>array(floatval($row->min_amount), "align=center"),
		"max_amount"=>array(floatval($row->max_amount), "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_exchange_menu.tpl");
$smarty->assign('main_tpl',"admin_list_table.tpl");
$smarty->assign('custom_query',"ex1_id=$ex1_id&ex2_id=$ex2_id");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

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