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

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-images"></i>
    幻灯广告管理
</h1>

<!-- 操作栏 -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div>
        <a href="index.php?r=manage/ad/addhuan" class="btn btn-primary">
            <i class="fas fa-plus"></i> 添加广告
        </a>
    </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>
        <small class="text-muted">共 <?php echo isset($page['total']) ? $page['total'] : 0; ?> 条</small>
    </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-70">ID</th>
                        <th>标题</th>
                        <th class="text-center w-100">平台</th>
                        <th class="text-center w-90">排序</th>
                        <th class="w-160">广告图片</th>
                        <th class="w-200">链接</th>
                        <th class="text-center w-130">添加时间</th>
                        <th class="text-center w-150">操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (!empty($page['list'])): ?>
                    <?php foreach ($page['list'] as $row): ?>
                    <tr>
                        <td class="text-center font-weight-bold"><?php echo $row['id']; ?></td>
                        <td class="font-weight-600"><?php echo $row['title']; ?></td>
                        <td class="text-center">
                            <span class="badge badge-<?php echo $row['type'] == 0 ? 'primary' : 'info'; ?>">
                                <?php echo $row['type'] == 0 ? '网页端' : '移动端'; ?>
                            </span>
                        </td>
                        <td class="text-center font-weight-bold text-muted"><?php echo intval($row['px']); ?></td>
                        <td>
                            <img src="<?php echo $row['pic']; ?>" alt="" class="mw-120" style="max-height:40px ; border-radius:4px">
                        </td>
                        <td>
                            <div class="text-truncate mw-180">
                                <a href="<?php echo $row['link']; ?>" target="_blank"><?php echo htmlspecialchars($row['link']); ?></a>
                            </div>
                        </td>
                        <td class="text-center small text-muted"><?php echo $row['date']; ?></td>
                        <td class="text-center">
                            <div class="btn-group btn-group-sm">
                                <a href="index.php?r=manage/ad/edithuan&id=<?php echo $row['id']; ?>" class="btn btn-outline-primary" title="编辑">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <button class="btn btn-outline-danger" onclick="del(<?php echo $row['id']; ?>)" title="删除">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="8" class="text-center py-5">
                            <div class="empty-state">
                                <i class="fas fa-images 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>
        <!-- 分页 -->
        <?php if(isset($page['page']) && $page['page']): ?>
        <div class="text-center mt-3">
            <?php echo $page['page']; ?>
        </div>
        <?php endif; ?>
    </div>
</div>

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

<script>
function del(id) {
    if(confirm('确定要删除这个广告吗？')) {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'index.php?r=manage/ad/deletehuan&id=' + id, true);
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4 && xhr.status == 200) {
                try {
                    var result = JSON.parse(xhr.responseText);
                    if (result.status === 'y') {
                        location.reload();
                    } else {
                        alert(result.info);
                    }
                } catch(e) {
                    location.reload();
                }
            }
        };
        xhr.send();
    }
}
</script>
