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


session_start();

if (!isset($_SESSION['member_id']) || empty($_SESSION['member_id'])) {
    header('Location: /member/login.php');
    exit;
}

require_once dirname(__FILE__) . '/../core/db/db.php';
$db = \Core\Db\DB::getInstance();

$error = '';
$success = false;

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $old_password = trim($_POST['old_password'] ?? '');
    $new_password = trim($_POST['new_password'] ?? '');
    $confirm_password = trim($_POST['confirm_password'] ?? '');
    
    if (empty($old_password) || empty($new_password)) {
        $error = '请填写所有密码字段';
    } elseif ($new_password !== $confirm_password) {
        $error = '两次输入的新密码不一致';
    } elseif (strlen($new_password) < 6) {
        $error = '新密码长度不能少于6位';
    } else {
        $member = $db->fetch("SELECT password FROM {$db->table('member')} WHERE id = ?", [$_SESSION['member_id']]);
        
        // 验证旧密码
        if (!password_verify($old_password, $member['password']) && md5($old_password) !== $member['password']) {
            $error = '原密码错误';
        } else {
            $db->update('member', [
                'password' => password_hash($new_password, PASSWORD_DEFAULT),
                'update_time' => time()
            ], 'id = ?', [$_SESSION['member_id']]);
            
            $success = true;
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改密码 - 会员中心</title>
    <link rel="stylesheet" href="/public/css/site.css">
    <style>
    .password-form { max-width: 500px; margin: 0 auto; background: #fff; padding: 40px; border-radius: 12px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
    .password-title { font-size: 20px; color: #0d3b66; margin-bottom: 30px; text-align: center; }
    .form-group { margin-bottom: 20px; }
    .form-group label { display: block; margin-bottom: 8px; color: #333; font-weight: 500; }
    .form-group input { width: 100%; padding: 12px 15px; border: 1px solid #ddd; border-radius: 6px; font-size: 14px; }
    .form-group input:focus { outline: none; border-color: #00d2d3; }
    .btn-save { width: 100%; padding: 14px; background: linear-gradient(135deg, #00d2d3, #007991); color: #fff; border: none; border-radius: 6px; font-size: 16px; cursor: pointer; }
    .btn-save:hover { background: linear-gradient(135deg, #00b8b9, #006070); }
    .error-msg { background: #f8d7da; color: #721c24; padding: 12px 15px; border-radius: 6px; margin-bottom: 20px; font-size: 13px; }
    .success-msg { background: #d4edda; color: #155724; padding: 12px 15px; border-radius: 6px; margin-bottom: 20px; font-size: 13px; }
    .back-link { display: block; text-align: center; margin-top: 20px; color: #666; text-decoration: none; }
    .back-link:hover { color: #00d2d3; }
    </style>
</head>
<body>
    <header class="site-header">
        <div class="container">
            <div class="header-inner">
                <div class="logo"><h1>企业CMS</h1></div>
                <nav class="header-nav">
                    <a href="/">首页</a>
                    <a href="/member/">会员中心</a>
                </nav>
            </div>
        </div>
    </header>

    <section style="padding: 40px 0;">
        <div class="container">
            <div class="password-form">
                <h2 class="password-title">🔐 修改密码</h2>
                
                <?php if ($error): ?>
                <div class="error-msg"><?php echo htmlspecialchars($error); ?></div>
                <?php endif; ?>
                
                <?php if ($success): ?>
                <div class="success-msg">✓ 密码修改成功！</div>
                <?php endif; ?>
                
                <form method="post">
                    <div class="form-group">
                        <label>原密码</label>
                        <input type="password" name="old_password" required>
                    </div>
                    
                    <div class="form-group">
                        <label>新密码</label>
                        <input type="password" name="new_password" required placeholder="至少6位">
                    </div>
                    
                    <div class="form-group">
                        <label>确认新密码</label>
                        <input type="password" name="confirm_password" required placeholder="再次输入新密码">
                    </div>
                    
                    <button type="submit" class="btn-save">确认修改</button>
                </form>
                
                <a href="/member/" class="back-link">← 返回会员中心</a>
            </div>
        </div>
    </section>

    <footer class="site-footer">
        <div class="container">
            <div class="footer-bottom">
                <p>© <?php echo date('Y'); ?> 企业CMS 版权所有</p>
            </div>
        </div>
    </footer>
</body>
</html>