<?php
session_start();
if (!isset($_SESSION['unohs'])) {
    header("location:index.php?msg=unauthorized");
    exit();
}
include("conn.php");
date_default_timezone_set('Asia/Kolkata');

// Handle Approve/Reject action
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $uid = mysqli_real_escape_string($conn, $_POST['uid']);
    $amount = mysqli_real_escape_string($conn, $_POST['amount']);
    $sid = mysqli_real_escape_string($conn, $_POST['sid']);

    if (isset($_POST['approve'])) {
        // Approve
        $wallet = mysqli_fetch_assoc(mysqli_query($conn, "SELECT motta FROM shonu_kaichila WHERE balakedara='$uid'"));
        $newBalance = intval($wallet['motta']) + intval($amount);
        mysqli_query($conn, "UPDATE shonu_kaichila SET motta='$newBalance' WHERE balakedara='$uid'");
        mysqli_query($conn, "UPDATE thevani SET sthiti='1' WHERE shonu='$sid'");
    } elseif (isset($_POST['reject'])) {
        // Reject
        mysqli_query($conn, "UPDATE thevani SET sthiti='2' WHERE shonu='$sid'");
    }

    header("Location: deposit_update.php?success=1");
    exit();
}

// Fetch dashboard counts
$today = date('Y-m-d');

// Today Approved Recharge
$todayApproved = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(motta) as total FROM thevani WHERE sthiti='1' AND DATE(dinankavannuracisi)='$today'"));
$todayApprovedAmount = $todayApproved['total'] ?: 0;

// Total Approved Recharge
$totalApproved = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(motta) as total FROM thevani WHERE sthiti='1'"));
$totalApprovedAmount = $totalApproved['total'] ?: 0;

// Total Rejected Recharge
$totalRejected = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(motta) as total FROM thevani WHERE sthiti='2'"));
$totalRejectedAmount = $totalRejected['total'] ?: 0;
?>

<?php include 'header.php'; ?>

<div class="main-panel">
    <div class="content-wrapper">

        <h4 class="font-weight-bold text-dark">Deposit Update</h4>

        <?php if (isset($_GET['success'])) { ?>
            <div class="alert alert-success" id="successAlert">
                Action performed successfully!
            </div>
        <?php } ?>

        <!-- Dashboard Summary Boxes -->
        <div class="row mb-4">
            <div class="col-md-4">
                <div class="card bg-success text-white">
                    <div class="card-body">
                        <h5 class="card-title">Today's Approved Recharge</h5>
                        <h3>₹<?= number_format($todayApprovedAmount, 2) ?></h3>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-primary text-white">
                    <div class="card-body">
                        <h5 class="card-title">Total Approved Recharge</h5>
                        <h3>₹<?= number_format($totalApprovedAmount, 2) ?></h3>
                    </div>
                </div>
            </div>
            <div class="col-md-4">
                <div class="card bg-danger text-white">
                    <div class="card-body">
                        <h5 class="card-title">Total Rejected Recharge</h5>
                        <h3>₹<?= number_format($totalRejectedAmount, 2) ?></h3>
                    </div>
                </div>
            </div>
        </div>

        <!-- Pending Payment Section -->
        <h4 class="mt-4">Pending Payment Update</h4>
        <div class="table-responsive">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User ID</th>
                        <th>Mobile</th>
                        <th>Reference Number</th>
                        <th>Amount</th>
                        <th>Order ID</th>
                        <th>Date</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $pending = mysqli_query($conn, "SELECT * FROM thevani WHERE sthiti='0' ORDER BY shonu DESC");
                    $i = 1;
                    while ($row = mysqli_fetch_assoc($pending)) {
                    ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td><?= $row['balakedara'] ?></td>
                        <td><?= $row['duravani'] ?></td>
                        <td><?= $row['ullekha'] ?></td>
                        <td><?= $row['motta'] ?></td>
                        <td><?= $row['dharavahi'] ?></td>
                        <td><?= $row['dinankavannuracisi'] ?></td>
                        <td>
                            <form method="post" style="display:inline-block;">
                                <input type="hidden" name="uid" value="<?= $row['balakedara'] ?>">
                                <input type="hidden" name="amount" value="<?= $row['motta'] ?>">
                                <input type="hidden" name="sid" value="<?= $row['shonu'] ?>">
                                <button type="submit" name="approve" class="btn btn-success btn-sm">Approve</button>
                            </form>
                            <form method="post" style="display:inline-block;">
                                <input type="hidden" name="uid" value="<?= $row['balakedara'] ?>">
                                <input type="hidden" name="amount" value="<?= $row['motta'] ?>">
                                <input type="hidden" name="sid" value="<?= $row['shonu'] ?>">
                                <button type="submit" name="reject" class="btn btn-danger btn-sm">Reject</button>
                            </form>
                        </td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

        <!-- Completed Payment Section -->
        <h4 class="mt-5">Completed Payment Records</h4>
        <div class="table-responsive">
            <table class="table table-striped table-bordered">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>User ID</th>
                        <th>Mobile</th>
                        <th>Reference Number</th>
                        <th>Amount</th>
                        <th>Order ID</th>
                        <th>Date</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $completed = mysqli_query($conn, "SELECT * FROM thevani WHERE sthiti='1' ORDER BY shonu DESC");
                    $i = 1;
                    while ($row = mysqli_fetch_assoc($completed)) {
                    ?>
                    <tr>
                        <td><?= $i++ ?></td>
                        <td><?= $row['balakedara'] ?></td>
                        <td><?= $row['duravani'] ?></td>
                        <td><?= $row['ullekha'] ?></td>
                        <td><?= $row['motta'] ?></td>
                        <td><?= $row['dharavahi'] ?></td>
                        <td><?= $row['dinankavannuracisi'] ?></td>
                    </tr>
                    <?php } ?>
                </tbody>
            </table>
        </div>

    </div>
</div>

<!-- jQuery and Success Alert Auto-hide -->
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
<script>
$(document).ready(function(){
    setTimeout(function() {
        $("#successAlert").fadeOut('slow');
    }, 3000);

    setTimeout(function() {
        if (window.history.replaceState) {
            const cleanUrl = window.location.href.split('?')[0];
            window.history.replaceState({}, document.title, cleanUrl);
        }
    }, 3000);
});
</script>

</body>
</html>
