<?

require_once("../dasar/session.php");
require_once("conn.php");
include '../dasar/ubahtgl.php';
$menu6='active';

$kodeup = $_SESSION['kode'];
$markup_u = $_SESSION['markup'];
if(isset($_POST['limit']))
{
    $limit = $_POST['limit'];
}
else if(isset($_GET['limit']))
{
    $limit=$_GET['limit'];
}
else
{
	 $limit=20;
}

if(isset($_POST['cari']))
{
    $cari = $_POST['cari'];
    
}
else
{
	 $cari='';
}

$idate=date('Y-m-d');

$btn=0;
if(isset($_GET['dw']))
{
$kode = base64_decode($_GET['dw']);
$dw=$_GET['dw'];  	
    	}
else
{
	$kode=$kodeup;
	$dw=base64_encode($kodeup);
	$btn=1;
}

if(empty($kodeup))
{
     $kodeup='XXXXXXXX';
     $kode='XXXXXXXX';
}

$getmarkup = mysqli_query($conn,"SELECT * FROM reseller where kode = '$kodeup'");
 while($record = mysqli_fetch_array($getmarkup)){
              
        $markup_u = $record['markup'];
        $level=$record['kode_level'];
    $kodeup=$record['kode_upline'];  
    	
     
 }
 
$exel = mysqli_query($conn,"SELECT * FROM level where kode = '$level'");
while($record = mysqli_fetch_array($exel)){
    $markup_u = $record['selisih_harga']+$markup_u;
    }


if(isset($_GET['awal']))
{
$noPage = $_GET['awal'];
}
else
{
	$noPage=1;
}
$offset = ($noPage - 1) *$limit;

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="description" content="">
        <meta name="author" content="">

        <title>Downline - Web Report</title>

        <!-- Bootstrap Core CSS -->
        <link href="../css/bootstrap.min.css" rel="stylesheet">

        <!-- MetisMenu CSS -->
        <link href="../css/metisMenu.min.css" rel="stylesheet">

        <!-- DataTables CSS -->
        <link href="../css/dataTables/dataTables.bootstrap.css" rel="stylesheet">

        <!-- DataTables Responsive CSS -->
        <link href="../css/dataTables/dataTables.responsive.css" rel="stylesheet">
 <link rel="icon" href="../favicon.ico" type="image/icon"/>
        <!-- Custom CSS -->
        <link href="../css/startmin.css" rel="stylesheet">

        <!-- Custom Fonts -->
        <link href="../css/font-awesome.min.css" rel="stylesheet" type="text/css">

        <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
        <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

		<style>
.pagination {
    display: inline-block;
}

.pagination a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
    transition: background-color .3s;
}

.pagination a.active {
    background-color: #4CAF50;
    color: white;
}

