<?php
ob_start();
session_start();

require_once 'includes.php';

require_once("conn.php");

require_once("config.php");

require_once 'dbconfig4.php';

if(isset($_SESSION['admin_user_id'])){

$user_qury=mysqli_query($conn,"SELECT * FROM ormsusers WHERE user_id='$_SESSION[admin_user_id]'");

$user_resalt=mysqli_fetch_array($user_qury);

$customer = $user_resalt['user_id'];
	
$adminbalance = number_format((float)$user_resalt['balance'],2);

if($user_resalt['image']==""){

$image_path="images/empty.jpg";

}

else{

$image_path="../dashboard/images/admin/".$user_resalt['image'];

}

$q=mysqli_query($conn,"SELECT * FROM ormsregister WHERE username='".$_SESSION['user_name']."' LIMIT 1 ");

$row=mysqli_fetch_array($q);
$resalerId = $row['userid'];

}

if (isset($_GET['rpid'])) {

$user_qury=mysqli_query($conn,"UPDATE `ormsreload` SET `status` = '2' WHERE `rid` = '".$_GET['rpid']."'; ");

$user_qury=mysqli_query($conn,"SELECT * FROM ormsusers WHERE user_id='".$_GET['user_id']."' LIMIT 1 ");

$user_resalt=mysqli_fetch_array($user_qury);

$balance = $_GET['refundamount'] + $user_resalt[18];
	
$user_qury=mysqli_query($conn,"UPDATE `ormsusers` SET `balance` = '".$balance."' WHERE `user_id` = '".$_GET['user_id']."'; ");

header("refresh:2;my_reload_history.php"); // redirects image view page after 5 seconds.

}

if (isset($_GET['pid'])) {

$user_qury=mysqli_query($conn,"UPDATE `ormsreload` SET `status` = '1' WHERE `rid` = '".$_GET['pid']."'; ");

header("refresh:2;my_reload_history.php"); // redirects image view page after 5 seconds.

}

?>
<!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">
<title>My Reload History | Recharge and Billing System | ORMS | Dashbaord</title>
<?php
require_once 'headercss.php';
?>

</head>

<body>

    <!--**********************************
        Main wrapper start
    ***********************************-->
    <div id="main-wrapper">
	
<?php
require_once 'header.php';
?>

<!--**********************************
Sidebar start
***********************************-->
<div class="deznav">
<div class="deznav-scroll">
<?php

require_once 'sidebarmenu.php';

?>
</div>
</div>
<!--**********************************
Sidebar end
***********************************-->

<!--**********************************
Content body start
***********************************-->
<div class="content-body">
<!-- row -->
<div class="container-fluid">
<div class="row">
<div class="col-xl-12 col-xxl-12 col-sm-12">
<div class="alert alert-danger bg-danger text-white" role="alert">
<strong>Alert!</strong> ඔබගේ ගිනුම තහවුරු කිරීම අසම්පුර්නයි.<a href="identity_verify.php" class="btn btn-success float-right" style="padding: 0px 3px;">සම්පුර්න කරන්න</a>
</div>
</div>
</div>   
<div class="row page-titles mx-0">
<div class="col-sm-6 p-md-0">
<div class="welcome-text">
<h4>All My Reload History</h4>
</div>
</div>
<div class="col-sm-6 p-md-0 justify-content-sm-end mt-2 mt-sm-0 d-flex">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="home.php">Home</a></li>
<li class="breadcrumb-item active"><a href="javascript:void(0);">My Reload History</a></li>
<li class="breadcrumb-item active"><a href="javascript:void(0);">All My Reload History</a></li>
</ol>
</div>
</div>

