<?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 -->
<!-- Start Page Content -->
<!-- ============================================================== -->
<?php
$order_id = htmlspecialchars($_GET['order_id'],ENT_QUOTES);
$trans_code = htmlspecialchars($_GET['trans_code'],ENT_QUOTES);	

$sql_currency = "SELECT *
FROM currency_rate_setting 
WHERE status='1'";
$result_currency = $mysqli->query($sql_currency);
$row_currency = $result_currency->fetch_assoc();
$deposit_rm = $row_currency['deposit_rm'];

$sql_my_wallet = "SELECT * 
FROM wallet
WHERE ref_code='$ref_code' 
LIMIT 1";
$result_my_wallet = $mysqli-> query($sql_my_wallet);
if ($result_my_wallet -> num_rows > 0)
{
	$row_my_wallet = $result_my_wallet ->fetch_assoc();
	$my_cash_wallet = $row_my_wallet['cash_wallet'];
}

$sql_order_item = "SELECT *
				   FROM woocommerce_order_info
				   WHERE order_id  = '$order_id'
                   AND trans_code  = '$trans_code'
				   AND status='1'";
$result_order_item = $mysqli-> query($sql_order_item);
if ($result_order_item -> num_rows > 0)
{
	$row_order_item = $result_order_item ->fetch_assoc();
	$order_items  = $row_order_item['order_items'];
    $order_full_total = $row_order_item['order_full_total'];
}

$data_total_payment = 
"SELECT SUM(amount) As num 
FROM package_payment
WHERE order_id = '$order_id'
AND trans_code = '$trans_code'
AND ref_code = '$ref_code'
AND status = '1'";    
$result_total_payment = $mysqli->query($data_total_payment);
if ($result_total_payment ->num_rows > 0)
{
	$row_total_payment = $result_total_payment ->fetch_assoc();
	$full_total_payment = $row_total_payment['num'];
}

$order_balance = $order_full_total - $full_total_payment;

$order_balance_rm = (float)$order_balance * (float)$deposit_rm;
$min_rm = 50;
$min_usd = (float)$min_rm / (float)$deposit_rm;

// format for display
$order_balance_rm_display = number_format($order_balance_rm, 2);
$min_usd_display = number_format($min_usd, 2);

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


<div class="row">
<div class="col-lg-12">
<div class="card">
    <div class="card-body">
        <form method="post" action="../Controller/Database_sql.php" enctype="multipart/form-data">
            <div class="form-body">
                <h3 class="card-title">
                  Pay Balance For Package <?php echo $order_items; ?>
                  (Balance USD <?php echo number_format($order_balance,2); ?> | Balance RM <?php echo $order_balance_rm_display; ?>)
                </h3>
                <hr>
                <div class="row p-t-20">
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">
                              Insert Payment Value (USD) | Minimum USD <?php echo $min_usd_display; ?> | E-wallet: $ <?php echo number_format($my_cash_wallet,2); ?>
                            </label>
                            <input type="number"
                           step="0.01"
                           min="<?php echo htmlspecialchars(number_format($min_usd, 2, '.', ''), ENT_QUOTES); ?>"
                           class="form-control"
                           name="amount"
                           required>
                        </div>
                    </div>   
                    
                    <div class="col-md-6">
                        <div class="form-group">
                            <label class="control-label">Payment Method</label>
                            <br>
                            <select class="form-control" name="payment_method" required>
                                <option value="">Please Select Payment Method</option>
                                <option value="E-wallet">Pay Using E-wallet</option>
                                <!--option value="PayPal">Pay Using PayPal</option-->
                                <?php if ($country_static == "MY") { ?>
                                  <option value="Toyyibpay">Pay Using Toyyibpay</option>
                                <?php } ?>
                            </select>
                        </div>
                    </div>

                </div>
            </div>
            <div class="form-actions">
                <input type="hidden" name="ref_code" value="<?php echo $ref_code;?>">
                <input type="hidden" name="order_id" value="<?php echo $order_id;?>" required>
                <input type="hidden" name="trans_code" value="<?php echo $trans_code;?>" required>
                <input type="hidden" name="full_amount" value="<?php echo $order_full_total;?>" required>
                <a href="list_of_package.php" class="btn btn-success">Back</a>
                <button name="Pay_Package" class="btn btn-success"><i class="fa fa-check"></i> Pay</button>
            </div>
        </form>
    </div>
</div>
</div>
</div>

<!--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">
                        <input type="hidden" class="form-control" name="order_id" value="<?php echo $order_id;?>" required>
                        <input type="hidden" class="form-control" name="trans_code" value="<?php echo $trans_code;?>" required>
                        <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'])) 
{
    $order_id = $_POST['order_id'];
    $trans_code = $_POST['trans_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']));
}

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');
?>

<div class="row">
<div class="col-12">
<div class="card">
    <div class="card-body">
        <h4 class="card-title">Payment History For <?php echo $order_items; ?></h4>
        <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>Invoice ID</th>
                        <th>Payment Method</th>
                        <th>Payer Info</th>
                        <th>Amount</th>
                        <th>Balance</th>
                        <th>Payment Date</th>
                        <th>Action</th>
                    </tr>
                </thead>                                      
                <tbody>
                    <?php
                    $q = "SELECT *
                    FROM package_payment
                    Where order_id = '$order_id'
                    And trans_code = '$trans_code'
                    And status = '1'
                    order by id Asc";

                    $result = $mysqli->query($q);
                    if ($result->num_rows > 0)
                    {
                        while($record = $result -> fetch_assoc())
                        {  
						    $invoice_id = $record['id'];
                            $ref_code  = $record['ref_code'];
                            $amount  = $record['amount'];
                            $balance = $record['balance'];
                            $payment_method = $record['payment_method'];
                            $created_date = date('d-m-Y H:i:s', strtotime($record['created_date']));

                            $sql_user = "SELECT * 
                            FROM user 
                            WHERE ref_code='$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" onsubmit="return confirm('Do you want to proceed?');" enctype="multipart/form-data">
                    <tr>
                        <td>INV00<?php echo $invoice_id; ?></td>
                        <td><?php echo $payment_method; ?></td>
                        <td><?php echo $ref_code;?> | <?php echo $name;?></td>
                        <td>USD <?php echo number_format($amount,2); ?></td>
                        <td>USD <?php echo number_format($balance,2); ?></td>
                        <td><?php echo $created_date; ?></td>
                        <td><a href="pay_invoice.php?invoice_id=<?php echo $invoice_id; ?>&order_id=<?php echo $order_id; ?>&trans_code=<?php echo $trans_code; ?>" class="btn btn-success">View Invoice</a></td>
                        <?php
                        }  
                    }
                    ?>
                    </form> 
                    </tr>
                </tbody>
            </table>
            <a href="list_of_package.php" class="btn btn-success">Back</a>
            </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>