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

<?php
$comm_type = $_GET['comm_type'];

if ($comm_type == "Bonus_Sponsor")
{
    $title = 'Bonus Sponsor';
    $type_of_process = "bonus_sponso";
    $currency_label = "USD";
}
else
if ($comm_type == "Hibah_Unilevel")
{
    $title = 'Hibah Unilevel';
    $type_of_process = "hibah_unilevel_level_";
    $currency_label = "USD";
}
else
if ($comm_type == "Gold_Sales")
{
    $title = 'Gold Sales';
    $type_of_process = "Gold Sales Commission";
    $currency_label = "RM";
}
else
if ($comm_type == "Gold_Buy_Back")
{
    $title = 'Gold Buy Back';
    $type_of_process = "Gold Buy Back Commission";
    $currency_label = "RM";
}
?>

<!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 -->
    
<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">Date Selection</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>
                    </div>
                    <div class="form-actions">
                        <button name="Date_Selection" class="btn btn-success"><i class="fa fa-check"></i> Submit</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>  

<?php
if (isset($_POST['Date_Selection'])) 
{
    $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']));
}

date_default_timezone_set("Asia/Kuala_Lumpur");
$default_from_date = date('Y-m-01');
$default_to_date   = date('Y-m-t');

if (isset($_POST['Date_Selection'])) {
    $from_date = $_POST['from_date'];
    $to_date   = $_POST['to_date'];

    $from_date_display = date('d-m-Y', strtotime($from_date));
    $to_date_display   = date('d-m-Y', strtotime($to_date));
} else {
    $from_date = $default_from_date;
    $to_date   = $default_to_date;

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

?>                
    
<!-- Start Page Content -->
<!-- ============================================================== -->
<div class="row">
<div class="col-12">
    <div class="card">
        <div class="card-body">
            <h4 class="card-title"><?php echo $title; ?> 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>Commission ID</th>                           
                            <th>Agent Detail</th>
                            <th>Purchaser Detail</th>
                            <th>Description</th>
                            <th>Commission Amount</th>
                            <th>Wallet Balance</th>
						    <th>Date</th>
                        </tr>
                    </thead>                                      
                    <tbody>
                        <?php
                        if ( $level == "1")
                        {
                            $q = "SELECT *
                                  FROM transactions
                                  WHERE type_of_process LIKE '$type_of_process%'
                                    AND DATE(created_date) BETWEEN '$from_date' AND '$to_date'
                                  ORDER BY id DESC";
						}
                        else
                        {
                            $q = "SELECT *
                                  FROM transactions
                                  WHERE user_ref_code = '$ref_code'
                                    AND type_of_process LIKE '$type_of_process%'
                                    AND DATE(created_date) BETWEEN '$from_date' AND '$to_date'
                                  ORDER BY id DESC";     
						}
                                            
                        $result = $mysqli->query($q);
                        if ($result->num_rows > 0)
                        {
                            while($record = $result -> fetch_assoc())
                            {    						   
						        $id = $record['id'];
                                $user_ref_code = $record['user_ref_code'];
                                $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'];
	                            }  

                                $purchaser_detail = '-';
                                $buyer_ref_code = '-';
                                $buyer_name = '-';

                                $txn_trans_code = $record['trans_code'] ?? '';
                                if ($comm_type == "Gold_Sales" && $txn_trans_code != '') {
                                    $txn_trans_code_esc = $mysqli->real_escape_string($txn_trans_code);

                                    $sql_gold_sale = "SELECT customer_full_name, customer_email, customer_phone_number, item_code
                                                      FROM gold_sales
                                                      WHERE trans_code = '$txn_trans_code_esc'
                                                        AND status = '1'
                                                      ORDER BY id DESC
                                                      LIMIT 1";
                                    $res_gold_sale = $mysqli->query($sql_gold_sale);
                                    if ($res_gold_sale && $res_gold_sale->num_rows > 0) {
                                        $row_gold_sale = $res_gold_sale->fetch_assoc();
                                        $purchaser_detail = trim(($row_gold_sale['customer_full_name'] ?? '') . "\n" . ($row_gold_sale['customer_email'] ?? '') . "\n" . ($row_gold_sale['customer_phone_number'] ?? '') . "\nItem Code: " . ($row_gold_sale['item_code'] ?? ''));
                                    }
                                }
                                else
                                if ($comm_type == "Gold_Buy_Back" && $txn_trans_code != '') {
                                    $txn_trans_code_esc = $mysqli->real_escape_string($txn_trans_code);

                                    $sql_gold_buy_back = "SELECT customer_full_name, customer_email, customer_phone_number, item_code
                                                          FROM gold_buy_back
                                                          WHERE trans_code = '$txn_trans_code_esc'
                                                            AND status = '1'
                                                          ORDER BY id DESC
                                                          LIMIT 1";
                                    $res_gold_buy_back = $mysqli->query($sql_gold_buy_back);
                                    if ($res_gold_buy_back && $res_gold_buy_back->num_rows > 0) {
                                        $row_gold_buy_back = $res_gold_buy_back->fetch_assoc();
                                        $purchaser_detail = trim(($row_gold_buy_back['customer_full_name'] ?? '') . "\n" . ($row_gold_buy_back['customer_email'] ?? '') . "\n" . ($row_gold_buy_back['customer_phone_number'] ?? '') . "\nItem Code: " . ($row_gold_buy_back['item_code'] ?? ''));
                                    }
                                }
                                else
                                if ($txn_trans_code != '') {

                                    $txn_trans_code_esc = $mysqli->real_escape_string($txn_trans_code);

                                    // Find purchaser ref_code from woocommerce_order_info
                                    $sql_order = "SELECT new_ref_code
                                                  FROM woocommerce_order_info
                                                  WHERE trans_code = '$txn_trans_code_esc'
                                                    AND status = '1'
                                                  ORDER BY id DESC
                                                  LIMIT 1";
                                    $res_order = $mysqli->query($sql_order);
                                    if ($res_order && $res_order->num_rows > 0) {
                                        $row_order = $res_order->fetch_assoc();
                                        $buyer_ref_code = $row_order['new_ref_code'] ?? '';

                                        if ($buyer_ref_code != '') {
                                            $buyer_ref_code_esc = $mysqli->real_escape_string($buyer_ref_code);

                                            // Get buyer name from user table
                                            $sql_buyer = "SELECT name, last_name
                                                          FROM user
                                                          WHERE ref_code = '$buyer_ref_code_esc'
                                                            AND status = '1'
                                                          LIMIT 1";
                                            $res_buyer = $mysqli->query($sql_buyer);
                                            if ($res_buyer && $res_buyer->num_rows > 0) {
                                                $row_buyer = $res_buyer->fetch_assoc();
                                                $buyer_name = trim(($row_buyer['name'] ?? '') . ' ' . ($row_buyer['last_name'] ?? ''));
                                                $purchaser_detail = $buyer_ref_code . " | " . $buyer_name;
                                            } else {
                                                $purchaser_detail = $buyer_ref_code;
                                            }
                                        }
                                    }
                                }
                        ?>
                        <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>
                            <td><?php echo $user_ref_code;?> <br> <?php echo $name;?></td>
                            <td><?php echo nl2br(htmlspecialchars($purchaser_detail, ENT_QUOTES));?></td>
							<td>
                              <?php
                                $desc = trim($description ?? '');
                                // replace " | " or "|" with newline
                                $desc = preg_replace('/\s*\|\s*/', "\n", $desc);
                                echo nl2br(htmlspecialchars($desc, ENT_QUOTES));
                              ?>
                            </td>
                            <td><?php echo $currency_label; ?> <?php echo $in_transactions;?></td>
                            <td><?php echo $currency_label; ?> <?php echo $balance;?></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>
