<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <title>优雅的Bootstrap弹窗表单</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css" rel="stylesheet">
    <style>
        :root {
            --primary-color: #4e73df;
            --secondary-color: #2e59d9;
            --success-color: #1cc88a;
            --danger-color: #e74a3b;
            --light-color: #f8f9fc;
            --dark-color: #5a5c69;
        }

        body {
            font-family: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fc;
        }

        /* 自定义弹窗样式 */
        .custom-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1050;
            overflow-y: auto;
        }

        .custom-modal-dialog {
            max-width: 500px;
            margin: 1.75rem auto;
            transition: transform 0.3s ease-out;
        }

        .custom-modal-content {
            border: none;
            border-radius: 0.5rem;
            box-shadow: 0 0.15rem 1.75rem 0 rgba(58, 59, 69, 0.15);
            overflow: hidden;
        }

        .custom-modal-header {
            padding: 1.5rem;
            background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
            color: white;
            position: relative;
        }

        .custom-modal-title {
            font-weight: 700;
            font-size: 1.25rem;
        }

        .btn-close-custom {
            position: absolute;
            top: 1rem;
            right: 1rem;
            font-size: 1.5rem;
            color: rgba(255, 255, 255, 0.8);
            background: none;
            border: none;
            cursor: pointer;
            transition: color 0.2s;
        }

        .btn-close-custom:hover {
            color: white;
        }

        .custom-modal-body {
            padding: 1.5rem;
            background-color: white;
        }

        .custom-modal-footer {
            padding: 1rem 1.5rem;
            background-color: var(--light-color);
            border-top: 1px solid #e3e6f0;
            display: flex;
            justify-content: flex-end;
        }

        /* 表单样式 */
        .form-label {
            font-weight: 600;
            color: var(--dark-color);
            margin-bottom: 0.5rem;
        }

        .form-control-custom {
            border: 1px solid #d1d3e2;
            border-radius: 0.35rem;
            padding: 0.75rem 1rem;
            transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
        }

        .form-control-custom:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 0.2rem rgba(78, 115, 223, 0.25);
        }

        /* 验证样式 */
        .is-invalid {
            border-color: var(--danger-color);
        }

        .is-valid {
            border-color: var(--success-color);
        }

        .invalid-feedback {
            color: var(--danger-color);
            font-size: 0.875rem;
            margin-top: 0.25rem;
        }

        .valid-feedback {
            color: var(--success-color);
            font-size: 0.875rem;
            margin-top: 0.25rem;
        }

        /* 按钮样式 */
        .btn-primary-custom {
            background-color: var(--primary-color);
            border-color: var(--primary-color);
            padding: 0.5rem 1.25rem;
            font-weight: 600;
            transition: all 0.2s;
        }

        .btn-primary-custom:hover {
            background-color: var(--secondary-color);
            border-color: var(--secondary-color);
            transform: translateY(-1px);
        }

        .btn-outline-secondary-custom {
            border-color: #d1d3e2;
            color: var(--dark-color);
            padding: 0.5rem 1.25rem;
            font-weight: 600;
            transition: all 0.2s;
        }

        .btn-outline-secondary-custom:hover {
            background-color: #f8f9fc;
            border-color: #d1d3e2;
        }

        /* 动画效果 */
        @keyframes fadeIn {
            from {
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }

        @keyframes slideInDown {
            from {
                transform: translateY(-50px);
                opacity: 0;
            }
            to {
                transform: translateY(0);
                opacity: 1;
            }
        }

        .modal-show {
            display: block;
            animation: fadeIn 0.3s;
        }

        .modal-show .custom-modal-dialog {
            animation: slideInDown 0.3s;
        }

        /* 浮动标签效果 */
        .form-floating-custom {
            position: relative;
            margin-bottom: 1.5rem;
        }

        .form-floating-custom label {
            position: absolute;
            top: 0.75rem;
            left: 1rem;
            color: #6e707e;
            transition: all 0.2s;
            pointer-events: none;
        }

        .form-floating-custom input:focus ~ label,
        .form-floating-custom input:not(:placeholder-shown) ~ label {
            top: -0.75rem;
            left: 0.75rem;
            font-size: 0.75rem;
            background-color: white;
            padding: 0 0.25rem;
            color: var(--primary-color);
        }

        /* 密码强度指示器 */
        .password-strength {
            height: 4px;
            background-color: #e3e6f0;
            margin-top: 0.5rem;
            border-radius: 2px;
            overflow: hidden;
        }

        .password-strength-bar {
            height: 100%;
            width: 0;
            transition: width 0.3s ease, background-color 0.3s ease;
        }
    </style>
</head>
<body>
<div class="container py-5">
    <div class="row justify-content-center">
        <div class="col-lg-8 text-center">
            <h1 class="display-4 mb-4">优雅的Bootstrap弹窗示例</h1>
            <p class="lead mb-5">点击下方按钮查看带有表单验证的现代化弹窗</p>
            <button class="btn btn-primary btn-lg px-4 py-2" id="openModalBtn">
                <i class="bi bi-box-arrow-in-down-right me-2"></i>打开注册弹窗
            </button>
        </div>
    </div>
</div>

<!-- 自定义弹窗 -->
<div class="custom-modal" id="customModal">
    <div class="custom-modal-dialog">
        <div class="custom-modal-content">
            <div class="custom-modal-header">
                <h5 class="custom-modal-title">
                    <i class="bi bi-person-plus-fill me-2"></i>用户注册
                </h5>
                <button class="btn-close-custom" id="closeModalBtn" type="button">
                    <i class="bi bi-x-lg"></i>
                </button>
            </div>
            <div class="custom-modal-body">
                <form id="registrationForm" novalidate>
                    <div class="form-floating-custom mb-4">
                        <input class="form-control form-control-custom" id="username" placeholder="用户名" required
                               type="text">
                        <label for="username">用户名</label>
                        <div class="invalid-feedback">请输入有效的用户名（4-20个字符）</div>
                        <div class="valid-feedback">用户名可用</div>
                    </div>

                    <div class="form-floating-custom mb-4">
                        <input class="form-control form-control-custom" id="email" placeholder="电子邮箱" required
                               type="email">
                        <label for="email">电子邮箱</label>
                        <div class="invalid-feedback">请输入有效的电子邮箱地址</div>
                        <div class="valid-feedback">邮箱格式正确</div>
                    </div>

                    <div class="form-floating-custom mb-4">
                        <input class="form-control form-control-custom" id="password" minlength="8" placeholder="密码"
                               required type="password">
                        <label for="password">密码</label>
                        <div class="invalid-feedback">密码至少需要8个字符</div>
                        <div class="password-strength">
                            <div class="password-strength-bar"></div>
                        </div>
                    </div>

                    <div class="form-floating-custom mb-4">
                        <input class="form-control form-control-custom" id="confirmPassword" placeholder="确认密码"
                               required type="password">
                        <label for="confirmPassword">确认密码</label>
                        <div class="invalid-feedback">密码不匹配</div>
                    </div>

                    <div class="mb-4">
                        <div class="form-check">
                            <input class="form-check-input" id="terms" required type="checkbox">
                            <label class="form-check-label" for="terms">
                                我已阅读并同意 <a class="text-primary" href="#">服务条款</a> 和 <a class="text-primary"
                                                                                                   href="#">隐私政策</a>
                            </label>
                            <div class="invalid-feedback">必须同意条款才能继续</div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="custom-modal-footer">
                <button class="btn btn-outline-secondary-custom me-2" id="cancelBtn" type="button">
                    取消
                </button>
                <button class="btn btn-primary-custom" id="submitBtn" type="button">
                    <i class="bi bi-check-lg me-1"></i> 提交注册
                </button>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<script>
    // 弹窗控制
    const modal = document.getElementById('customModal');
    const openModalBtn = document.getElementById('openModalBtn');
    const closeModalBtn = document.getElementById('closeModalBtn');
    const cancelBtn = document.getElementById('cancelBtn');
    const submitBtn = document.getElementById('submitBtn');
    const form = document.getElementById('registrationForm');

    // 打开弹窗
    openModalBtn.addEventListener('click', () => {
        modal.classList.add('modal-show');
        document.body.style.overflow = 'hidden';
    });

    // 关闭弹窗
    function closeModal() {
        modal.classList.remove('modal-show');
        document.body.style.overflow = 'auto';
        form.reset();
        resetValidation();
    }

    closeModalBtn.addEventListener('click', closeModal);
    cancelBtn.addEventListener('click', closeModal);

    // 点击弹窗外部关闭
    modal.addEventListener('click', (e) => {
        if (e.target === modal) {
            closeModal();
        }
    });

    // 表单验证
    function resetValidation() {
        const inputs = form.querySelectorAll('input');
        inputs.forEach(input => {
            input.classList.remove('is-invalid', 'is-valid');
        });
    }

    // 实时验证
    form.addEventListener('input', (e) => {
        const input = e.target;
        validateInput(input);

        // 密码强度检测
        if (input.id === 'password') {
            updatePasswordStrength(input.value);
        }

        // 确认密码验证
        if (input.id === 'confirmPassword' || input.id === 'password') {
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirmPassword').value;

            if (confirmPassword && password !== confirmPassword) {
                document.getElementById('confirmPassword').classList.add('is-invalid');
                document.getElementById('confirmPassword').classList.remove('is-valid');
            } else if (confirmPassword) {
                document.getElementById('confirmPassword').classList.add('is-valid');
                document.getElementById('confirmPassword').classList.remove('is-invalid');
            }
        }
    });

    function validateInput(input) {
        if (input.id === 'username') {
            const isValid = input.value.length >= 4 && input.value.length <= 20;
            updateValidationState(input, isValid);
        } else if (input.id === 'email') {
            const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
            const isValid = emailRegex.test(input.value);
            updateValidationState(input, isValid);
        } else if (input.id === 'password') {
            const isValid = input.value.length >= 8;
            updateValidationState(input, isValid);
        } else if (input.id === 'terms') {
            updateValidationState(input, input.checked);
        }
    }

    function updateValidationState(input, isValid) {
        if (input.value === '' && input.required) {
            input.classList.remove('is-invalid', 'is-valid');
            return;
        }

        if (isValid) {
            input.classList.add('is-valid');
            input.classList.remove('is-invalid');
        } else {
            input.classList.add('is-invalid');
            input.classList.remove('is-valid');
        }
    }

    // 密码强度检测
    function updatePasswordStrength(password) {
        const strengthBar = document.querySelector('.password-strength-bar');
        let strength = 0;

        // 长度检测
        if (password.length >= 8) strength += 20;
        if (password.length >= 12) strength += 20;

        // 复杂度检测
        if (/[A-Z]/.test(password)) strength += 20;
        if (/[0-9]/.test(password)) strength += 20;
        if (/[^A-Za-z0-9]/.test(password)) strength += 20;

        // 更新UI
        strengthBar.style.width = `${strength}%`;

        if (strength < 40) {
            strengthBar.style.backgroundColor = '#e74a3b'; // 红色
        } else if (strength < 80) {
            strengthBar.style.backgroundColor = '#f6c23e'; // 黄色
        } else {
            strengthBar.style.backgroundColor = '#1cc88a'; // 绿色
        }
    }

    // 提交表单
    submitBtn.addEventListener('click', () => {
        let isValid = true;
        const inputs = form.querySelectorAll('input[required]');

        inputs.forEach(input => {
            validateInput(input);
            if (input.classList.contains('is-invalid')) {
                isValid = false;
            }
        });

        // 检查密码匹配
        const password = document.getElementById('password').value;
        const confirmPassword = document.getElementById('confirmPassword').value;

        if (password !== confirmPassword) {
            document.getElementById('confirmPassword').classList.add('is-invalid');
            isValid = false;
        }

        if (isValid) {
            // 模拟提交
            submitBtn.innerHTML = '<span class="spinner-border spinner-border-sm me-1" role="status" aria-hidden="true"></span> 提交中...';
            submitBtn.disabled = true;

            setTimeout(() => {
                alert('注册成功！');
                closeModal();
                submitBtn.innerHTML = '<i class="bi bi-check-lg me-1"></i> 提交注册';
                submitBtn.disabled = false;
            }, 1500);
        } else {
            // 滚动到第一个错误
            const firstInvalid = form.querySelector('.is-invalid');
            if (firstInvalid) {
                firstInvalid.scrollIntoView({behavior: 'smooth', block: 'center'});
            }
        }
    });
</script>
</body>
</html>