<div class="row">
<div class="col-lg-12">
<div class="row tab-content">
<div id="list-view" class="tab-pane fade active show col-lg-12">
    <div class="card">
        <div class="card-header">
            <h4 class="card-title">All Reloads</h4>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table id="example3" class="table table-bordered">
                    <thead>
                        <tr>
                            <th>ID<br>#</th> 
							<th>Status<br>Transaction ID</th>
                            <th>Mobile Number<br>Operator</th>
                            <th>Amount<br>Net Amount</th>
                            <th>Closing Balance<br>Opening Balance</th>
                            <th>Received Date<br>Time</th>
							<th>Action<br>#</th> 
                        </tr>
                    </thead>
                    <tbody>
    <?php

    //$stmt = $DB_con->prepare("SELECT ormsreload.*, ormsservices.sname, ormsservices.scode FROM ormsreload LEFT JOIN ormsservices ON ormsreload.provider=ormsservices.scode  WHERE ormsreload.customer='".$customer."' ORDER BY ormsreload.rid");
	$stmt = $DB_con->prepare("SELECT rid,r.refno,r.customer,r.provider,r.number,r.amount,r.comiz,r.add_date,r.status,r.userid ruserid,r.usertype,u.user_id uuser_id,u.fullname,u.balance,u.pkgcount,u.pkgcount,u.pkgcount,s.sid,s.scode,s.sname,s.scomission FROM ormsreload r INNER JOIN ormsusers u ON r.customer=u.user_id INNER JOIN ormsservices s ON r.provider=s.scode WHERE r.userid='".$customer."' and r.usertype='Admin' ORDER BY rid DESC ");
	

    $stmt->execute();

    $ob= $user_resalt['balance'];

    if($stmt->rowCount() > 0)

    {

    while($row=$stmt->fetch(PDO::FETCH_ASSOC))

    {

    extract($row);

	$samount=$row['amount'];

	$scomiss=$row['comiz'];

	$cb =$ob-$row['amount'];
	
	$dicountBalance = $samount -($samount * $scomiss/100);

	$lttotal=$ob+$dicountBalance;
	
	$ottotal=$lttotal-$dicountBalance;
	
    ?>
                        <tr>
                            <td><?php echo $row['rid']; ?></td>

                            <td>
                            <?php 

                            if($row['status'] == "0"){

                            echo '<button class="btn btn-primary btn-sm">Fail</button>';

                            }else if($row['status'] == "1"){

                            echo '<button class="btn btn-success btn-sm">Success</button>';

                            }else if($row['status'] == "2"){

                            echo '<button class="btn btn-danger btn-sm">Refund</button>';

                            }else if($row['status'] == "3"){

                            echo '<button class="btn btn-warning btn-sm">Hold</button>';

                            }

                            ?>
							<p><?php echo $row['refno']; ?></p>
                            </td>
                            <td><p><strong><?php echo $row['number']; ?></strong></p>
							<p><strong><?php echo $row['sname']; ?></strong></p></td>
                            <td><p><strong>Rs.<?php echo number_format((float)$row['amount'],2); ?></strong></p>
							<p><strong>Rs.<?php echo number_format((float)$dicountBalance,2); ?> (<?php echo number_format((float)$scomiss,2); ?>%)</strong></p></td>
                            <td>
							<?php 

                            if($row['status'] == "0"){
 ?>
                            <p><strong>Rs.<?php echo number_format((float)$user_resalt['balance'],2); ?></strong></p>
							<p><strong>Rs.<?php echo number_format((float)$user_resalt['balance'],2); ?></strong></p>
							
<?php 
                            }else{
 ?>
                            <p><strong>Rs.<?php echo number_format((float)$ottotal,2); ?></strong></p>
							<p><strong>Rs.<?php echo number_format((float)$lttotal,2); ?></strong></p>
							

<?php 
                            }

                            ?>
							
							
							</td>
                            <td><p><strong><?php echo date_format(date_create($row['add_date']),"M d, Y"); ?></strong></p>
							<p><strong><?php echo date_format(date_create($row['add_date']),"h:i:s A"); ?></strong></p></td>    
							<td>

                                                        <a class="btn btn-sm btn-success" href="./my_reload_history.php?pid=<?php echo $row["rid"]; ?>&amount=<?php echo $row["amount"]; ?>&user_id=<?php echo $row["uuser_id"]; ?>" onClick="return confirm('Active Reload');"><i class="la la-send"></i> Send </a>      

                                                        <a class="btn btn-sm btn-danger" href="./my_reload_history.php?rpid=<?php echo $row["rid"]; ?>&refundamount=<?php echo $dicountBalance; ?>&user_id=<?php echo $row["uuser_id"]; ?>" onClick="return confirm('Refund Credit');"><i class="la la-money"></i> Refund</a>
                                                        
														<a class="btn btn-sm btn-dark" href="receipt.php?reloadno=<?php echo $row['number']; ?>&refnumber=<?php echo $row['refno']; ?>" onClick="return confirm('View Receipt');"><i class="la la-eye"></i> Receipt</a>

														<a href="add_tickets.php" class="btn btn-primary btn-sm"><i class="la la-info"></i> Help</a>
							
							</td>
                        </tr>

        <?php  

            $ob= $cb;
        ?>
    <?php } 

    }
    ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</div>
</div>
</div>

