<?php
/**
 * 芋头CMS
 * ============================================================================
 * 版权所有 2016-2028 沅陵县乌托邦网络服务有限公司，并保留所有权利。
 * 网站地址: http://www.yutoucms.cn
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 芋头 <mxgsf@qq.com>
 * Date: 2026-5-20
 */


$adminId = $_SESSION['admin_id'] ?? 0;
$admin = $db->fetch("SELECT * FROM {$db->table('admin')} WHERE id = ?", [$adminId]);
if (!$admin) {
    if (isset($_GET['ajax'])) {
        echo '<div style="padding:20px;text-align:center;color:#ef4444;">管理员不存在</div>';
        exit;
    }
    echo '<script>alert("管理员不存在");history.back();</script>';
    exit;
}

$isAjax = isset($_GET['ajax']);

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    requireAdminCsrfToken(true);
    $realname = trim($_POST['realname'] ?? '');
    $pen_name = trim($_POST['pen_name'] ?? '');
    $phone = trim($_POST['phone'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $job_content = trim($_POST['job_content'] ?? '');
    $new_password = trim($_POST['new_password'] ?? '');
    $confirm_password = trim($_POST['confirm_password'] ?? '');

    $data = [
        'realname' => $realname,
        'pen_name' => $pen_name,
        'phone' => $phone,
        'email' => $email,
        'job_content' => $job_content,
        'update_time' => time()
    ];

    if (!empty($new_password)) {
        if (strlen($new_password) < 6) {
            echo json_encode(['code' => 400, 'msg' => '密码长度不能少于6位']);
            exit;
        }
        if ($new_password !== $confirm_password) {
            echo json_encode(['code' => 400, 'msg' => '两次密码输入不一致']);
            exit;
        }
        $data['password'] = password_hash($new_password, PASSWORD_DEFAULT);
    }

    if (!empty($_POST['avatar'])) {
        $data['avatar'] = trim($_POST['avatar']);
    }

    $db->update('admin', $data, 'id = ?', [$adminId]);

    if (!empty($realname)) {
        $_SESSION['admin_realname'] = $realname;
    }

    echo json_encode(['code' => 200, 'msg' => '保存成功']);
    exit;
}

if ($isAjax) {
    $avatarUrl = !empty($admin['avatar']) ? htmlspecialchars($admin['avatar']) : '';
    ?>
    <style>
    .panel-form-group{margin-bottom:14px;}
    .panel-form-group label{display:block;font-size:12px;color:#64748b;margin-bottom:4px;font-weight:500;}
    .panel-form-control{width:100%;padding:9px 12px;border:1px solid #e2e8f0;border-radius:6px;font-size:13px;outline:none;box-sizing:border-box;}
    .panel-form-control:focus{border-color:#4f46e5;}
    .panel-form-row{display:grid;grid-template-columns:1fr 1fr;gap:12px;}
    .panel-save-btn{width:100%;padding:11px;background:#4f46e5;color:#fff;border:none;border-radius:8px;font-size:14px;font-weight:500;cursor:pointer;margin-top:16px;}
    .panel-save-btn:hover{background:#4338ca;}
    .panel-password-section{border-top:1px solid #e2e8f0;padding-top:14px;margin-top:14px;}
    .panel-password-title{font-size:12px;color:#64748b;margin-bottom:10px;font-weight:500;}
    </style>
    <form id="profileForm" onsubmit="return saveProfile(event)">
        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getAdminCsrfToken()); ?>">
        <div style="text-align:center;margin-bottom:20px;padding-bottom:16px;border-bottom:1px solid #e2e8f0;">
            <div style="width:80px;height:80px;border-radius:50%;background:#f1f5f9;margin:0 auto 10px;display:flex;align-items:center;justify-content:center;font-size:36px;overflow:hidden;" id="panelAvatarPreview">
                <?php if ($avatarUrl): ?>
                <img src="<?php echo $avatarUrl; ?>" style="width:100%;height:100%;object-fit:cover;">
                <?php else: ?>
                <svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                <?php endif; ?>
            </div>
            <div style="font-size:13px;color:#1e293b;font-weight:600;"><?php echo htmlspecialchars($admin['realname'] ?: $admin['username']); ?></div>
            <input type="text" name="avatar" id="panelAvatarInput" value="<?php echo $avatarUrl; ?>" placeholder="输入头像URL" style="width:100%;padding:8px;border:1px solid #e2e8f0;border-radius:6px;font-size:12px;margin-top:8px;box-sizing:border-box;" oninput="updatePanelAvatar(this.value)">
        </div>

        <div class="panel-form-group">
            <label>用户名</label>
            <input type="text" class="panel-form-control" value="<?php echo htmlspecialchars($admin['username']); ?>" disabled style="background:#f1f5f9;color:#94a3b8;">
        </div>

        <div class="panel-form-row">
            <div class="panel-form-group"><label>真实姓名</label><input type="text" name="realname" class="panel-form-control" value="<?php echo htmlspecialchars($admin['realname'] ?? ''); ?>"></div>
            <div class="panel-form-group"><label>笔名</label><input type="text" name="pen_name" class="panel-form-control" value="<?php echo htmlspecialchars($admin['pen_name'] ?? ''); ?>"></div>
        </div>

        <div class="panel-form-row">
            <div class="panel-form-group"><label>手机号</label><input type="text" name="phone" class="panel-form-control" value="<?php echo htmlspecialchars($admin['phone'] ?? ''); ?>"></div>
            <div class="panel-form-group"><label>邮箱</label><input type="email" name="email" class="panel-form-control" value="<?php echo htmlspecialchars($admin['email'] ?? ''); ?>"></div>
        </div>

        <div class="panel-form-group">
            <label>工作内容</label>
            <input type="text" name="job_content" class="panel-form-control" value="<?php echo htmlspecialchars($admin['job_content'] ?? ''); ?>">
        </div>

        <div class="panel-password-section">
            <div class="panel-password-title">修改密码（留空则不修改）</div>
            <div class="panel-form-group"><label>新密码</label><input type="password" name="new_password" class="panel-form-control" placeholder="输入新密码" autocomplete="new-password"></div>
            <div class="panel-form-group"><label>确认密码</label><input type="password" name="confirm_password" class="panel-form-control" placeholder="再次输入新密码" autocomplete="new-password"></div>
        </div>

        <button type="submit" class="panel-save-btn"><svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg> 保存修改</button>
    </form>
    <script>
    function updatePanelAvatar(val) {
        var p = document.getElementById('panelAvatarPreview');
        if (val) {
            p.innerHTML = '<img src="' + val + '" style="width:100%;height:100%;object-fit:cover;">';
        } else {
            p.innerHTML = '<svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>';
        }
    }
    function saveProfile(e) {
        e.preventDefault();
        var fd = new FormData(document.getElementById('profileForm'));
        fetch('/admin/main.php?act=profile', {method: 'POST', body: fd}).then(function(r) { return r.json(); }).then(function(d) {
            if (d.code == 200) {
                alert('保存成功');
                openProfilePanel();
            } else {
                alert(d.msg);
            }
        });
    }
    </script>
    <?php
    exit;
}
?>
<div class="page-container fade-in">
    <div class="page-header">
        <div>
            <h2><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg> 个人信息</h2>
            <p style="font-size:13px;color:var(--text-light);margin:4px 0 0 0;">管理您的账户信息</p>
        </div>
    </div>
    <form method="post" enctype="multipart/form-data">
        <input type="hidden" name="csrf_token" value="<?php echo htmlspecialchars(getAdminCsrfToken()); ?>">
        <div style="display:grid;grid-template-columns:1fr 320px;gap:20px;">
            <div>
                <div class="content-card">
                    <div class="card-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2"/><rect x="8" y="2" width="8" height="4" rx="1" ry="1"/></svg> 基本信息</div>
                    <div class="card-body">
                        <div style="display:grid;grid-template-columns:1fr 1fr;gap:16px;">
                            <div class="form-group">
                                <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">用户名</label>
                                <input type="text" class="form-control" value="<?php echo htmlspecialchars($admin['username']); ?>" disabled style="background:var(--gray-50);color:var(--text-light);">
                            </div>
                            <div class="form-group">
                                <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">真实姓名</label>
                                <input type="text" name="realname" class="form-control" value="<?php echo htmlspecialchars($admin['realname'] ?? ''); ?>">
                            </div>
                            <div class="form-group">
                                <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">笔名</label>
                                <input type="text" name="pen_name" class="form-control" value="<?php echo htmlspecialchars($admin['pen_name'] ?? ''); ?>">
                            </div>
                            <div class="form-group">
                                <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">手机号</label>
                                <input type="text" name="phone" class="form-control" value="<?php echo htmlspecialchars($admin['phone'] ?? ''); ?>">
                            </div>
                        </div>
                        <div class="form-group">
                            <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">邮箱</label>
                            <input type="email" name="email" class="form-control" value="<?php echo htmlspecialchars($admin['email'] ?? ''); ?>">
                        </div>
                        <div class="form-group" style="margin-bottom:0;">
                            <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">工作内容</label>
                            <input type="text" name="job_content" class="form-control" value="<?php echo htmlspecialchars($admin['job_content'] ?? ''); ?>">
                        </div>
                    </div>
                </div>
                <div class="content-card" style="margin-top:16px;">
                    <div class="card-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="11" width="18" height="11" rx="2" ry="2"/><path d="M7 11V7a5 5 0 0 1 10 0v4"/></svg> 修改密码</div>
                    <div class="card-body">
                        <div class="form-group">
                            <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">新密码</label>
                            <input type="password" name="new_password" class="form-control" placeholder="留空则不修改密码" autocomplete="new-password">
                        </div>
                        <div class="form-group" style="margin-bottom:0;">
                            <label style="font-size:13px;font-weight:500;color:var(--text);margin-bottom:6px;display:block;">确认密码</label>
                            <input type="password" name="confirm_password" class="form-control" placeholder="再次输入新密码" autocomplete="new-password">
                        </div>
                    </div>
                </div>
            </div>
            <div>
                <div class="content-card">
                    <div class="card-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/></svg> 头像设置</div>
                    <div class="card-body" style="text-align:center;">
                        <div id="avatarPreview" style="width:120px;height:120px;border-radius:50%;overflow:hidden;margin:0 auto 12px;border:2px solid var(--border-light);cursor:pointer;background:var(--gray-50);display:flex;align-items:center;justify-content:center;" onclick="document.getElementById('avatarInput').click();">
                            <?php if (!empty($admin['avatar'])): ?>
                            <img src="<?php echo htmlspecialchars($admin['avatar']); ?>" style="width:100%;height:100%;object-fit:cover;">
                            <?php else: ?>
                            <svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>
                            <?php endif; ?>
                        </div>
                        <input type="text" name="avatar" id="avatarInput" class="form-control" value="<?php echo htmlspecialchars($admin['avatar'] ?? ''); ?>" placeholder="输入头像图片URL" style="text-align:center;">
                        <p style="font-size:12px;color:var(--text-light);margin-top:8px;">支持 JPG、PNG、GIF，建议正方形</p>
                    </div>
                </div>
                <div class="content-card" style="margin-top:16px;">
                    <div class="card-header"><svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg> 账户信息</div>
                    <div class="card-body" style="padding:12px 16px;">
                        <div style="display:flex;justify-content:space-between;padding:10px 0;border-bottom:1px solid var(--border-light);">
                            <span style="font-size:13px;color:var(--text-light);">最后登录</span>
                            <span style="font-size:13px;color:var(--dark);"><?php echo $admin['last_login_time'] ? date('Y-m-d H:i', $admin['last_login_time']) : '-'; ?></span>
                        </div>
                        <div style="display:flex;justify-content:space-between;padding:10px 0;">
                            <span style="font-size:13px;color:var(--text-light);">最后IP</span>
                            <span style="font-size:13px;color:var(--dark);"><?php echo htmlspecialchars($admin['last_login_ip'] ?? '-'); ?></span>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div style="margin-top:20px;display:flex;gap:12px;">
            <button type="submit" class="btn btn-primary" style="padding:12px 32px;font-size:15px;"><svg style="width:16px;height:16px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><polyline points="20 6 9 17 4 12"/></svg> 保存修改</button>
            <a href="?act=dashboard" class="btn btn-secondary" style="padding:12px 24px;font-size:15px;text-decoration:none;">取消</a>
        </div>
    </form>
</div>
<script>
document.getElementById('avatarInput').addEventListener('input', function(e) {
    var preview = document.getElementById('avatarPreview');
    if (e.target.value) {
        preview.innerHTML = '<img src="' + e.target.value + '" style="width:100%;height:100%;object-fit:cover;">';
    } else {
        preview.innerHTML = '<svg style="width:22px;height:22px;" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/></svg>';
    }
});
</script>
