<?php
include('header.php');

if ($_SESSION['userid'] != "ADMIN") {
?>
    <script>
        window.location.href = 'index.php';
    </script>
    <?php
    die;
}

$res = mysqli_query($conn, "SELECT * FROM `usertable` ");

if (isset($_POST['delete']) && $_POST['delete'] == "ahkwebsolutions") {
    $id = base64_decode($_POST['userid']);
    $id = mysqli_real_escape_string($conn, $id); // ✅ security
    $del = mysqli_query($conn, "DELETE FROM `usertable` WHERE userid='$id'");
    if ($del) {
    ?>
        <script>
            $(function() {
                Swal.fire('Success','Data Deleted Success!','success');
            });
            window.setTimeout(function() {
                window.location.href = "userlist.php";
            }, 1500);
        </script>
    <?php
    } else {
    ?>
        <script>
            $(function() {
                Swal.fire('Oops','Data Not Deleted !','error');
            });
        </script>
<?php
    }
}
?>

<?php
if(isset($_GET['login']) && $_GET['login'] ==1 && $_GET['userid'] && $_GET['userid']!=NULL){
    $userid = base64_decode($_GET['userid']);
    $_SESSION['userid'] = $userid;
    ?>
    <script>
         setTimeout(function () {
                window.location='';
            }, 00);
    </script>
    <?php
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User List</title>
<meta name="viewport" content="width=device-width, initial-scale=1">

<!-- ✅ Styles -->
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/buttons/2.4.1/css/buttons.dataTables.min.css">
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

<style>
body {
    margin: 0;
    font-family: 'Poppins', sans-serif;
    background: linear-gradient(135deg,#fceabb,#f8b500,#fbd786,#f7797d);
    background-size: 400% 400%;
    animation: gradientShift 15s ease infinite;
    min-height: 100vh;
}
@keyframes gradientShift {
  0%{background-position:0% 50%}
  50%{background-position:100% 50%}
  100%{background-position:0% 50%}
}
.container {max-width: 1150px; margin: 20px auto;}
.card {background: rgba(255,255,255,0.7); border-radius: 20px; padding: 20px;}
.card-header {
    background: linear-gradient(90deg,#ff416c,#ff4b2b,#ff6a00);
    color: white; text-align:center; padding: 10px; font-size: 20px; border-radius: 15px;
    margin-bottom: 15px;
}
#table_id {width:100%!important; table-layout:fixed; border-collapse:collapse;}
#table_id th,#table_id td {
    text-align:center; padding:8px; overflow:hidden;
    white-space:normal; text-overflow:ellipsis; vertical-align:middle;
}
#table_id thead th {
    background: linear-gradient(90deg,#ff416c,#ff4b2b,#ff6a00);
    color:white; position:sticky; top:0; z-index:2;
}
.dataTables_wrapper {overflow-x:auto;}
.btn {border-radius:20px; padding:5px 10px; font-size:0.8rem; border:none; cursor:pointer;}
.btn-danger {background:linear-gradient(45deg,#ff416c,#ff4b2b); color:white;}
.btn-success {background:linear-gradient(45deg,#00b09b,#96c93d); color:white;}
.btn-warning {background:linear-gradient(45deg,#f7b733,#fc4a1a); color:white;}
</style>
</head>

<body>
<div class="container">
    <div class="card">
        <div class="card-header">👥 User List</div>

        <table id="table_id" class="display nowrap">
            <thead>
                <tr>
                    <th>Sr.No</th>
                    <th>User ID</th>
                    <th>Mobile</th>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Usertype</th>
                    <th>Wallet</th>
                    <th>Reset</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <?php
                if (mysqli_num_rows($res) > 0) {
                    $sl = 1;
                    while ($data = mysqli_fetch_assoc($res)) {
                        if ($data['userid'] == "@SMILE") continue;
                        ?>
                        <tr>
                            <td><?= $sl++ ?></td>
                            <td><?= htmlspecialchars($data['userid']) ?></td>
                            <td><?= htmlspecialchars($data['phone']) ?></td>
                            <td><?= htmlspecialchars($data['name']) ?></td>
                            <td><?= htmlspecialchars($data['emailid']) ?></td>
                            <td><?= htmlspecialchars($data['usertype']) ?></td>
                            <td><span class="btn btn-success">₹ <?= $data['walletamount'] ?></span></td>
                            <td>
                                <!-- ✅ Reset confirmation added here -->
                                <form action="reset_password.php" method="post" class="resetForm">
                                    <input type="hidden" name="userid" value="<?= base64_encode($data['userid']) ?>">
                                    <button type="button" class="btn btn-warning btn-sm" onclick="confirmReset(this, '<?= htmlspecialchars($data['userid']) ?>')">
                                        <i class="fa fa-key"></i>
                                    </button>
                                </form>
                            </td>
                            <td>
                                <a href="edituser.php?userid=<?= base64_encode($data['userid']) ?>" class="btn btn-success btn-sm"><i class="fa fa-edit"></i></a>
                                <form action="" method="post" style="display:inline;">
                                    <input type="hidden" name="userid" value="<?= base64_encode($data['userid']) ?>">
                                    <input type="hidden" name="delete" value="ahkwebsolutions">
                                    <button type="button" class="btn btn-danger btn-sm" onclick="deleteRecord(this)"><i class="fa fa-trash"></i></button>
                                </form>
                            </td>
                        </tr>
                        <?php
                    }
                }
                ?>
            </tbody>
        </table>
    </div>
</div>

<!-- ✅ Scripts -->
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/2.4.1/js/buttons.print.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

<script>
$(document).ready(function(){
    $('#table_id').DataTable({
        pageLength: 100,
        lengthMenu: [[100, 200, 500], [100, 200, 500]],
        responsive: true,
        dom: 'Bfrtip',
        buttons: ['copy','csv','excel','pdf','print']
    });
});

// ✅ Delete Confirmation
function deleteRecord(btn){
    Swal.fire({
        title:'Are you sure?',
        text:'This user will be deleted permanently!',
        icon:'warning',
        showCancelButton:true,
        confirmButtonColor:'#3085d6',
        cancelButtonColor:'#d33',
        confirmButtonText:'Yes, delete!'
    }).then((result)=>{
        if(result.isConfirmed){
            $(btn).closest('form').submit();
        }
    });
}

// ✅ Password Reset Confirmation
function confirmReset(btn, userid){
    Swal.fire({
        title: '⚠ Confirm Password Reset',
        text: 'Are you sure you want to reset password for ' + userid + '?',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#d33',
        cancelButtonColor: '#3085d6',
        confirmButtonText: 'Yes, Reset'
    }).then((result)=>{
        if(result.isConfirmed){
            $(btn).closest('form').submit();
        }
    });
}
</script>

</body>
</html>