<!-- <div class="row">
<div class="col-lg-12">
<div class="row tab-content">
<div id="list-view" class="tab-pane fade active show col-lg-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">All My Reload History</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table id="example3" class="table table-bordered">
	<thead>
		<tr>
			<th>ID</th>						
			<th>Status</th>
			<th>Transaction Id</th>
			<th>Received Date/Time</th>
			<th>Mobile Number</th>
			<th>Provider</th>
			<th>Amount</th>
			<th>Net Amount</th>
			<th>Closing Balance</th>
			<th>Opening Balance</th>
		</tr>
	</thead>
	<tbody>
<?php

$stmt = $DB_con->prepare('SELECT * FROM ormsregister ORDER BY reid');

$stmt->execute();

if($stmt->rowCount() > 0)

{

while($row=$stmt->fetch(PDO::FETCH_ASSOC))

{

extract($row);

?>
		<tr>
			<td><?php echo $row['reid']; ?></td>
			<td>
			<?php 

			if($row['status'] == "0"){

			echo '<button class="btn btn-primary btn-sm">Pending</button>';

			}else if($row['status'] == "1"){

			echo '<button class="btn btn-success btn-sm">Active</button>';

			}

			?>
			</td>
			<td><?php echo $row['add_date']; ?></td>		
			<td><p><strong><?php echo $row['fname']; ?> <?php echo $row['lname']; ?></strong></p></td>
			<td><p><strong><?php echo $row['shopname']; ?></strong></p></td>
			<td><p><strong><?php echo $row['sellertype']; ?></strong></p></td>	
			<td><p><strong><?php echo $row['shopname']; ?></strong></p></td>
			<td><p><strong><?php echo $row['sellertype']; ?></strong></p></td>
			<td><p><strong><?php echo $row['sellertype']; ?></strong></p></td>
		</tr>
<?php } 

}
?>
	</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div> -->

</div>
</div>
<!--**********************************
Content body end
***********************************-->

<?php
require_once 'footer.php';
?>

<!--**********************************
Support ticket button start
***********************************-->

<!--**********************************
Support ticket button end
***********************************-->


</div>
<!--**********************************
Main wrapper end
***********************************-->

<?php
require_once 'footerjs.php';
?>


<script>

function exportTableToExcel(tableID, filename = ''){
var downloadLink;
var dataType = 'application/vnd.ms-excel';
var tableSelect = document.getElementById(tableID);
var tableHTML = tableSelect.outerHTML.replace(/ /g, '%20');

// Specify file name
filename = filename?filename+'.xls':'excel_data.xls';

// Create download link element
downloadLink = document.createElement("a");

document.body.appendChild(downloadLink);

if(navigator.msSaveOrOpenBlob){
var blob = new Blob(['\ufeff', tableHTML], {
type: dataType
});
navigator.msSaveOrOpenBlob( blob, filename);
}else{
// Create a link to the file
downloadLink.href = 'data:' + dataType + ', ' + tableHTML;

// Setting the file name
downloadLink.download = filename;

//triggering the function
downloadLink.click();
}
}
</script>

<script>

$(document).ready(function(){

loadGallery(true, 'a.thumbnail');

//This function disables buttons when needed
function disableButtons(counter_max, counter_current){
$('#investow-previous-image, #investow-next-image').investow();
if(counter_max == counter_current){
$('#investow-next-image').hide();
} else if (counter_current == 1){
$('#investow-previous-image').hide();
}
}

/**
*
* @param setIDs        Sets IDs when DOM is loaded. If using a PHP counter, set to false.
* @param setClickAttr  Sets the attribute for the click handler.
*/

function loadGallery(setIDs, setClickAttr){
var current_image,
selector,
counter = 0;

$('#investow-next-image, #investow-previous-image').click(function(){
if($(this).attr('id') == 'investow-previous-image'){
current_image--;
} else {
current_image++;
}

selector = $('[data-image-id="' + current_image + '"]');
updateGallery(selector);
});

function updateGallery(selector) {
var $sel = selector;
current_image = $sel.data('image-id');
$('#image-gallery-caption').text($sel.data('caption'));
$('#image-gallery-title').text($sel.data('title'));
$('#image-gallery-image').attr('src', $sel.data('image'));
disableButtons(counter, $sel.data('image-id'));
}

if(setIDs == true){
$('[data-image-id]').each(function(){
counter++;
$(this).attr('data-image-id',counter);
});
}
$(setClickAttr).on('click',function(){
updateGallery($(this));
});
}
});
</script>

<script>

function Publiinvested_teainvestrs(id){

$.ajax({
url:'publiinvested_teainvestrs.php',
method:'POST',
data:{id:id},
success:function(data){
location.reload();

}
});

}

function Reject_ads(id){
alert(id);
$.ajax({
url:'reject_teainvestrs.php',
method:'POST',
data:{id:id},
success:function(data){
location.reload();

}
});

}
</script>

</body>
</html>