<?php
include '../config/koneksi.php';
include '../helpers/functions.php';

if (!isLogin() || !isAdmin()) redirect('../auth/login.php');

$clients = mysqli_query($conn, "
    SELECT * FROM users 
    WHERE role='client'
    ORDER BY id DESC
");

$title = "Data Client";
include '../layouts/header.php';
?>

<div class="container-fluid">
    <div id="overlay" class="overlay"></div>

    <div class="row">
        <div class="col-lg-2 p-0">
            <?php include '../layouts/sidebar.php'; ?>
        </div>

        <div class="col-lg-10 p-0">
            <?php include '../layouts/navbar.php'; ?>

            <div class="content-wrap">
                <?php showFlash(); ?>

                <div class="card p-4">
                    <h4 class="section-title">Data Client</h4>

                    <div class="table-responsive table-modern">                   
                        <table class="table table-bordered align-middle">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Nama User</th>
                                    <th>Username</th>
                                    <th>Saldo</th>
                                    <th>Dibuat</th>
                                    <th>Aksi</th>
                                </tr>
                            </thead>

                            <tbody>
                                <?php $no=1; while($c = mysqli_fetch_assoc($clients)) : ?>
                                <tr>
                                    <td><?= $no++; ?></td>

                                    <td><?= htmlspecialchars($c['nama']); ?></td>

                                    <td><?= htmlspecialchars($c['username']); ?></td>

                                    <td class="fw-bold text-success">
                                        <?= rupiah($c['saldo']); ?>
                                    </td>

                                    <td><?= $c['created_at'] ?? '-'; ?></td>

                                    <td>
                                        <!-- Tombol Edit -->
                                        <button 
                                            class="btn btn-warning btn-sm btn-edit"
                                            data-id="<?= $c['id']; ?>"
                                            data-nama="<?= $c['nama']; ?>"
                                            data-saldo="<?= $c['saldo']; ?>"
                                            data-bs-toggle="modal"
                                            data-bs-target="#modalEditSaldo">
                                            Edit Saldo
                                        </button>
                                    </td>
                                </tr>
                                <?php endwhile; ?>
                            </tbody>

                        </table>
                    </div>
                </div>

            </div>
        </div>
    </div>
</div>

<!-- MODAL EDIT SALDO -->
<div class="modal fade" id="modalEditSaldo" tabindex="-1">
  <div class="modal-dialog">
    <form method="POST" action="update_saldo.php">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Edit Saldo Client</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>

            <div class="modal-body">
                <input type="hidden" name="id" id="edit_id">

                <div class="mb-3">
                    <label>Nama</label>
                    <input type="text" id="edit_nama" class="form-control" readonly>
                </div>

                <div class="mb-3">
                    <label>Saldo</label>
                    <input type="number" name="saldo" id="edit_saldo" class="form-control" required>
                </div>
            </div>

            <div class="modal-footer">
                <button type="submit" name="update" class="btn btn-primary">Update</button>
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
            </div>
        </div>
    </form>
  </div>
</div>

<script>
document.querySelectorAll('.btn-edit').forEach(btn => {
    btn.addEventListener('click', function() {
        document.getElementById('edit_id').value = this.dataset.id;
        document.getElementById('edit_nama').value = this.dataset.nama;
        document.getElementById('edit_saldo').value = this.dataset.saldo;
    });
});
</script>

<?php include '../layouts/footer.php'; ?>