{include file="app/manage/view/index/emlog_nav"}

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-user-shield"></i>
    管理员管理
</h1>

<!-- 管理员统计 -->
<div class="row mb-4">
    <div class="col-md-4">
        <div class="card text-white bg-primary">
            <div class="card-body text-center">
                <h6>总管理员</h6>
                <div class="h3"><?php echo isset($total_admins) ? number_format($total_admins) : '0'; ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-white bg-success">
            <div class="card-body text-center">
                <h6>活跃管理员</h6>
                <div class="h3"><?php echo isset($active_admins) ? number_format($active_admins) : '0'; ?></div>
            </div>
        </div>
    </div>
    <div class="col-md-4">
        <div class="card text-white bg-warning">
            <div class="card-body text-center">
                <h6>超级管理员</h6>
                <div class="h3"><?php echo isset($super_admins) ? number_format($super_admins) : '0'; ?></div>
            </div>
        </div>
    </div>
</div>

<!-- 操作按钮 -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="action-buttons">
        <a href="index.php?r=manage/manage/addmanage" class="btn btn-primary">
            <i class="fas fa-user-plus"></i> 添加管理员
        </a>
        <button class="btn btn-outline-secondary" onclick="location.reload()">
            <i class="fas fa-sync-alt"></i> 刷新
        </button>
    </div>
    
    <!-- 可以添加搜索功能 -->
    <div class="search-form w-250">
        <div class="input-group">
            <input type="text" class="form-control" id="keyword" placeholder="搜索管理员..." value="<?php echo isset($keyword) ? $keyword : ''; ?>">
            <div class="input-group-append">
                <button class="btn btn-outline-secondary" type="button" onclick="searchAdmin()">
                    <i class="fas fa-search"></i>
                </button>
            </div>
        </div>
    </div>
</div>

<!-- 管理员列表 -->
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">
            <i class="fas fa-table"></i> 管理员列表
        </h5>
        <div class="text-muted">
            <small>共 <?php echo isset($Page['count']) ? number_format($Page['count']) : 0; ?> 个管理员</small>
        </div>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover table-striped">
                <thead class="thead-light">
                    <tr>
                        <th class="text-center w-80">ID</th>
                        <th>管理员信息</th>
                        <th class="w-120">权限等级</th>
                        <th class="w-140">创建时间</th>
                        <th class="w-100">最后登录</th>
                        <th class="text-center w-140">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($Page['list'])): ?>
                    <?php foreach ($Page['list'] as $item): ?>
                    <tr>
                        <td class="text-center font-weight-bold">
                            <?php echo $item['id']; ?>
                        </td>
                        <td>
                            <div class="d-flex align-items-center">
                                <?php if(!empty($item['pic'])): ?>
                                <img src="<?php echo $item['pic']; ?>" alt="头像" class="rounded-circle mr-3 w-50 h-50" style="object-fit: cover">
                                <?php else: ?>
                                <div class="mr-3 bg-secondary rounded-circle d-flex align-items-center justify-content-center text-white font-weight-bold w-50 h-50">
                                    <?php echo strtoupper(mb_substr($item['username'], 0, 1, 'UTF-8')); ?>
                                </div>
                                <?php endif; ?>
                                <div>
                                    <div class="font-weight-bold h6 mb-0">
                                        <?php echo $item['username']; ?>
                                    </div>
                                    <?php if(!empty($item['nickname'])): ?>
                                    <small class="text-muted">昵称: <?php echo $item['nickname']; ?></small>
                                    <?php endif; ?>
                                </div>
                            </div>
                        </td>
                        <td>
                            <?php if($item['id'] == 1): ?>
                            <span class="badge badge-danger badge-pill px-3">
                                <i class="fas fa-crown mr-1"></i>超级管理员
                            </span>
                            <?php else: ?>
                            <span class="badge badge-primary badge-pill px-3">
                                <i class="fas fa-user-shield mr-1"></i>管理员
                            </span>
                            <?php endif; ?>
                        </td>
                        <td class="text-center">
                            <div class="small">
                                <?php echo date('Y-m-d', strtotime($item['date'] ?? 'today')); ?>
                                <br>
                                <span class="text-muted"><?php echo date('H:i', strtotime($item['date'] ?? 'today')); ?></span>
                            </div>
                        </td>
                        <td class="text-center">
                            <span class="text-muted">—</span>
                        </td>
                        <td class="text-center">
                            <div class="btn-group btn-group-sm" role="group">
                                <a href="index.php?r=manage/manage/edit&id=<?php echo $item['id']; ?>" class="btn btn-outline-primary" title="编辑信息">
                                    <i class="fas fa-edit"></i>
                                </a>
                                
                                <?php if($item['id'] != ($_SESSION['manage_uid'] ?? 0)): ?>
                                <a href="index.php?r=manage/manage/setpass&id=<?php echo $item['id']; ?>" class="btn btn-outline-warning" title="修改密码">
                                    <i class="fas fa-key"></i>
                                </a>
                                
                                <button class="btn btn-outline-danger" onclick="deleteAdmin(<?php echo $item['id']; ?>)" title="删除账户">
                                    <i class="fas fa-trash"></i>
                                </button>
                                <?php else: ?>
                                <span class="text-muted">当前登录</span>
                                <?php endif; ?>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center py-5">
                            <div class="empty-state">
                                <i class="fas fa-user-shield fa-3x text-muted mb-3"></i>
                                <h5 class="text-muted">暂无管理员数据</h5>
                                <p class="text-muted">点击上方"添加管理员"按钮开始添加管理员</p>
                            </div>
                        </td>
                    </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
        
        <!-- 分页导航 -->
        <nav aria-label="分页导航" class="mt-4">
            <div class="text-center">
                <?php if(isset($Page['page']) && $Page['page']): ?>
                <div class="pagination d-inline-flex">
                    <?php echo $Page['page']; ?>
                </div>
                <?php endif; ?>
                <?php if(isset($Page['count'])): ?>
                <div class="text-muted mt-2">
                    <small>共 <?php echo number_format($Page['count']); ?> 条数据</small>
                </div>
                <?php endif; ?>
            </div>
        </nav>
    </div>
