import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:qhdkfq_regulatory_flutter/tools/my_appbar.dart';

class ChangePassPage extends StatefulWidget {
  const ChangePassPage({super.key});

  @override
  _ChangePassPageState createState() => _ChangePassPageState();
}

class _ChangePassPageState extends State<ChangePassPage>
    with SingleTickerProviderStateMixin {


  final _formKey = GlobalKey<FormState>();
  bool _obscureCurrentPassword = true;
  bool _obscureNewPassword = true;
  bool _obscureConfirmPassword = true;

  final TextEditingController _currentPasswordController = TextEditingController();
  final TextEditingController _newPasswordController = TextEditingController();
  final TextEditingController _confirmPasswordController = TextEditingController();

  @override
  void dispose() {
    _currentPasswordController.dispose();
    _newPasswordController.dispose();
    _confirmPasswordController.dispose();
    super.dispose();
  }

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      // 表单验证通过，处理密码修改逻辑
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(
          content: Text('密码修改成功！'),
          backgroundColor: Colors.green,
        ),
      );
      // 模拟处理延迟
      Future.delayed(const Duration(seconds: 1), () {
        Navigator.pop(context);
      });
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppbar(title: "修改密码"),
      body: Padding(
        padding: const EdgeInsets.all(10.0),

        child: SingleChildScrollView(
          padding: const EdgeInsets.all(10),
          child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
              const SizedBox(height: 10),

            // 表单区域
            Form(
              key: _formKey,
              child: Column(
                children: [
                  // 原密码
                  _buildPasswordField(
                    label: '原密码',
                    controller: _currentPasswordController,
                    obscureText: _obscureCurrentPassword,
                    onToggleVisibility: () {
                      setState(() {
                        _obscureCurrentPassword = !_obscureCurrentPassword;
                      });
                    },
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return '请输入原密码';
                      }
                      if (value.length < 6) {
                        return '密码长度至少为6位';
                      }
                      return null;
                    },
                  ),

                  const SizedBox(height: 35),


                  // 新密码
                  _buildPasswordField(
                    label: '新密码',
                    controller: _newPasswordController,
                    obscureText: _obscureNewPassword,
                    onToggleVisibility: () {
                      setState(() {
                        _obscureNewPassword = !_obscureNewPassword;
                      });
                    },
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return '请输入新密码';
                      }
                      if (value.length < 6) {
                        return '密码长度至少为6位';
                      }
                      if (!RegExp(r'[A-Z]').hasMatch(value)) {
                        return '需包含大写字母';
                      }
                      if (!RegExp(r'[0-9]').hasMatch(value)) {
                        return '需包含数字';
                      }
                      return null;
                    },
                  ),
                  const SizedBox(height: 35),

                  // 确认新密码
                  _buildPasswordField(
                    label: '确认新密码',
                    controller: _confirmPasswordController,
                    obscureText: _obscureConfirmPassword,
                    onToggleVisibility: () {
                      setState(() {
                        _obscureConfirmPassword = !_obscureConfirmPassword;
                      });
                    },
                    validator: (value) {
                      if (value == null || value.isEmpty) {
                        return '请确认新密码';
                      }
                      if (value != _newPasswordController.text) {
                        return '两次输入的密码不一致';
                      }
                      return null;
                    },
                  ),
                  const SizedBox(height: 40),

                  // 确定按钮
                  SizedBox(
                    width: double.infinity,
                    height: 54,
                    child: ElevatedButton(
                      onPressed: _submitForm,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: const Color(0xFF2196F3),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                        ),
                        elevation: 0,
                      ),
                      child: const Text(
                        '确定',
                        style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                            color: Colors.white),
                      ),
                    ),
                  ),
                ],
              ),
            ),
            ],
          ),
        ),


      ),
    );
  }

  Widget _buildPasswordField({
    required String label,
    required TextEditingController controller,
    required bool obscureText,
    required VoidCallback onToggleVisibility,
    required String? Function(String?)? validator,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 标签和星号
        Row(
          children: [
            const Text(
              '*',
              style: TextStyle(
                fontSize: 16,
                color: Colors.red,
              ),
            ),
            const SizedBox(width: 4),
            Text(
              label,
              style: const TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w500,
                  color: Color(0xFF333333)),
            ),
          ],
        ),
        // const SizedBox(height: 8),

        // 密码输入框
        TextFormField(
          controller: controller,
          obscureText: obscureText,
          validator: validator,
          decoration: InputDecoration(
            hintText: '请输入$label',
            hintStyle: const TextStyle(color: Color(0xFF999999)),
            contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 12),
            // suffixIcon: IconButton(
            //   icon: Icon(
            //     obscureText ? Icons.visibility_off : Icons.visibility,
            //     color: const Color(0xFF999999),
            //   ),
            //   onPressed: onToggleVisibility,
            // ),
          ),
        ),
      ],
    );
  }
}

