import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:get/get.dart';
import 'package:loopin/IM/controller/im_user_info_controller.dart';

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

  @override
  State<Des> createState() => _DesState();
}

class _DesState extends State<Des> {
  final _formKey = GlobalKey<FormBuilderState>();
  final userInfoController = Get.find<ImUserInfoController>();

  void _save() async {
    if (_formKey.currentState?.saveAndValidate() ?? false) {
      final signature = _formKey.currentState?.fields['signature']?.value;
      final result = await userInfoController.updateSignature(signature);
      if (result) {
        Get.back();
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () => FocusScope.of(context).unfocus(),
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          title: const Text('修改简介'),
          actions: [
            TextButton(
              onPressed: _save,
              child: const Text(
                '保存',
                style: TextStyle(color: Colors.red, fontSize: 16),
              ),
            ),
          ],
        ),
        body: Padding(
          padding: const EdgeInsets.all(16),
          child: FormBuilder(
              key: _formKey,
              child: Obx(() {
                return Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    const Text(
                      '简介',
                      style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                    ),
                    const SizedBox(height: 8),
                    FormBuilderTextField(
                      name: 'signature',
                      initialValue: userInfoController.signature.value,
                      maxLines: 6, // 最多显示6行
                      minLines: 3, // 最少显示3行
                      decoration: const InputDecoration(
                        hintText: '请输入内容',
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.symmetric(horizontal: 12, vertical: 10),
                      ),
                      maxLength: 100,
                      validator: FormBuilderValidators.compose([
                        FormBuilderValidators.required(errorText: '内容不能为空'),
                        FormBuilderValidators.maxLength(100, errorText: '内容不能超过100个字符'),
                      ]),
                    ),
                    const SizedBox(height: 8),
                    const Text(
                      '最长支持100个字符，请文明用语',
                      style: TextStyle(fontSize: 12, color: Colors.grey),
                    ),
                  ],
                );
              })),
        ),
      ),
    );
  }
}
