<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
global $configverssion_id; global $configapp_version; global $userID; global $server; global $LANG; $resellerID = getReseller();
//=== List page stuffs =============
$page = 1; $setLimit = 30;
if(isset($_GET["page"])) $page = (int)$_GET["page"];
$pageLimit = ($page * $setLimit) - $setLimit; 
$pageurl=$_SERVER['REQUEST_URI']."?";
if(count($_GET)) { $pageurl = $_SERVER['REQUEST_URI'].'&'; }
$trans = array("s=d_o" => "", "s=d_n" => "", "s=r_h" => "", "s=r_l" => "", "s=s_r" => "", "s=s_u" => "", "s=s_q" => "", "s=s_c" => "", "s=s_f" => "", "s=s_p" => "");
$pageurl = strtr($pageurl,$trans);
$pageurl = str_replace('&&','&',$pageurl);
//====End ==============
define('TITLE','Affiliate');
if(!isAdmin(getUser()) && (userData(getUser(),'phone') == '' || userData(getUser(),'first_name') == '') ) {
	header('location: profile?setting'); exit();
}
if(!isAdmin(getUser()) && userData($userID,'email_verified') != 0) {
	showVerify();
}
if(getSetting('affiliateEnabled')<1){
	header('location: index.php'); exit();
}
$today = date('Y-m-d'); $time = strtotime($today.'-1 months'); $lastMonth = date("Y-m", $time); $time = strtotime($today.'-1 years'); $lastYear = date("Y", $time);
?>
<?php

if(isset($_REQUEST['view'])&& $_REQUEST['view']>0) {
} else {
	if(empty(userData($userID,'affiliate_id'))) {
		$affliliate = genRandomPassword(16);
		mysqli_query($server, "UPDATE  `users` SET  `affiliate_id` =  '$affliliate' WHERE `id` ='$userID'");
	}
	//show list
	$added = '';	
	if(isset($_GET['s'])) {
	} else {
		$added .= " order by id DESC ";	
	}

    $query="SELECT * FROM users WHERE referrer = '$userID' $added limit $pageLimit,$setLimit";
	$result = mysqli_query($server,$query) or die(mysqli_error($server));
	$x = mysqli_num_rows($result);
	$query2="SELECT * FROM transactions WHERE customer_id = '$userID' AND is_earned = 1 $added limit $pageLimit,$setLimit";
	$result2 = mysqli_query($server,$query2) or die(mysqli_error($server));
	$x2 = mysqli_num_rows($result2);
?>
<!-- Quick Navs ::::: You can customize this -->
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card blue">
            <div class="card-content white-text"><?=$LANG['affliate_tip']?></div>
        </div>
    </div>
 </div>
 <div class="row no-m-t no-m-b">
 	<div class="col s12 m12 l12">
        <div class="card white">
            <div class="card-content"><h6><strong><?=$LANG['affiliate_link'].': <a href="'.home_base_url().'index.php?ref='.userData($userID,'affiliate_id').'">'.home_base_url().'index.php?ref='.userData($userID,'affiliate_id').'</a>'?></strong></h6></div>
        </div>
    </div>
 </div>
<div class="row no-m-t no-m-b">
 	<div class="col s12 m4 l3">
         <div class="card stats-card purple accent-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Referred Visits']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(userData($userID,'refered_visits'))?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card light-blue darken-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Referred Signups']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(refferedSignups($userID))?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card green accent-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Referred Sales']?></span>
                 <span class="stats-counter" style="color: white;"><span class="counter" style="color: white;"><?=number_format(refferedSales($userID) )?></span></span>
             </div>
          </div>
    </div>
    <div class="col s12 m4 l3">
         <div class="card stats-card light-green darken-4" >
             <div class="card-content">
                 <span class="card-title" style="color: white;"><?=$LANG['Total Commissions']?></span>
                 <span class="stats-counter" style="color: white;"><?=userCurrencySymbol($userID)?><span class="counter" style="color: white;"><?=number_format(creditEarned($userID),2)?></span></span>
             </div>
          </div>
    </div>   
 </div>

<div class="row">
	<div class="col s12">
		<ul class="tabs tab-demo z-depth-1" style="width: 100%;">
			<li class="tab col s6"><a id="sc5" href="#signs" class=""><?=$LANG['Referred Signups']?></a></li>
			<li id="bulk_cont" class="tab col s6"><a href="#rewards"><?=$LANG['Commission History']?></a></li>
		</ul>
	</div>
    <div id="signs">
        <div class="row no-m-t no-m-b">
            <div class="col s12 m12 l12">
                 <div class="card card-white">   
                    <div class="card-content">
                    
                    <table class="highlight responsive-table">
                        <thead>
                            <tr>
                                <th data-field="id"><?=$LANG['First Name']?></th>
                                <th data-field="number"><?=$LANG['Last Name']?></th>
                                <th data-field="type"><?=$LANG['Email']?></th>
                                <th data-field="cost"><?=$LANG['Joined']?></th>
                            </tr>
                         </thead>
                         <tbody>
        <?php $xf=0; while ($rows = mysqli_fetch_assoc($result)) { $xf+=1; $stats = ''; 
        ?>
                                <tr class="">
                                     <td><?=$rows['first_name']?></td>
                                     <td><?=$rows['last_name']?></td>
                                     <td><?=$rows['email']?></td>
                                     <td><?=date('M j Y',strtotime($rows['date']))?></td>
                                </tr>
        <?php } ?>
        <?php if($xf<1) echo '<tr class="orange lighten-5"><td colspan=10><h5 class="center-align">'.$LANG['No records found!'].'</h5></td></tr>' ?>	
                         </tbody>
                    </table>  
                  </div>
                </div>          
             </div>
          </div>
         <div class="row">
            <div class="col s12 m12 l12" style="margin-top: 5px;">
                <?php displayPagination($setLimit,$page,$query);  ?>
            </div>
         </div>     
    </div>                             
    <div id="rewards">
        <div class="row no-m-t no-m-b">
            <div class="col s12 m12 l12">
                 <div class="card card-white">   
                    <div class="card-content">
                    
                    <table class="highlight responsive-table">
                        <thead>
                            <tr>
                                <th data-field="id"><?=$LANG['Commision Date']?></th>
                                <th data-field="number"><?=$LANG['Description']?></th>
                                <th data-field="type"><?=$LANG['Credits']?></th>
                                <th data-field="cost"><?=$LANG['Reference']?></th>
                            </tr>
                         </thead>
                         
                         <tbody>
        <?php $xf=0; while ($rows = mysqli_fetch_assoc($result2)) { $xf+=1; $stats = ''; 
        ?>
                                <tr class="">
                                     <td><?=date('F j Y',strtotime($rows['date']))?></td>
                                     <td><?=$rows['description']?></td>
                                     <td><?=number_format($rows['credits'],4)?></td>
                                     <td><?=$rows['transaction_reference']?></td>
                                </tr>
        <?php } ?>
        <?php if($xf<1) echo '<tr class="orange lighten-5"><td colspan=10><h5 class="center-align">'.$LANG['No records found!'].'</h5></td></tr>' ?>	
                         </tbody>
                    </table>  
                  </div>
                </div>          
             </div>
          </div>
         <div class="row">
            <div class="col s12 m12 l12" style="margin-top: 5px;">
                <?php displayPagination($setLimit,$page,$query);  ?>
            </div>
         </div>     
    </div>                             

<?php } ?> 
