<?php
//ini_set("display_errors",1);

include('../includes/site_config.php');
include('../session_handler.inc');
include('../lib/lib.Functions.php');
include('paginate.php');
include_once __INCLUDE_CLASS_PATH."/class.ItemManager.php";
$option_manager = new OptionManager();
$mail=$option_manager->GetValue('mail');
$CONST_AFFMAIL=$mail;
# retrieve the template
$area = 'member';

if(isset($_REQUEST['rID'])){
$rID=form_get('rID');
$que=mysql_query("DELETE from affiliates where MD5(aff_userid)='$rID'");
}

if (isset($_GET['mode'])) $mode=$_GET['mode'];

$rdoApprove=$_POST['rdoApprove'];
if ($mode=='update') {

	$rdoApprove=$_POST['rdoApprove'];
	$txtAffiliate=$_POST['txtAffiliate'];
	$txtUsername=$_POST['txtUsername'];
	$WPassword=$_POST['WPassword'];
	$WAuthEmail=$_POST['WAuthEmail'];
	$reason = form_get('reason');

	//$reason=mysql_escape_string($reason);

	if ($rdoApprove=='Approve') {
		# approved will show up in search
		$query="UPDATE affiliates SET aff_approved=1 WHERE aff_userid=$txtAffiliate";
		mysql_query($query,$link);
		if (!mysql_query($query,$link)) {error_page(mysql_error(),"System Error");}
		$message=GENERAL_DEAR." $txtUsername,\n\n".AFF_AUTHORISE_MAIL_BODY.$CONST_COMPANY.AFF_AUTHORISE_MAIL_BODY1." http://$CONST_URL/affiliates/\n\n".AFF_AUTHORISE_USERNAME.": $txtUsername\n".AFF_AUTHORISE_PASSWORD.": $WPassword\n\n".AFF_AUTHORISE_MAIL_BODY2." $CONST_AFFMAIL\n\n".AFF_AUTHORISE_ADMIN;
		send_mail ("$WAuthEmail", "$CONST_AFFMAIL", "$CONST_COMPANY ".AFF_AUTHORISE_SUBJECT_APPROVAL, "$message","text","ON");
	} else {
		# rejected will not show up in search or for approval until user amends
		$query="UPDATE affiliates SET aff_approved=2 WHERE aff_userid=$txtAffiliate";
	mysql_query($query,$link);
		if (!mysql_query($query,$link)) {error_page(mysql_error(),"System Error");}
		$message=GENERAL_DEAR." $txtUsername,\n\n ".AFF_AUTHORISE_MAIL_BODY_UNAPPROVED." $CONST_COMPANY ($CONST_URL) ".AFF_AUTHORISE_MAIL_BODY1_UNAPPROVED."\n $reason \n\n".AFF_AUTHORISE_MAIL_BODY2_UNAPPROVED." $CONST_AFFMAIL\n\n".AFF_AUTHORISE_ADMIN;
		$message=stripslashes($message);
		send_mail ("$WAuthEmail", "$CONST_AFFMAIL", "$CONST_COMPANY ".AFF_AUTHORISE_SUBJECT_UNAPPROVAL, "$message","text","ON");
	}
}

# retrieve the affiliate
$query="SELECT * FROM affiliates WHERE aff_approved=0 LIMIT 1";
$retval=mysql_query($query,$link) or die(mysql_error());
$TOTAL = mysql_num_rows($retval);

# if nothing is returned then show error otherwise get data
if ($TOTAL > 0) {
	
	$sql_array = mysql_fetch_object($retval);
}


//mysql_close($link);

?>
<? include("include/header.php");?>
      <!-- form begins here -->
<? include("banner_header.php");?>
<div class="mainwidth">
<div class="admin-page-header"><div class="admin-page-heading">&nbsp;&nbsp;<?=APPR_AFFILIATE?></div></div>
<div class="clear"></div>
<div class="admin-page">
<table width="<?php print("$CONST_TABLE_WIDTH"); ?>" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">



  <tr>
    <td align="right">
      <?php // require_once("$CONST_INCLUDE_ROOT/user_status.inc.php");?>
    </td>
  </tr>
  
