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

/*PAGE TITLE*/
$page_title = "Партнерская программа";

$header = "Партнерская программа";

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

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


$table_headers = Array(
	"itter"=>Array("№","width=20 align=center"),
	"partner_id"=>Array("<a href='".$pager['new_sort']."$table".".id' title='Sort'><nobr>ID партнера</nobr></a>","align=center width=40"),
	"regdate"=>Array("<a href='".$pager['new_sort']."$table".".regdate' title='Sort'>Дата регистрации</a>","align=center"),
	"amount"=>Array("Заработал для Вас","align=center"),
);

$query = "select * from $table where ref_partner_id=$PARTNER_ID AND status=1 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 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 sum(amount) as summ from partner_transactions where partner_id=$PARTNER_ID and ref_partner_id=$row->id";
//	echo $query;
	$tmp_row = mysql_fetch_object(mysql_query($query));		  
	
	$amount = MoneyToStr(ceil($tmp_row->summ*100)/100);	  

	$table_items[$itter] = Array(
		"partner_id"=>array($row->id,"align=center"),
		"itter"=>array($itter,"align=center"),
		"regdate" => array('<nobr>'.strftime( '%d %b %Y<br><small>%H:%M:%S</small>', $row -> regdate ).'</nobr>',"align=center"),
		"amount"=>Array(($amount==0)?"-":$amount." $","align=center"),
);
	$itter++;
}

$smarty->assign('page_title',"Партнерская программа");

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

/* table template */
$smarty->assign("PARTNER_ID", $PARTNER_ID);
$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_partner_program_menu.tpl");
$smarty->assign('main_tpl',"partner_partner_program.tpl");
$smarty->assign('use_group_funcs',array_key_exists("group_funcs",$table_headers));

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