import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:vitalapp/components/button.dart';
import 'package:vitalapp/components/cell.dart';
import 'package:vitalapp/pages/home/personal_center/controller.dart';

class PasswordPage extends GetView<PersonalCenterController> {
  const PasswordPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        VListFormCellGroup(
          children: [
            Obx(
              () {
                return VListFormCell(
                  label: "原密码",
                  contentWidget: Expanded(
                    child: _PasswordInput(
                      isPasswordVisible: false,
                      value: controller.state.newPassword,
                      onChanged: (value) =>
                          controller.state.oldPassword = value,
                    ),
                  ),
                );
              },
            ),
            Obx(
              () {
                return VListFormCell(
                  label: "新密码",
                  contentWidget: Expanded(
                    child: _PasswordInput(
                      isPasswordVisible: false,
                      value: controller.state.newPassword,
                      onChanged: (value) =>
                          controller.state.newPassword = value,
                    ),
                  ),
                );
              },
            ),
            Obx(
              () {
                return VListFormCell(
                  label: "确认密码",
                  contentWidget: Expanded(
                    child: _PasswordInput(
                      isPasswordVisible: false,
                      value: controller.state.newPassword,
                      onChanged: (value) =>
                          controller.state.confirmPassword = value,
                    ),
                  ),
                );
              },
            ),
          ],
        ),
        const SizedBox(
          height: 100,
        ),
        VButton(
          label: "设  置",
          onTap: controller.setPassword,
        ),
      ],
    );
  }
}

class _PasswordInput extends StatefulWidget {
  final String value;
  final ValueChanged<String> onChanged;
  final bool isPasswordVisible;

  const _PasswordInput({
    Key? key,
    required this.value,
    required this.onChanged,
    required this.isPasswordVisible,
  }) : super(key: key);

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

class _PasswordInputState extends State<_PasswordInput> {
  bool _isPasswordVisible = false;
  TextEditingController _textEditingController = TextEditingController();
  @override
  void initState() {
    _isPasswordVisible = widget.isPasswordVisible;
    _textEditingController = TextEditingController(text: widget.value);
    if (mounted) {
      setState(() {});
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: _textEditingController,
      obscureText: !_isPasswordVisible,
      onChanged: (value) {
        widget.onChanged(value);
      },
      style: TextStyle(
        color: Colors.grey.shade700,
        fontSize: 20,
        overflow: TextOverflow.ellipsis,
      ),
      decoration: const InputDecoration(
        // filled: true,
        hintText: "请输入密码",
        border: InputBorder.none,
        isCollapsed: true,
        // suffixIcon: GestureDetector(
        //   onTap: () {
        //     setState(() {
        //       _isPasswordVisible = !_isPasswordVisible;
        //     });
        //   },
        //   child: Icon(
        //     _isPasswordVisible ? Icons.visibility_off : Icons.visibility,
        //   ),
        // ),
      ),
    );
  }
}