<? if ($TOTAL > 0) { ?>
  <tr>

    <td><table width="100%"  border="0" cellpadding="<?php print("$CONST_SUBTABLE_CELLPADDING"); ?>" cellspacing="<?php print("$CONST_SUBTABLE_CELLSPACING"); ?>">
        <form method="post" action="<?=$_SERVER['PHP_SELF']?>?mode=update" name="FrmAffiliate">
      	  <input type='hidden' name='txtAffiliate' value="<?php print("$sql_array->aff_userid"); ?>">
      	  <input type='hidden' name='txtUsername' value="<?php print("$sql_array->aff_username"); ?>">
      	  <input type='hidden' name='WPassword' value="<?php print("$sql_array->aff_password"); ?>">
      	  <input type='hidden' name='WAuthEmail' value="<?php print("$sql_array->aff_email"); ?>">
<tr>

    <td ><?php echo AFF_AUTHORISE_CHECK?></td>
  </tr>
          <tr>

            <td colspan="2" align="left" valign="top" class="tdhead">&nbsp;</td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo GENERAL_USERNAME?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_username"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_SURNAME?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_surname"); ?></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_FORENAME?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_forename"); ?></td>
          </tr>

          <tr>
            <td colspan="2" align="left" valign="top" class="tdfoot">&nbsp;</td>
          </tr>
          <tr>

            <td colspan="2" align="left" valign="top" class="tdhead">&nbsp;</td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_BUSINESS?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_business"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_ADDRESS?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_address"); ?></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_STREET?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_street"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_CITY?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_town"); ?></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_STATE?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_state"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_ZIP?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_zipcode"); ?></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo GENERAL_COUNTRY?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_country"); ?></td>
          </tr>

          <tr>
            <td colspan="2" align="left" valign="top" class="tdfoot">&nbsp;</td>
          </tr>
          <tr>

            <td colspan="2" align="left" valign="top" class="tdhead">&nbsp;</td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_TELEPHONE?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_telephone"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_FAX?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_fax"); ?></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_EMAIL?></td>
            <td align="left" valign="top"><?php print("$sql_array->aff_email"); ?></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"></td>
            <td align="left" valign="top"></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_WEB?></td>
            <td align="left" valign="top"><a href="<?php print("$sql_array->aff_website"); ?>" target='_blank'><?php print("$sql_array->aff_website"); ?></a></td>
          </tr>

          <tr class="tdeven">

            <td align="left" valign="top"></td>
            <td align="left" valign="top"></td>
          </tr>

          <tr class="tdodd">

            <td align="left" valign="top"><?php echo AFF_ACCOUNT_CHEQUE?>:</td>
            <td align="left" valign="top"><?php print("$sql_array->aff_payname"); ?></td>
          </tr>



          <tr align="center">

            <td colspan="2" valign="top" class="tdfoot">
		<input type="radio" name="rdoApprove" value="Approve" checked>
        <?php echo AFF_AUTHORISE_APPROVE?>
<input type="radio" name="rdoApprove" value="Reject">
        <?php echo AFF_AUTHORISE_REJECT?>&nbsp;<?php echo AFF_AUTHORISE_REASON?>
        <input type='text' name='reason' class="inputl" size='30'></td>
          </tr>

          <tr>

            <td colspan="2" align="left" valign="top"><center>
                <input type="submit" name="Submit" value="<?php echo BUTTON_UPDATE ?>" class="button">
        </center></td>
          </tr>

        </form>
      </table>
	 </td>
  </tr><? }else { ?>
  <table>
  <tr > 


            <td>&nbsp;</td>


 <td  align="left"> <font color="#CC0000" size="+2">Nothing To Approve!</font></td>



 <td align="center"></td>

          </tr><table>
  
  <? }?>

</table>
</div><div class="admin-page-bottom"></div></div>



<br /><br />
<div class="mainwidth">
<div class="admin-page-header">
  <div class="admin-page-heading">&nbsp;&nbsp;<?=APPR_AFF?></div>
</div>
<div class="clear"></div>
<div class="admin-page">

  <table width="100%" align="<?php print("$CONST_TABLE_ALIGN"); ?>" border="0" cellspacing="<?php print("$CONST_TABLE_CELLSPACING"); ?>" cellpadding="<?php print("$CONST_TABLE_CELLPADDING"); ?>">
  
  <tr>
  <td class="admin-head2" align="left">Affiliate</td>
  <td class="admin-head2" align="left">Email</td>
  <td class="admin-head2" align="left">Earned</td>
  <td class="admin-head2" align="left">Clicks</td>
  <td class="admin-head2" align="left">Profiles</td>
  <td>&nbsp;</td>
  </tr>
  
  <tr><td colspan="6">&nbsp;</td></tr>
 
     
  
  <? 
  
  $query="SELECT *, unix_timestamp(aff_joindate) AS aff_joindate FROM affiliates 
								 LEFT JOIN receipts ON(aff_userid=rec_affuserid)
								 LEFT JOIN members ON(mem_referrer=aff_userid)
								 where aff_approved='1'";
//$joindate=date($CONST_FORMAT_DATE_SHORT,$sql_array->aff_joindate);
					   
	$record_per_page='20';

$pager = new PS_Pagination($link,$query,$record_per_page,5);
$rs = $pager->paginate();					   
	while($app_Blog=mysql_fetch_object($rs)){	
	
	$query="SELECT COUNT(*) FROM members WHERE mem_referrer='$app_Blog->aff_userid'";
$retval=mysql_query($query,$link) or die(mysql_error());
$sql_array = mysql_fetch_array($retval);
$no_referred = $sql_array[0];	

$query="SELECT SUM(rec_affamount) FROM receipts WHERE rec_affuserid='$app_Blog->aff_userid'";
$retval=mysql_query($query,$link) or die(mysql_error());
$sql_array = mysql_fetch_array($retval);
$earned2date = sprintf("%.2f",$sql_array[0]);			   
  ?>
  <tr><td  width="125px"> <?=$app_Blog->aff_username?></td>
  <td><?=$app_Blog->aff_email?></td>
  <td><?=$earned2date?></td>
  <td><?=$app_Blog->aff_clickthru?></td>
  <td><?=$no_referred?></td>
  <td><? if($DEMO==false){?><a href="<?=$_SERVER['PHP_SELF']?>?rID=<?=md5($app_Blog->aff_userid)?>">delete</a><? }?></td>
  
  
  
  </tr>
  <tr><td>&nbsp;</td></tr>
  <? }?>
   <tr align="right"><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td><? echo $pager->renderNext(); ?></td></tr>
  </table>
  
        </div></div><div class="admin-page-bottom"></div></div>

<?php include("include/footer.php");  ?>