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

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

$keyword = $_GET['keyword'] ?? '';

if (!empty($keyword)) {
    $keywordEscaped = mysqli_real_escape_string($conn, $keyword);
    $users = mysqli_query($conn, "
        SELECT * FROM users 
        WHERE nama LIKE '%$keywordEscaped%' 
           OR username LIKE '%$keywordEscaped%' 
           OR role LIKE '%$keywordEscaped%'
        ORDER BY id DESC
    ");
} else {
    $users = mysqli_query($conn, "SELECT * FROM users ORDER BY id DESC");
}

$title = "Management User";
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">
                    <div class="d-flex justify-content-between align-items-center gap-3 mb-4 top-action-stack">
                        <div>
                            <h4 class="section-title mb-1">Management User</h4>
                            <p class="text-muted mb-0">Kelola data admin dan client dengan lebih mudah.</p>
                        </div>

                        <a href="user_tambah.php" class="btn btn-primary">
                            + Tambah User
                        </a>
                    </div>

                    <form method="GET" class="row g-3 mb-4 search-stack">
                        <div class="col-md-9">
                            <input type="text" name="keyword" class="form-control" placeholder="Cari nama, username, atau role..." value="<?= htmlspecialchars($keyword); ?>">
                        </div>
                        <div class="col-md-3 d-grid">
                            <button type="submit" class="btn btn-outline-primary">Cari</button>
                        </div>
                    </form>

                    <!-- DESKTOP TABLE -->
                    <div class="table-responsive table-modern desktop-user-table">
                        <table class="table table-hover align-middle">
                            <thead>
                                <tr>
                                    <th width="60">No</th>
                                    <th>Nama</th>
                                    <th>Username</th>
                                    <th>Role</th>
                                    <th>Saldo</th>
                                    <th>Dibuat</th>
                                    <th width="180">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php if (mysqli_num_rows($users) > 0): ?>
                                    <?php 
                                    mysqli_data_seek($users, 0);
                                    $no = 1; 
                                    while($u = mysqli_fetch_assoc($users)) : 
                                    ?>
                                        <tr>
                                            <td><?= $no++; ?></td>
                                            <td>
                                                <div class="d-flex align-items-center gap-3">
                                                    <div class="avatar-circle">
                                                        <?= strtoupper(substr($u['nama'], 0, 1)); ?>
                                                    </div>
                                                    <div>
                                                        <div class="fw-semibold"><?= htmlspecialchars($u['nama']); ?></div>
                                                        <small class="text-muted">ID: <?= $u['id']; ?></small>
                                                    </div>
                                                </div>
                                            </td>
                                            <td><?= htmlspecialchars($u['username']); ?></td>
                                            <td>
                                                <?php if ($u['role'] == 'admin'): ?>
                                                    <span class="badge bg-dark">Admin</span>
                                                <?php else: ?>
                                                    <span class="badge bg-primary">Client</span>
                                                <?php endif; ?>
                                            </td>
                                            <td class="fw-semibold text-success"><?= rupiah($u['saldo']); ?></td>
                                            <td><?= date('d M Y H:i', strtotime($u['created_at'])); ?></td>
                                            <td>
                                                <div class="d-flex gap-2 flex-wrap">
                                                    <a href="user_edit.php?id=<?= $u['id']; ?>" class="btn btn-warning btn-sm">Edit</a>

                                                    <?php if ($u['id'] != $_SESSION['id']): ?>
                                                        <a href="user_hapus.php?id=<?= $u['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus user ini?')">Hapus</a>
                                                    <?php else: ?>
                                                        <span class="text-muted small">Akun aktif</span>
                                                    <?php endif; ?>
                                                </div>
                                            </td>
                                        </tr>
                                    <?php endwhile; ?>
                                <?php else: ?>
                                    <tr>
                                        <td colspan="7" class="text-center py-4 text-muted">
                                            Data user tidak ditemukan.
                                        </td>
                                    </tr>
                                <?php endif; ?>
                            </tbody>
                        </table>
                    </div>

                    <!-- MOBILE CARD -->
                    <div class="user-mobile-card">
                        <?php
                        $usersMobile = mysqli_query($conn, !empty($keyword) ? "
                            SELECT * FROM users 
                            WHERE nama LIKE '%$keywordEscaped%' 
                               OR username LIKE '%$keywordEscaped%' 
                               OR role LIKE '%$keywordEscaped%'
                            ORDER BY id DESC
                        " : "SELECT * FROM users ORDER BY id DESC");
                        ?>

                        <?php if (mysqli_num_rows($usersMobile) > 0): ?>
                            <?php while($u = mysqli_fetch_assoc($usersMobile)) : ?>
                                <div class="mobile-user-item">
                                    <div class="top">
                                        <div class="avatar-circle">
                                            <?= strtoupper(substr($u['nama'], 0, 1)); ?>
                                        </div>
                                        <div>
                                            <div class="fw-bold fs-5"><?= htmlspecialchars($u['nama']); ?></div>
                                            <small class="text-muted">ID: <?= $u['id']; ?></small>
                                        </div>
                                    </div>

                                    <div class="meta"><b>Username:</b> <?= htmlspecialchars($u['username']); ?></div>
                                    <div class="meta"><b>Role:</b> <?= ucfirst($u['role']); ?></div>
                                    <div class="meta"><b>Saldo:</b> <span class="text-success fw-semibold"><?= rupiah($u['saldo']); ?></span></div>
                                    <div class="meta"><b>Dibuat:</b> <?= date('d M Y H:i', strtotime($u['created_at'])); ?></div>

                                    <div class="actions">
                                        <a href="user_edit.php?id=<?= $u['id']; ?>" class="btn btn-warning btn-sm">Edit</a>

                                        <?php if ($u['id'] != $_SESSION['id']): ?>
                                            <a href="user_hapus.php?id=<?= $u['id']; ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin ingin menghapus user ini?')">Hapus</a>
                                        <?php else: ?>
                                            <span class="text-muted small align-self-center">Akun aktif</span>
                                        <?php endif; ?>
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        <?php else: ?>
                            <div class="text-center py-4 text-muted">Data user tidak ditemukan.</div>
                        <?php endif; ?>
                    </div>

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

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