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

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-tachometer-alt"></i>
    控制台
</h1>

<!-- 快捷操作 -->
<div class="card">
    <div class="card-header">
        <i class="fas fa-bolt"></i> 快捷操作
    </div>
    <div class="card-body">
        <div class="action-buttons">
            <button class="btn btn-primary" onclick="zcClearCache()">
                <i class="fas fa-sync-alt"></i> 清除缓存
            </button>
            <a href="index.php?r=manage/user/index" class="btn btn-outline-primary">
                <i class="fas fa-users"></i> 用户管理
            </a>
            <a href="index.php?r=manage/items/index" class="btn btn-outline-success">
                <i class="fas fa-shopping-bag"></i> 商品管理
            </a>
            <a href="index.php?r=manage/find/addarticle" class="btn btn-outline-warning">
                <i class="fas fa-edit"></i> 发布文章
            </a>
            <a href="index.php?r=manage/set/index" class="btn btn-outline-info">
                <i class="fas fa-cog"></i> 系统设置
            </a>
        </div>
    </div>
</div>

<!-- 系统信息面板 -->
<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <i class="fas fa-server"></i> 系统信息
            </div>
            <div class="card-body">
                <div class="table-responsive">
                    <table class="table table-bordered">
                        <tbody>
                            <tr>
                                <th class="w-150">本地版本</th>
                                <td>
                                    <span class="badge badge-secondary">ZhiCms <?php echo htmlspecialchars($localVersion, ENT_QUOTES, 'UTF-8'); ?></span>
                                    <?php if(!empty($serverVersion) && $serverVersion != $localVersion): ?>
                                        <span class="text-muted ml-1">最新：<?php echo htmlspecialchars($serverVersion, ENT_QUOTES, 'UTF-8'); ?></span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th>更新标题</th>
                                <td>
                                    <?php if(!empty($vinfo)): ?>
                                        <strong><?php echo htmlspecialchars($vinfo, ENT_QUOTES, 'UTF-8'); ?></strong>
                                    <?php else: ?>
                                        <span class="text-muted">—</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th>更新时间</th>
                                <td>
                                    <?php if(!empty($updateTime)): ?>
                                        <?php echo htmlspecialchars($updateTime, ENT_QUOTES, 'UTF-8'); ?>
                                    <?php else: ?>
                                        <span class="text-muted">—</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th>更新内容</th>
                                <td>
                                    <?php if(!empty($updateInfo)): ?>
                                        <div class="update-info-box" style="max-height:200px;overflow:auto;white-space:pre-wrap;font-size:13px;line-height:1.6;background:#f7f8fa;border:1px solid #eaecef;border-radius:6px;padding:10px 12px;color:#444;"><?php echo nl2br(htmlspecialchars($updateInfo, ENT_QUOTES, 'UTF-8')); ?></div>
                                    <?php endif; ?>
                                    <?php if(!empty($vlistHtml)): ?>
                                        <ul class="update-vlist" style="margin:8px 0 0;padding-left:20px;"><?php echo $vlistHtml; ?></ul>
                                    <?php elseif(!empty($vlist) && is_array($vlist)): ?>
                                        <ul class="update-vlist" style="margin:8px 0 0;padding-left:20px;">
                                            <?php foreach($vlist as $vi): ?>
                                            <li style="font-size:13px;line-height:1.8;color:#444;"><?php echo htmlspecialchars($vi, ENT_QUOTES, 'UTF-8'); ?></li>
                                            <?php endforeach; ?>
                                        </ul>
                                    <?php elseif(empty($updateInfo)): ?>
                                        <span class="text-muted">暂无更新信息</span>
                                    <?php endif; ?>
                                    <?php if(!empty($patchMd5)): ?>
                                        <div class="form-text text-muted">补丁包 MD5：<code><?php echo htmlspecialchars($patchMd5, ENT_QUOTES, 'UTF-8'); ?></code></div>
                                    <?php endif; ?>
                                </td>
                            </tr>
                            <tr>
                                <th>升级操作</th>
                                <td>
                                    <button class="btn btn-sm btn-outline-secondary mr-2" id="btn-check-update" onclick="zcCheckUpdate()">
                                        <i class="fas fa-cloud-download-alt"></i> 检查更新
                                    </button>
                                    <?php if(empty($installed)): ?>
                                        <button class="btn btn-sm btn-success" onclick="doUpdate('install')">
                                            <i class="fas fa-download"></i> 立即安装
                                        </button>
                                        <div class="form-text">检测到尚未安装，安装将下载完整程序包并写入数据库与版本号（用户配置不会被覆盖）。</div>
                                    <?php elseif(!empty($canUpgrade)): ?>
                                        <button class="btn btn-sm btn-primary" onclick="doUpdate('upgrade')">
                                            <i class="fas fa-arrow-up"></i>
                                            <?php if(!empty($isPatch)): ?>
                                                立即打补丁（同版本）
                                            <?php else: ?>
                                                立即升级到 <?php echo htmlspecialchars($serverVersion, ENT_QUOTES, 'UTF-8'); ?>
                                            <?php endif; ?>
                                        </button>
                                        <?php if(!empty($isPatch)): ?>
                                            <span class="badge badge-warning ml-1">补丁更新</span>
                                            <div class="form-text">版本号未变，但服务器有新的补丁包（MD5 未应用过），点击后将记录该补丁 MD5，避免重复提示。</div>
                                        <?php else: ?>
                                            <div class="form-text">后台升级：下载更新包、安全覆盖文件并执行数据库升级（用户配置不会被覆盖）。</div>
                                        <?php endif; ?>
                                    <?php else: ?>
                                        <span class="text-muted ml-2"><i class="fas fa-ban"></i> 当前不可升级（官网已关闭升级通道或已应用最新补丁）</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">
                <i class="fas fa-chart-pie"></i> 站点概况
            </div>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>所有文章</span>
                        <span class="badge badge-primary badge-pill"><?php echo isset($articleCount) ? $articleCount : 0; ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>所有商品</span>
                        <span class="badge badge-success badge-pill"><?php echo isset($goodsCount) ? $goodsCount : 0; ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>注册用户</span>  
                        <span class="badge badge-info badge-pill"><?php echo isset($userCount) ? $userCount : 0; ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>今日新增</span>
                        <span class="badge badge-warning badge-pill"><?php echo isset($todayCount) ? $todayCount : 0; ?></span>
                    </li>
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <span>今日采集</span>
                        <span class="badge badge-danger badge-pill"><?php echo isset($todayGoodsCount) ? $todayGoodsCount : 0; ?></span>
                    </li>
                </ul>
            </div>
        </div>
        
        <div class="card">
            <div class="card-header">
                <i class="fas fa-info-circle"></i> 系统提示
            </div>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    <li class="list-group-item">
                        <i class="fas fa-check-circle text-success mr-2"></i>
                        系统运行正常
                    </li>
                    <li class="list-group-item">
                        <i class="fas fa-check-circle text-success mr-2"></i>
                        数据库连接正常
                    </li>
                    <li class="list-group-item">
                        <i class="fas fa-check-circle text-success mr-2"></i>
                        缓存功能可用
                    </li>
                    <li class="list-group-item">
                        <i class="fas fa-sync text-warning mr-2"></i>
                        建议定期清理缓存
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>

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

<script>
function doUpdate(mode) {
    mode = mode || 'upgrade';
    var tipText = mode === 'install' ? '正在安装，请稍候…' : '正在升级，请稍候…';
    if(typeof showToast === 'function') {
        showToast(tipText, 'success');
    }
    $.ajax({
        url: 'index.php?r=manage/index/downloadFile',
        type: 'POST',
        data: { mode: mode },
        dataType: 'json',
        headers: { 'X-Requested-With': 'XMLHttpRequest' },
        success: function(ret) {
            if (ret && ret.status == 'y') {
                showToast && showToast(ret.info, 'success');
                setTimeout(function(){ location.reload(); }, 1500);
            } else {
                showToast && showToast((ret && ret.info) ? ret.info : '操作失败', 'danger');
            }
        },
        error: function(xhr) {
            var msg = '请求失败';
            try {
                var m = xhr.responseText.match(/\{.*\}/s);
                if (m) { var j = JSON.parse(m[0]); if (j && j.info) msg = j.info; }
            } catch(e) {}
            showToast && showToast(msg, 'danger');
        }
    });
}
</script>