<?php
/** @var void template for ACC Dev Portal */
if (!defined('ACC_PATH')) {
    exit;
}

$authed = Acc_Dev_Portal::require_auth();
$user   = $authed ? Acc_Dev_Portal::current_user() : null;
$csrf   = Acc_Dev_Portal::csrf_token();
$doc    = $authed ? Acc_Dev_Portal::get_doc() : null;
?>
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
    <meta charset="utf-8">
    <title>ACC Dev Hub — مرکز مستندات و پشتیبان‌گیری</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="noindex,nofollow">
    <meta name="theme-color" content="#0B1220">
    <link rel="stylesheet" href="/acc-engine/assets/css/base/fonts-local.css?v=2">
    <link rel="stylesheet" href="/acc-engine/assets/css/base/dev-applied.css?v=6">
</head>
<body>
<?php if (!$authed): ?>
    <div class="dev-login-wrap">
        <form class="dev-login" id="dev-login-form" method="post" action="">
            <div class="dev-brand" style="margin-bottom:16px;color:inherit">
                <span class="dev-brand__mark">DV</span>
                <span>
                    <div class="dev-brand__name" style="color:var(--dev-ink)">ACC <em>Dev Hub</em></div>
                    <div class="dev-brand__sub" style="color:var(--dev-muted)">ورود مدیران سیستم</div>
                </span>
            </div>
            <h1>ورود به پورتال توسعه</h1>
            <p>فقط کاربران ممتاز (مثل <code>ostadowner</code> یا نقش <code>super_admin</code>) مجازند.</p>
            <input type="hidden" name="csrf" value="<?php echo esc_attr($csrf); ?>">
            <label for="username">نام کاربری</label>
            <input class="dev-input" id="username" name="username" value="" autocomplete="username" required>
            <label for="password">رمز عبور</label>
            <input class="dev-input" id="password" name="password" type="password" autocomplete="current-password" placeholder="رمز را وارد کنید" required>
            <div class="dev-status is-error" id="login-err" hidden></div>
            <button class="dev-btn" type="submit">ورود</button>
        </form>
    </div>
    <script>
    (function () {
        const form = document.getElementById('dev-login-form');
        const err = document.getElementById('login-err');
        form.addEventListener('submit', async (e) => {
            e.preventDefault();
            err.hidden = true;
            const fd = new FormData(form);
            fd.set('action', 'login');
            const res = await fetch(location.pathname, {
                method: 'POST',
                body: fd,
                credentials: 'same-origin',
                headers: { Accept: 'application/json', 'X-Requested-With': 'XMLHttpRequest' }
            });
            const data = await res.json().catch(() => ({ ok: false, message: 'خطا' }));
            if (!data.ok) {
                err.hidden = false;
                err.textContent = data.message || 'ورود ناموفق';
                return;
            }
            location.reload();
        });
    })();
    </script>
<?php else: ?>
    <div id="dev-root"></div>
    <script>
        window.ACC_DEV_CFG = {
            base: '/acc-engine/',
            endpoint: '/acc-engine/dev.php',
            csrf: <?php echo wp_json_encode($csrf); ?>,
            user: <?php echo wp_json_encode([
                'id' => (int) ($user['ID'] ?? 0),
                'user_login' => (string) ($user['user_login'] ?? ''),
                'display_name' => (string) ($user['display_name'] ?? ''),
            ], JSON_UNESCAPED_UNICODE); ?>,
            doc: <?php echo wp_json_encode($doc ? [
                'id' => (int) $doc['id'],
                'title' => $doc['title'],
                'version' => (int) $doc['version'],
                'synced_at' => $doc['synced_at'],
            ] : null, JSON_UNESCAPED_UNICODE); ?>,
            initialTab: 'docs'
        };
    </script>
    <script src="/acc-engine/assets/js/views/DevPortalApp.js?v=6"></script>
<?php endif; ?>
</body>
</html>
