<?php include("../Header/Session.php"); ?>
<?php include("../Header/Connection.php"); ?>

<!DOCTYPE html>
<html lang="en">
<?php include ("../Header/head.php"); ?>
<body class="skin-default fixed-layout">

<!-- Main wrapper - style you can find in pages.scss -->
<div id="main-wrapper">    
<?php include ("../Header/header.php"); ?>
<?php include ("../Header/left_menu.php"); ?>
<?php include ("../Header/right_sidebar.php"); ?>
        
<!-- Page wrapper  -->
<div class="page-wrapper">
<!-- Container fluid  -->
<div class="container-fluid">
<!-- Bread crumb and right sidebar toggle -->
<!--?php include ("../Header/breadcumb.php"); ?-->
<br><br>
<!-- End Bread crumb and right sidebar toggle -->

<?php
if ($ref_code != "MGP10000" && $ref_code != "UHM11000" && $ref_code != "UHM12000" && $ref_code != "UHM13000") 
{
    echo "<script type='text/javascript'>alert('You are not authorized');</script>";
    unset($_SESSION['sess_user']);
    session_destroy();
    echo "<script type='text/javascript'> document.location='https://mygoldsdnbhd.com/log-masuk/'; </script>";
} 
?>

<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-body">
                <form method="post" action="" enctype="multipart/form-data">
                    <div class="form-body">
                        <h3 class="card-title">Member Transaction Report</h3>
                        <hr>
                        <div class="row p-t-20">
                            <div class="col-md-6">
                                    <div class="form-group">
                                        <label class="control-label">From</label>
                                        <input type="date" class="form-control" name="from_date"  required>
                                    </div>
                            </div>
                            <div class="col-md-6">
                                <div class="form-group">
                                    <label class="control-label">To</label>
                                    <input type="date" class="form-control" name="to_date"  required>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label class="control-label">Member ID</label>
                                    <input type="text" class="form-control" name="member_ref_code"  required>
                                </div>
                            </div>                 
                        </div>
                    </div>
                    <div class="form-actions">
                        <button name="Member_Selection" class="btn btn-success"><i class="fa fa-check"></i> Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>  

<?php
if (isset($_POST['Member_Selection'])) 
{
    $member_ref_code = $_POST['member_ref_code'];

    $from_date = $_POST['from_date'];
    $to_date = $_POST['to_date'];

    $from_date_display = date('d-m-Y', strtotime($_POST['from_date']));
    $to_date_display = date('d-m-Y', strtotime($_POST['to_date']));

    $sql_user = "SELECT * 
    FROM user 
    WHERE ref_code='$member_ref_code'
    And status='1'";
    $result_user = $mysqli->query($sql_user);
	if ($result_user ->num_rows > 0)
	{
		$row_user = $result_user ->fetch_assoc();
        $member_name = $row_user['name'];
	}  
}

date_default_timezone_set("Asia/Kuala_Lumpur");
$from_date_display = $from_date_display ?? date('d-m-Y');
$to_date_display   = $to_date_display   ?? date('d-m-Y');

if (empty($from_date)) $from_date = date('Y-m-d');
if (empty($to_date))   $to_date   = date('Y-m-d');

?>                
    
<!-- Start Page Content -->
<!-- ============================================================== -->
<div class="row">
<div class="col-12">
    <div class="card">
        <div class="card-body">
            <h4 class="card-title"><?php echo $member_ref_code; ?> |  <?php echo $member_name; ?> Transactions From <?php echo $from_date_display; ?> To <?php echo $to_date_display; ?></h4>
            <h6 class="card-subtitle">Export data to Copy, CSV, Excel, PDF & Print</h6>
            <div class="table-responsive m-t-40">
                                
                <table id="example23"
                    class="display nowrap table table-hover table-striped table-bordered"
                    cellspacing="0" width="100%">
                    <thead>
                        <tr>
                            <th style="display:none;">ID</th>
                            <th>ID</th>                           
                            <th>Agent Detail</th>
                            <th>Type Of Process</th>
                            <th>Amount</th>
                            <th>Wallet Balance</th>
                            <th>Description</th>
                            
						    <th>Date</th>
                        </tr>
                    </thead>                                      
                    <tbody>
                        <?php
                        $q = "SELECT *
                        FROM transactions
                        WHERE user_ref_code = '$member_ref_code'
                        AND created_date >= '$from_date'
                        AND created_date <= '$to_date'
                        ORDER BY id Asc";
                                            
                        $result = $mysqli->query($q);
                        if ($result->num_rows > 0)
                        {
                            while($record = $result -> fetch_assoc())
                            {    						   
						        $id = $record['id'];
                                $user_ref_code = $record['user_ref_code'];
                                $type_of_process = $record['type_of_process'];
                                $in_transactions = number_format($record['in_transactions'],2);
						        $balance = number_format($record['balance'],2);
                                $description = $record['description'];
                                $created_date = $record['created_date'];
                                //$created_date = date('d-m-Y', strtotime($created_date));
						        $status = $record['status'];

                                $sql_user = "SELECT * FROM user 
                                WHERE ref_code='$user_ref_code'
                                And status='1'";
                                $result_user = $mysqli->query($sql_user);
	                            if ($result_user ->num_rows > 0)
	                            {
		                            $row_user = $result_user ->fetch_assoc();
		                            $type_of_user = $row_user['type_of_user'];
                                    $name = $row_user['name'];
                                    $last_name = $row_user['last_name'];
                                    $email = $row_user['email'];
                                    $phone_number = $row_user['phone_number'];
	                            }  
                        ?>
                        <form method="post" action="../Controller/Database_sql.php" enctype="multipart/form-data">
                        <tr>
                            <td style="display:none;"><?php echo $id;?></td>
                            <td>COM00<?php echo $id;?>
                            <td><?php echo $user_ref_code;?> | <?php echo $name;?> | <?php echo $type_of_user;?>   </td>
                            <td><?php echo $type_of_process;?></td>
                            <td>USD <?php echo $in_transactions;?></td>
                            <td>USD <?php echo $balance;?></td>
                            <td><?php echo $description;?></td>
                            <td><?php echo $created_date;?></td>
                        </tr>
                        <?php 
                        }
                        }
                        ?>
                    </tbody>
                    </table>
                </form>
                                            
            </div>
        </div>
    </div>                      
</div>
</div>
<!-- ============================================================== -->
<!-- End PAge Content -->

</div>
<!-- End Container fluid  -->
</div>
<!-- End Page wrapper  -->

<!-- Footer Start-->
<?php include ("../Footer/footer.php"); ?>
<!-- Footer End-->

</div>
<!-- End Wrapper -->
<!-- All Jquery -->
    

<!-- All Jquery Start-->
<?php include ("../Header/all_jquery.php"); ?>
<!-- All Jquery End-->

</body>

</html>