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

$m_id = intval($_GET['m']);
if (0 > $m_id)
{
	$m_id = 0;
}

if (0 != $m_id)
{
	$search_string = "merchant_id = $m_id";
}


/*PAGE TITLE*/
$page_title = "Заказы";

$header = "Заказы";

$table = "merchant_orders";
/* pager start */
$params['tables'] = $table;
$params['nocheckfield']=true;
$params['s_prefix'] = $table;
$params['query'] = "select count(*) 
from 
($table left outer join ex_currencies on ex_currencies.id = $table.ex_currency_from_id) 
left outer join ex_currencies curr2 on curr2.id = $table.ex_currency_to_id ".
"INNER JOIN merchant_users on $table.merchant_id=merchant_users.id".
  ($search_string?" where $search_string":"");
$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"),
	"merchant"=>Array("<a href='".$pager['new_sort']."$table".".merchant_id' title='Sort'>Продавец</a>",""),
	"email"=>Array("E-mail плательщика",),
	"amount"=>Array("<a href='".$pager['new_sort']."$table".".amount_from' title='Sort'>Сумма</a>",""),
	"description"=>Array("<a href='".$pager['new_sort']."$table".".description' title='Sort'>Описание</a>",""),
	"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"),
	);

$query = "
  select $table.*, 
ex_currencies.code as from_code, curr2.code as to_code, 
ex_currencies.name as from_name, curr2.name as to_name,
merchant_users.name, merchant_users.last_name, merchant_users.p_name, merchant_users.email as merchant_email
from 
($table left outer join ex_currencies on ex_currencies.id = $table.ex_currency_from_id) 
left outer join ex_currencies curr2 on curr2.id = $table.ex_currency_to_id ".
"INNER JOIN merchant_users on $table.merchant_id=merchant_users.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.", ".$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"),
		"email"=>array("<a href='mailto:$row->email'>$row->email</a>"),
		"description" => array(stripslashes(htmlspecialchars(($row->description)))),
		"merchant" => array("<strong><nobr>".htmlspecialchars(stripslashes($row->last_name.' '.$row->name.' '.$row->p_name))." [id $row->merchant_id]</nobr></strong><br><a href='mailto:$row->merchant_email'>$row->merchant_email</a>"),
		"amount" => array("<nobr>".floatval($row->amount_to)." <b>".$row->to_code."</b></nobr><br><nobr><small>(".floatval($row->amount_from)." <b>".$row->from_code."</b>)</small></nobr>", "title=\"\" align=\"center\""),
		"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++;
}

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

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

$merchant_users = array(array("id" => 0, "name" =>"Все продавцы"));
$res = mysql_query("SELECT * FROM merchant_users");
while ($row = mysql_fetch_object($res))
{
	$merchant_users[] = 
		array(
		'id' => $row->id,
		'selected' => ($m_id == $row->id)?"selected":"",
		'name' => htmlspecialchars(stripslashes($row->last_name.' '.$row->name.' '.$row->p_name)),
		);
}

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

/* table template */
$smarty->assign("custom_query", "m=$m_id");
$smarty->assign('pager',$pager);
$smarty->assign("merchant_users", $merchant_users);
$smarty->assign('table_headers',$table_headers);
$smarty->assign('table_items',$table_items);
$smarty->assign('table_width',"100%");
$smarty->assign('top_menu_tpl',"merchant/admin_merchant_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');
?>