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

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-link"></i>
    链接管理
</h1>

<!-- 操作栏 -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <div class="action-buttons">
        <a href="index.php?r=manage/ad/addlink" class="btn btn-primary">
            <i class="fas fa-plus"></i> 添加链接
        </a>
        <button class="btn btn-outline-info" onclick="checkLinks()">
            <i class="fas fa-sync-alt"></i> 检测链接
        </button>
        <button class="btn btn-outline-secondary" onclick="exportLinks()">
            <i class="fas fa-file-export"></i> 导出
        </button>
    </div>
    
    <div class="search-form w-300">
        <div class="input-group">
            <input type="text" class="form-control" id="keyword" placeholder="搜索链接标题或URL..." value="<?php echo isset($keyword) ? $keyword : ''; ?>">
            <div class="input-group-append">
                <button class="btn btn-outline-secondary" type="button" onclick="searchLinks()">
                    <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['total']) ? number_format($Page['total']) : 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="text-center w-90">排序</th>
                        <th class="w-200">链接地址</th>
                        <th class="text-center w-120">状态</th>
                        <th class="text-center w-150">操作</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="font-weight-600 mb-1">
                                <?php echo !empty($item['title']) ? $item['title'] : '<span class="text-muted">无标题</span>'; ?>
                            </div>
                            <?php if(!empty($item['description'])): ?>
                            <div class="text-muted small">
                                <?php echo mb_substr($item['description'], 0, 60, 'UTF-8'); ?>
                                <?php if(mb_strlen($item['description'], 'UTF-8') > 60): ?>...<?php endif; ?>
                            </div>
                            <?php endif; ?>
                            <div class="mt-1">
                                <span class="badge badge-light text-dark border">
                                    <i class="fas fa-tags mr-1"></i>
                                    友情链接
                                </span>
                            </div>
                        </td>
                        <td class="text-center font-weight-bold text-muted">
                            <?php echo intval($item['px']); ?>
                        </td>
                        <td>
                            <div class="text-truncate mw-200">
                                <a href="<?php echo $item['link']; ?>" target="_blank" class="text-decoration-none" title="<?php echo $item['link']; ?>">
                                    <?php echo $item['link']; ?>
                                </a>
                            </div>
                            <div class="small text-muted mt-1">
                                <i class="fas fa-mouse-pointer mr-1"></i>
                                访问: <?php echo isset($item['clicks']) ? number_format($item['clicks']) : 0; ?>
                            </div>
                        </td>
                        <td class="text-center">
                            <span class="badge badge-success badge-pill">
                                <i class="fas fa-check mr-1"></i>正常
                            </span>
                            <br>
                            <small class="text-muted mt-1">
                                <?php echo date('Y-m', strtotime($item['create_time'] ?? $item['date'])); ?>
                            </small>
                        </td>
                        <td class="text-center">
                            <div class="btn-group btn-group-sm">
                                <a href="index.php?r=manage/ad/editorlink&id=<?php echo $item['id']; ?>" class="btn btn-outline-primary" title="编辑链接">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <a href="<?php echo $item['link']; ?>" target="_blank" class="btn btn-outline-info" title="访问链接">
                                    <i class="fas fa-external-link-alt"></i>
                                </a>
                                <button class="btn btn-outline-danger" onclick="deleteLink(<?php echo $item['id']; ?>)" title="删除链接">
                                    <i class="fas fa-trash"></i>
                                </button>
                            </div>
                        </td>
                    </tr>
                    <?php endforeach; ?>
                    <?php else: ?>
                    <tr>
                        <td colspan="6" class="text-center py-5">
                            <div class="empty-state">
                                <i class="fas fa-link 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']): ?>
        <nav aria-label="分页导航" class="mt-4">
            <div class="text-center">
                <div class="pagination d-inline-flex">
                    <?php echo $Page['page']; ?>
                </div>
                <?php if(isset($Page['count'])): ?>
                <div class="text-muted mt-2">
                    <small>共 <?php echo number_format($Page['count']); ?> 条数据</small>
                </div>
                <?php endif; ?>
            </div>
        </nav>
        <?php endif; ?>
    </div>
</div>

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

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

function deleteLink(id) {
    if(!confirm('确定要删除这个链接吗？此操作不可恢复！')) return;
    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'index.php?r=manage/ad/deletelink&id=' + id, true);
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            try {
                var res = JSON.parse(xhr.responseText);
                if (res.status === 'y') {
                    showToast(res.info || '删除成功', 'success');
                    setTimeout(function(){ location.reload(); }, 800);
                } else {
                    showToast(res.info || '删除失败', 'danger');
                }
            } catch (e) {
                showToast('删除失败，请重试', 'danger');
            }
        }
    };
    xhr.send();
}

function checkLinks() {
    showToast('链接检测功能开发中...', 'info');
}

function exportLinks() {
    if(confirm('确定要导出链接数据吗？')) {
        // 实现导出功能
        showToast('导出功能开发中...', 'info');
    }
}

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' : type === 'danger' ? 'exclamation-circle' : 'info-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) {
        searchLinks();
    }
});
</script>