<?php
require __DIR__ . '/../../app/bootstrap.php';
require __DIR__ . '/../../app/database.php';
require __DIR__ . '/../../app/admin-sidebar.php';
require_login();

$adminName = isset($_SESSION['admin_name']) ? (string)$_SESSION['admin_name'] : 'Admin';
$adminUsername = isset($_SESSION['admin_username']) ? (string)$_SESSION['admin_username'] : 'admin';
$avatarText = strtoupper(substr($adminName !== '' ? $adminName : 'A', 0, 1));

$slot = isset($_GET['slot']) ? trim((string)$_GET['slot']) : '';
$status = isset($_GET['status']) ? trim((string)$_GET['status']) : '';
$deleteStatus = isset($_GET['delete_status']) ? trim((string)$_GET['delete_status']) : '';
$slotOptions = article_ad_slot_options();
$statusOptions = article_ad_status_options();
$typeOptions = article_ad_type_options();
$domains = array();
$dbError = '';
$ads = array();

try {
    $domains = list_enabled_domain_options();
    $ads = list_article_ads($slot, $status);
} catch (Exception $e) {
    $dbError = $e->getMessage();
}
?><!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo e(APP_NAME); ?> - 广告管理</title>
    <link rel="stylesheet" href="/assets/css/admin-ui.css">
</head>
<body>
<div class="admin-shell">
    <?php render_admin_sidebar('ads', $adminName, $adminUsername, $avatarText); ?>
    <main class="admin-content">
        <div class="content-wrap">
            <header class="topbar">
                <div><h1>广告管理</h1><p>统一维护文章内容页广告位，支持文章开头/中间/结尾及侧栏投放。</p></div>
                <div class="topbar-actions"><a class="btn-primary" href="/admin/ad/edit.php">新增广告</a></div>
            </header>
            <section class="page-card">
                <?php if ($deleteStatus === 'success'): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(34,197,94,0.18); background: rgba(34,197,94,0.06);"><strong style="color:#15803d;">删除成功</strong><p style="margin:0; color:#166534;">广告已删除。</p></div>
                <?php elseif ($deleteStatus === 'error'): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);"><strong style="color:#dc2626;">删除失败</strong><p style="margin:0; color:#b91c1c;">请稍后重试。</p></div>
                <?php endif; ?>
                <?php if ($dbError !== ''): ?>
                    <div class="mini-item" style="margin-bottom:16px; border-color: rgba(220,38,38,0.18); background: rgba(220,38,38,0.06);"><strong style="color:#dc2626;">数据库读取失败</strong><p style="margin:0; color:#b91c1c;"><?php echo e($dbError); ?></p></div>
                <?php endif; ?>
                <form class="filter-row" method="get" action="/admin/ad/list.php" style="margin-bottom:16px;">
                    <select class="select" name="slot">
                        <option value="all">全部位置</option>
                        <?php foreach ($slotOptions as $key => $label): ?>
                            <option value="<?php echo e($key); ?>"<?php echo $slot === $key ? ' selected' : ''; ?>><?php echo e($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select class="select" name="status">
                        <option value="all">全部状态</option>
                        <?php foreach ($statusOptions as $key => $label): ?>
                            <option value="<?php echo e($key); ?>"<?php echo $status === $key ? ' selected' : ''; ?>><?php echo e($label); ?></option>
                        <?php endforeach; ?>
                    </select>
                    <button class="btn-soft" type="submit">筛选</button>
                    <a class="btn-ghost" href="/admin/ad/list.php">重置</a>
                </form>
                <table class="data-table">
                    <thead><tr><th>ID</th><th>广告名称</th><th>位置</th><th>类型</th><th>投放域名</th><th>状态</th><th>排序</th><th>更新时间</th><th>操作</th></tr></thead>
                    <tbody>
                    <?php if (!empty($ads)): ?>
                        <?php foreach ($ads as $item): ?>
                            <?php $domainIds = list_article_ad_domain_ids((int)$item['id']); ?>
                            <tr>
                                <td><?php echo (int)$item['id']; ?></td>
                                <td><strong><?php echo e($item['name']); ?></strong></td>
                                <td><?php echo e(isset($slotOptions[$item['slot']]) ? $slotOptions[$item['slot']] : $item['slot']); ?></td>
                                <td><?php echo e(isset($typeOptions[$item['ad_type']]) ? $typeOptions[$item['ad_type']] : $item['ad_type']); ?></td>
                                <td><?php if (empty($domainIds)): ?>全部域名<?php else: ?><?php $domainLabels = array(); foreach ($domains as $domainItem) { if (in_array((int)$domainItem['id'], $domainIds, true)) { $domainLabels[] = (string)$domainItem['domain']; } } echo e(!empty($domainLabels) ? implode(' / ', $domainLabels) : '指定域名'); ?><?php endif; ?></td>
                                <td><span class="badge <?php echo e($item['status'] === 'enabled' ? 'success' : 'info'); ?>"><?php echo e(isset($statusOptions[$item['status']]) ? $statusOptions[$item['status']] : $item['status']); ?></span></td>
                                <td><?php echo (int)$item['sort_order']; ?></td>
                                <td><?php echo e($item['updated_at']); ?></td>
                                <td><a href="/admin/ad/edit.php?id=<?php echo (int)$item['id']; ?>">编辑</a> / <form method="post" action="/admin/ad/delete.php" style="display:inline;" onsubmit="return confirm('确认删除广告「<?php echo e($item['name']); ?>」吗？');"><?php echo csrf_field(); ?><input type="hidden" name="id" value="<?php echo (int)$item['id']; ?>"><button type="submit" class="link-btn" style="color:#dc2626;">删除</button></form></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <tr><td colspan="9" class="empty-note"><?php echo $dbError === '' ? '暂无广告，请先新增。' : '无法加载广告数据。'; ?></td></tr>
                    <?php endif; ?>
                    </tbody>
                </table>
            </section>
        </div>
    </main>
</div>
<script src="/assets/js/admin-ui.js"></script>
</body>
</html>