</div>

{include file="app/manage/view/index/emlog_footer"}

<style>
.pagination {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}

.pagination a, .pagination span {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 36px;
    height: 36px;
    padding: 0 12px;
    border-radius: 6px;
    font-size: 14px;
    text-decoration: none;
    color: #495057;
    border: 1px solid #dee2e6;
    background-color: #fff;
    transition: all 0.2s;
}

.pagination a:hover {
    background-color: #e9ecef;
    color: #007bff;
    border-color: #b3d7ff;
}

.pagination span.current {
    background-color: #007bff;
    color: #fff;
    border-color: #007bff;
    font-weight: 600;
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 0;
}
</style>

<script>
function searchAdmin() {
    var keyword = document.getElementById('keyword').value;
    var url = 'index.php?r=manage/manage/index';
    if (keyword) {
        url += '&keyword=' + encodeURIComponent(keyword);
    }
    location.href = url;
}

function deleteAdmin(id) {
    if(confirm('确定要删除这个管理员账户吗？此操作不可恢复！')) {
        var xhr = new XMLHttpRequest();
        xhr.open('POST', 'index.php?r=manage/manage/del', true);
        xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                try {
                    var result = JSON.parse(xhr.responseText);
                    if (result.status === 'y') {
                        showToast('管理员删除成功', 'success');
                        setTimeout(function() {
                            location.reload();
                        }, 1000);
                    } else {
                        showToast(result.info, 'danger');
                    }
                } catch(e) {
                    showToast('删除失败', 'danger');
                }
            }
        };
        xhr.send('id=' + id);
    }
}

function showToast(message, type = 'success') {
    var toast = document.createElement('div');
    toast.className = 'toast align-items-center text-white bg-' + type + ' border-0';
    toast.style.cssText = 'position: fixed; top: 20px; right: 20px; z-index: 9999; min-width: 280px;';
    toast.innerHTML = `
        <div class="d-flex">
            <div class="toast-body">
                <i class="fas fa-${type === 'success' ? 'check-circle' : 'exclamation-circle'} mr-2"></i>
                ${message}
            </div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" onclick="this.parentElement.parentElement.remove()"></button>
        </div>
    `;
    
    document.body.appendChild(toast);
    
    // 自动消失
    setTimeout(function() {
        if(toast.parentNode) {
            toast.parentNode.removeChild(toast);
        }
    }, 3000);
}

// 键盘事件 - 回车搜索
document.getElementById('keyword').addEventListener('keypress', function(e) {
    if(e.which === 13) {
        searchAdmin();
    }
});
</script>