.pagination a:hover:not(.active) {background-color: #ddd;}
</style>

<script>
function goBack() {
    window.history.back();
}
</script>

    </head>
    <body>

        <div id="wrapper">

            <?
			require_once("nav.php");
			?>

            <div id="page-wrapper">
                <div class="row">
                    <div class="col-lg-12">
                        <h1 class="page-header">Downline</h1>
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
				
                <!-- /.row -->
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel panel-red">
                            <div class="panel-heading">
                                <i class="fa fa-sitemap fa-fw"></i>&nbsp;&nbsp;Data Downline Anda
                            </div>
                            <!-- /.panel-heading -->
                            <div class="panel-body">
                                <div class="dataTable_wrapper" style="overflow-x:auto;">
									<form name='loginform' method='post' action='downline.php?dw=<? echo $dw; ?>'>
									
									Reseller / Limit&nbsp;&nbsp;<input type="text" name='cari' value="<? echo $cari; ?>">&nbsp;&nbsp;<input type="text" name='limit' value="<? echo $limit; ?>" style="width:50px;" />&nbsp;&nbsp;
									<button type="submit" class="btn btn-primary" name='tampilkan' value='Tampilkan'>Lihat</button><br><br>
									</form>
                                    <table class="table table-bordered table-hover">
                                        <thead>
                                            <tr>
                                                <th><center>No</center></th>
                                                <th><center>Kode Reseller</center></th>
                                                <th><center>Nama Reseller</center></th>
                                                <th><center>Markup Rp</center></th>
                                                <th><center>Saldo Rp</center></th>
												<th><center>Status</center></th>
												<th><center>Aktivitas Terakhir</center></th>
                                            </tr>
                                        </thead>
										<?
										$exe = mysqli_query($conn,"SELECT * FROM reseller where kode_upline = '$kode' and (kode like '%$cari%' or nama like '%$cari%') order by kode asc limit $offset,$limit");
										$jml = mysqli_query($conn,"SELECT * FROM reseller where kode_upline = '$kode' and (kode like '%$cari%' or nama like '%$cari%')");	
					
										$jumlah=mysqli_num_rows($jml);

				       					$no = 1+$offset;
										while($record = mysqli_fetch_array($exe)){
                       
                                            $kode = $record['kode'];
                                            $kodestr=base64_encode($kode);
                                            $kode_level2 = $record['kode_level'];
                    						$nama = $record['nama'];
                    						$saldo = $record['saldo'];
                    						$markup_d = $record['markup'];
                    						$aktif = $record['aktif'];
                    						$markup=$markup_d-$markup_u;
                    						
                    						$recordsaldo=0;
                    						$exesaldo = mysqli_query($conn,"SELECT * FROM mutasi where kode_reseller = '$kode' order by kode desc");
                    						$recordsaldo = mysqli_fetch_array($exesaldo);
                    						if ($recordsaldo>0)
                    						{
                    						    $saldo=$recordsaldo['saldo_akhir'];
                    						}
                    						
                    						
								        	if ($aktif == "1")
                    							{
                    								$stt = "Aktif";
                    							}
                    							else {$stt = "Non-Aktif";}
                                
                    						$aktifitas = DateToIndo($record['tgl_aktivitas']);
                    						//$area = $record['area'];
                    						$saldo2=number_format($saldo,0,',','.');

                                        ?><tbody>
                                            <tr>
                                                <td><center><? echo $no ;?></center></td>
                                                <td><center><a href="downline.php?dw=<? echo $kodestr; ?>&&limit=<? echo $limit; ?>"><? echo $kode ;?></a></center></td>
                                                <td><center><? echo $nama ;?></center></td>
                                                <td><center><? echo $markup ;?></center></td>
                                                <td><center><? echo $saldo2 ;?></center></td>
                                                <td><center><? echo $stt ;?></center></td>
                                                <td><center><a href="transaksi_downline.php?kode=<? echo $kodestr; ?>&&limit=<? echo $limit; ?>"><? echo $aktifitas ;?></center></td>
                                                
                                            </tr>
                                        </tbody>

											<?$no++;}?>
                                    </table>
									<div class="pagination">
										
									<?$i=$jumlah/$limit;
									$i=ceil($i);
									$showPage=0;
									if ($noPage > 1) echo "<a href='".$_SERVER['PHP_SELF']."?awal=".($noPage-1)."&&limit=".$limit."&&dw=".$dw."'> Prev</a>";
									for($j = 1; $j <= $i; $j++)
									{
										if ((($j >= $noPage - 3) && ($j <= $noPage + 3)) || ($j == 1) || ($j == $i))
										{
											if (($showPage == 1) && ($j != 2))  echo "<a>...</a>";
											if (($showPage != ($i - 1)) && ($j == $i))  echo "<a >...</a>";
											if ($j == $noPage) echo ' <a class="active">'.$j."</a> ";
												else echo " <a href='".$_SERVER['PHP_SELF']."?awal=".$j."&&limit=".$limit."&&dw=".$dw."'>".$j."</a> ";
											$showPage = $j;
											}
										}
									if ($noPage < $i) echo "<a href='".$_SERVER['PHP_SELF']."?awal=".($noPage+1)."&&limit=".$limit."&&dw=".$dw."'>Next</a>";

									?>
									
										
									</div> 
									<?
									if ($btn==0){
									    echo '<br><br><button onclick="goBack()" class="btn btn-primary"">Kembali</button>';
								    }
									?>
                                </div>
                                
                            </div>
                            <!-- /.panel-body -->
                        </div>
                        <!-- /.panel -->
                    </div>
                    <!-- /.col-lg-12 -->
                </div>
                
                <!-- /.row -->
            </div>
            <!-- /#page-wrapper -->

        </div>
        <!-- /#wrapper -->

        <!-- jQuery -->
        <script src="../js/jquery.min.js"></script>

        <!-- Bootstrap Core JavaScript -->
        <script src="../js/bootstrap.min.js"></script>

        <!-- Metis Menu Plugin JavaScript -->
        <script src="../js/metisMenu.min.js"></script>

        <!-- DataTables JavaScript -->
        <script src="../js/dataTables/jquery.dataTables.min.js"></script>
        <script src="../js/dataTables/dataTables.bootstrap.min.js"></script>

        <!-- Custom Theme JavaScript -->
        <script src="../js/startmin.js"></script>

        <!-- Page-Level Demo Scripts - Tables - Use for reference -->
        <script>
            $(document).ready(function() {
                $('#dataTables-example').DataTable({
                        responsive: true
                });
            });
        </script>
    <?
        include 'chat.php';
    ?>
    </body>
</html>
