<?php
include_once __DIR__ . '/../inc/admin_path.php';

function admin_header($title = '企业站后台'){
    admin_begin_csrf_output_filter();
    if(($_SERVER['REQUEST_METHOD'] ?? '') === 'POST' && ($_POST['action'] ?? '') === 'admin_ui_theme'){
        admin_ui_save_theme($_POST['admin_ui_theme'] ?? 'business-clean');
        if(function_exists('admin_oplog')) admin_oplog('change_admin_ui_theme', $_POST['admin_ui_theme'] ?? '');
        header('Location: ' . ($_SERVER['REQUEST_URI'] ?? cms_admin_url('index.php')));
        exit;
    }
    $current = admin_current();
    $uiThemes = admin_ui_themes();
    $uiTheme = admin_ui_theme();
?>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title><?php echo admin_h($title); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="<?php echo admin_h(cms_admin_url('assets/admin.css')); ?>">
</head>
<body class="admin-ui-<?php echo admin_h($uiTheme); ?>">
<div class="admin-shell">
    <aside class="sidebar">
        <div class="brand">
            <div class="brand-logo">E</div>
            <div>
                <strong>炎枢后台</strong>
                <span>Enterprise Admin</span>
            </div>
        </div>
        <nav class="side-nav">
            <div class="side-group-title">工作台</div>
            <a data-icon="首" class="<?php echo $current==='index.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('index.php')); ?>">后台首页</a>
            <a data-icon="站" class="<?php echo in_array($current,array('site_config.php','sites.php','batch_domains.php'),true)?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('site_config.php')); ?>">站群管理</a>

            <div class="side-group-title">内容运营</div>
            <a data-icon="文" class="<?php echo $current==='content_center.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('content_center.php')); ?>">内容中心</a>
            <a data-icon="链" class="<?php echo $current==='links.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('links.php')); ?>">友情链接</a>

            <div class="side-group-title">增长工具</div>
            <a data-icon="推" class="<?php echo in_array($current,array('push_center.php','baidu_push.php'),true)?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('push_center.php')); ?>">推送管理</a>
            <a data-icon="蛛" class="<?php echo $current==='spider.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('spider.php')); ?>">蜘蛛统计</a>
            <a data-icon="引" class="<?php echo $current==='spider_force.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('spider_force.php')); ?>">强引设置</a>
            <a data-icon="缓" class="<?php echo $current==='cache.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('cache.php')); ?>">静态缓存</a>

            <div class="side-group-title">系统维护</div>
            <a data-icon="授" class="<?php echo $current==='license.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('license.php')); ?>">系统授权</a>
            <a data-icon="密" class="<?php echo $current==='password.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('password.php')); ?>">修改密码</a>
            <a data-icon="检" class="<?php echo $current==='health.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('health.php')); ?>">站点体检</a>
            <a data-icon="安" class="<?php echo $current==='security.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('security.php')); ?>">安全建议</a>
            <a data-icon="志" class="<?php echo $current==='oplog.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('oplog.php')); ?>">操作日志</a>
            <a data-icon="初" class="<?php echo $current==='initial_setup.php'?'active':''; ?>" href="<?php echo admin_h(cms_admin_url('initial_setup.php')); ?>">初始设置</a>
            <a data-icon="前" href="/" target="_blank">访问前台</a>
        </nav>
        <div class="side-footer">
            <a href="<?php echo admin_h(cms_admin_url('logout.php')); ?>">退出登录</a>
        </div>
    </aside>
    <main class="main">
        <header class="topbar-admin">
            <div>
                <h1><?php echo admin_h($title); ?></h1>
                <p>企业多站 CMS：内容中心、主题同步、定时发布与站点维护</p>
            </div>
            <div class="topbar-account">
                <div class="topbar-shortcuts">
                    <a class="btn ghost small" href="<?php echo admin_h(cms_admin_url('content_center.php')); ?>">发内容</a>
                    <a class="btn ghost small" href="<?php echo admin_h(cms_admin_url('site_config.php')); ?>">管站群</a>
                </div>
                <form method="post" class="ui-theme-form">
                    <input type="hidden" name="action" value="admin_ui_theme">
                    <label>后台风格
                        <select name="admin_ui_theme" onchange="this.form.submit()">
                            <?php foreach($uiThemes as $k=>$v): ?>
                            <option value="<?php echo admin_h($k); ?>" <?php echo $uiTheme===$k?'selected':''; ?>><?php echo admin_h($v); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                </form>
                <div class="account-pill">
                    <span>账号</span>
                    <strong><?php echo admin_h(admin_current_account_name()); ?></strong>
                </div>
                <a class="btn ghost" href="<?php echo admin_h(cms_admin_url('logout.php')); ?>">退出</a>
            </div>
        </header>
        <section class="content">
<?php
}

function admin_footer(){
?>
        </section>
    </main>
</div>
<div class="admin-confirm-mask" id="adminConfirmMask" hidden>
    <div class="admin-confirm-dialog" role="dialog" aria-modal="true" aria-labelledby="adminConfirmTitle">
        <h3 id="adminConfirmTitle">确认操作</h3>
        <p id="adminConfirmText">确定执行该操作吗？</p>
        <div class="admin-confirm-actions">
            <button type="button" class="btn ghost" id="adminConfirmCancel">取消</button>
            <button type="button" class="btn danger" id="adminConfirmOk">确定</button>
        </div>
    </div>
</div>
<script>
(function(){
    var mask = document.getElementById('adminConfirmMask');
    if(!mask) return;
    var text = document.getElementById('adminConfirmText');
    var ok = document.getElementById('adminConfirmOk');
    var cancel = document.getElementById('adminConfirmCancel');
    var pending = null;
    var scrollKey = 'yanscms-scroll:' + location.pathname;
    function rememberScroll(){
        try { sessionStorage.setItem(scrollKey, String(window.scrollY || document.documentElement.scrollTop || 0)); } catch(e) {}
    }
    function restoreScroll(){
        try {
            var y = sessionStorage.getItem(scrollKey);
            if(y === null) return;
            sessionStorage.removeItem(scrollKey);
            window.scrollTo(0, Math.max(0, parseInt(y, 10) || 0));
        } catch(e) {}
    }
    if(document.readyState === 'loading') document.addEventListener('DOMContentLoaded', restoreScroll);
    else restoreScroll();
    function close(){
        mask.hidden = true;
        pending = null;
    }
    function open(message, callback){
        text.textContent = message || '确定执行该操作吗？';
        pending = callback;
        mask.hidden = false;
        ok.focus();
    }
    ok.addEventListener('click', function(){
        var fn = pending;
        close();
        if(fn) fn();
    });
    cancel.addEventListener('click', close);
    mask.addEventListener('click', function(e){ if(e.target === mask) close(); });
    document.addEventListener('keydown', function(e){ if(e.key === 'Escape' && !mask.hidden) close(); });
    document.addEventListener('submit', function(e){
        var form = e.target;
        var submitter = e.submitter || null;
        var message = form.getAttribute('data-confirm') || (submitter ? submitter.getAttribute('data-confirm') : '');
        if(!message || form.getAttribute('data-confirmed') === '1'){
            rememberScroll();
            return;
        }
        e.preventDefault();
        open(message, function(){
            rememberScroll();
            form.setAttribute('data-confirmed', '1');
            if(form.requestSubmit) form.requestSubmit(submitter || undefined);
            else form.submit();
        });
    }, true);
})();
</script>
</body>
</html>
<?php
}
?>
