<?php
include __DIR__ . '/auth.php';

$msg = '';
$error = '';
$account = admin_client_account();

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    $password = (string)($_POST['password'] ?? '');
    $password2 = (string)($_POST['password_confirm'] ?? '');
    $profile = array(
        'account' => trim($_POST['account'] ?? ''),
        'name' => trim($_POST['name'] ?? ''),
        'contact' => trim($_POST['contact'] ?? ''),
        'email' => trim($_POST['email'] ?? ''),
        'phone' => trim($_POST['phone'] ?? ''),
        'domain' => license_current_domain(),
        'registered_at' => $account['registered_at'] ?? date('Y-m-d H:i:s'),
    );

    if($profile['account'] === '' || $profile['name'] === ''){
        $error = '账号和客户名称不能为空';
    }elseif($password === '' && empty($account['password_hash'])){
        $error = '请设置登录密码';
    }elseif($password !== '' && strlen($password) < 6){
        $error = '登录密码至少需要 6 位';
    }elseif($password !== $password2){
        $error = '两次输入的密码不一致';
    }else{
        if($password !== ''){
            $profile['password_hash'] = password_hash($password, PASSWORD_DEFAULT);
            $profile['password_set_at'] = date('Y-m-d H:i:s');
        }
        $syncRet = admin_register_center_sync($profile);
        if(!is_array($syncRet)) $syncRet = array(false, '授权中心注册接口返回异常', array());
        $syncRet = array_pad($syncRet, 3, array());
        list($reqOk, $reqMsg, $json) = $syncRet;
        if(!is_array($json)) $json = array();
        $profile['sync_status'] = $reqOk && !empty($json['ok']) ? 'success' : 'failed';
        $profile['sync_message'] = $reqOk ? ($json['message'] ?? $reqMsg) : $reqMsg;
        $profile['customer_id'] = $json['customer_id'] ?? ($account['customer_id'] ?? '');
        $profile['last_sync_at'] = date('Y-m-d H:i:s');
        admin_client_account_save($profile);
        admin_oplog('client_register', $profile['account'] . ' / ' . $profile['sync_status']);
        $account = admin_client_account();
        $_SESSION = array();
        if(session_id() !== '') session_destroy();
        $registeredFlag = $profile['sync_status'] === 'success' ? '1' : 'local';
        header('Location: ' . cms_admin_url('login.php?registered=' . $registeredFlag));
        exit;
    }
}

$isLogged = !empty($_SESSION['admin_login']);
?>
<!doctype html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<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="entry-page">
<div class="entry-shell">
    <aside class="entry-brand">
        <div>
            <div class="entry-mark">炎</div>
            <h1>炎枢站群</h1>
            <p>完善客户资料后，系统会保存到本地后台，并同步到授权中心客户档案。</p>
            <div class="entry-meta">
                <div><strong>客户档案</strong><span>账号、客户名称、联系人、邮箱和电话用于后续授权维护。</span></div>
                <div><strong>账号同步</strong><span>注册成功后将跳转登录页，请使用新账号和密码登录后台。</span></div>
                <div><strong>授权中心</strong><span>如接口暂不可用，本地注册仍会保留，后续可再次同步。</span></div>
            </div>
        </div>
        <p>YanShu Site Cluster CMS</p>
    </aside>
    <main class="entry-content">
        <section class="entry-card">
            <h2>客户注册</h2>
            <p>请填写客户资料和登录密码。账号将作为后续登录后台和授权同步的身份标识。</p>
            <?php if($msg): ?><div class="notice"><?php echo admin_h($msg); ?></div><?php endif; ?>
            <?php if($error): ?><div class="error"><?php echo admin_h($error); ?></div><?php endif; ?>
            <form method="post">
                <label>登录 / 显示账号
                    <input name="account" value="<?php echo admin_h($account['account'] ?? ADMIN_USER); ?>" placeholder="例如：admin 或公司简称">
                </label>
                <label>登录密码
                    <input name="password" type="password" autocomplete="new-password" placeholder="至少 6 位，注册后用于登录后台">
                </label>
                <label>确认密码
                    <input name="password_confirm" type="password" autocomplete="new-password" placeholder="请再次输入密码">
                </label>
                <label>客户名称
                    <input name="name" value="<?php echo admin_h($account['name'] ?? ''); ?>" placeholder="公司名称或项目名称">
                </label>
                <label>联系人
                    <input name="contact" value="<?php echo admin_h($account['contact'] ?? ''); ?>" placeholder="负责人姓名">
                </label>
                <label>邮箱
                    <input name="email" value="<?php echo admin_h($account['email'] ?? ''); ?>" placeholder="用于授权通知">
                </label>
                <label>电话
                    <input name="phone" value="<?php echo admin_h($account['phone'] ?? ''); ?>" placeholder="联系电话">
                </label>
                <button>注册并同步授权中心</button>
            </form>
            <p class="entry-link">
                <?php if($isLogged): ?>
                    <a href="<?php echo admin_h(cms_admin_url('index.php')); ?>">返回后台首页</a>
                <?php else: ?>
                    <a href="<?php echo admin_h(cms_admin_url('login.php')); ?>">已有账号，返回登录</a>
                <?php endif; ?>
            </p>
        </section>
    </main>
</div>
</body>
</html>
