<?php
$page_num = isset($_GET['p']) ? intval($_GET['p']) : 1;
$perpage = 15;
$search = isset($_GET['search']) ? trim($_GET['search']) : '';

$where = "role = 'user'";
if ($search) {
    $search_esc = $db->escape($search);
    $where .= " AND (username LIKE '%$search_esc%' OR email LIKE '%$search_esc%')";
}

$total = $db->fetchOne("SELECT COUNT(*) as cnt FROM users WHERE $where")['cnt'];
$offset = ($page_num - 1) * $perpage;

$users = $db->fetchAll("
    SELECT u.*, ug.name as group_name 
    FROM users u 
    LEFT JOIN user_groups ug ON u.group_id = ug.id 
    WHERE $where 
    ORDER BY u.id DESC 
    LIMIT $offset, $perpage
");
?>

<!-- 搜索 -->
<div class="card" style="padding: 12px;">
    <form method="GET" style="display: flex; gap: 8px;">
        <input type="hidden" name="page" value="users">
        <input type="text" name="search" placeholder="搜索用户名或邮箱" value="<?php echo htmlspecialchars($search); ?>" 
               style="flex:1; padding: 12px; border: 1px solid #e5e7eb; border-radius: 10px; font-size: 14px;">
        <button type="submit" class="btn btn-primary" style="padding: 12px 16px;">
            <img src="https://api.iconify.design/mdi:magnify.svg?color=white" width="20" height="20" alt="">
        </button>
    </form>
</div>

<!-- 统计 -->
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 12px;">
    <div class="card" style="text-align: center;">
        <div style="font-size: 24px; font-weight: 700; color: #667eea;"><?php echo $total; ?></div>
        <div style="color: #999; font-size: 13px;">总用户</div>
    </div>
    <div class="card" style="text-align: center;">
        <div style="font-size: 24px; font-weight: 700; color: #10b981;"><?php echo $db->fetchOne("SELECT COUNT(*) as cnt FROM users WHERE role='user' AND status=1")['cnt']; ?></div>
        <div style="color: #999; font-size: 13px;">正常用户</div>
    </div>
</div>

<!-- 用户列表 -->
<?php foreach ($users as $u): ?>
<div class="card" style="display: flex; align-items: center; gap: 12px;">
    <img src="https://api.iconify.design/mdi:account-circle.svg?color=%23667eea" width="44" height="44" alt="">
    <div style="flex:1;">
        <div style="font-weight: 600;"><?php echo htmlspecialchars($u['username']); ?></div>
        <div style="font-size: 12px; color: #999;"><?php echo htmlspecialchars($u['email']); ?></div>
        <div style="font-size: 12px; color: #666;">余额: ¥<?php echo number_format($u['balance'], 2); ?></div>
    </div>
    <div style="text-align: right;">
        <span style="display: inline-block; padding: 4px 10px; border-radius: 20px; font-size: 12px; 
                     <?php echo $u['status'] ? 'background:#d1fae5;color:#065f46;' : 'background:#fee2e2;color:#991b1b;'; ?>">
            <?php echo $u['status'] ? '正常' : '禁用'; ?>
        </span>
        <div style="margin-top: 8px;">
            <a href="?page=user_detail&id=<?php echo $u['id']; ?>" style="color: #667eea; text-decoration: none; font-size: 13px;">
                <img src="https://api.iconify.design/mdi:eye.svg?color=%23667eea" width="16" height="16" alt=""> 详情
            </a>
        </div>
    </div>
</div>
<?php endforeach; ?>

<?php if (empty($users)): ?>
<div class="card" style="text-align: center; padding: 40px;">
    <img src="https://api.iconify.design/mdi:account-search.svg?color=%23999" width="48" height="48" alt="">
    <p style="color: #999;">暂无用户</p>
</div>
<?php endif; ?>

<!-- 分页 -->
<?php if ($total > $perpage): ?>
<div style="display: flex; justify-content: center; gap: 8px; margin-top: 16px;">
    <?php if ($page_num > 1): ?>
        <a href="?page=users&p=<?php echo $page_num-1; ?>" class="btn" style="background:#f5f5f5;">上一页</a>
    <?php endif; ?>
    <span style="padding: 10px 16px;"><?php echo $page_num; ?>/<?php echo ceil($total/$perpage); ?></span>
    <?php if ($page_num < ceil($total/$perpage)): ?>
        <a href="?page=users&p=<?php echo $page_num+1; ?>" class="btn" style="background:#f5f5f5;">下一页</a>
    <?php endif; ?>
</div>
<?php endif; ?>