import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/router/index.dart';

class UpdatePatientInfo extends FStatefulWidget
    implements FInteractiveContainer {
  ///原有值
  final String? value;

  ///标题
  final String? title;

  final bool Function(String)? onSave;

  final List<TextInputFormatter>? inputFormatters;

  final InputBorder? border;

  final int? maxlength;

  final int? maxLines;

  UpdatePatientInfo({
    this.title,
    this.value,
    this.onSave,
    this.inputFormatters,
    this.maxlength = 20,
    this.border,
    this.maxLines,
  });

  @override
  String get pageName => 'UpdatePatientInfo';

  @override
  FState<FStatefulWidget> createState() {
    return _UpdatePatientInfoState();
  }
}

class _UpdatePatientInfoState extends FState<UpdatePatientInfo> {
  final _controller = TextEditingController();
  final focusNode = FocusNode();
  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: FAppBar(
        title: FText(
          widget.title ?? i18nBook.common.edit.t,
        ),
        centerTitle: true,
        leading: FIconButton(
          name: "back",
          businessParent: widget,
          icon: FIcon(Icons.arrow_back_ios_new),
          onPressed: () {
            router.back();
          },
        ),
        elevation: 0,
        actions: [
          FTextButton(
              name: "confirm",
              businessParent: widget,
              child: FText(
                i18nBook.common.submit.t,
                style: TextStyle(color: Colors.white, fontSize: 14),
              ),
              onPressed: () {
                var result = widget.onSave?.call(_controller.text) ?? false;
                if (result) {
                  router.back();
                }
              }),
        ],
      ),
      body: FContainer(
        margin: EdgeInsets.symmetric(vertical: 20, horizontal: 15),
        child: FTextField(
          maxLength: widget.maxlength,
          controller: _controller,
          maxLines: widget.maxLines,
          focusNode: focusNode,
          inputFormatters: widget.inputFormatters,
          decoration: InputDecoration(
            hintText: i18nBook.common.pleaseEnter.t,
            contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 3),
            hintStyle: TextStyle(color: Colors.grey),
            border: widget.border ??
                UnderlineInputBorder(
                  borderSide: BorderSide(color: Colors.grey),
                ),
            suffix: FIconButton(
              name: "close",
              businessParent: this.widget,
              icon: FIcon(
                Icons.close,
                size: 22,
              ),
              onPressed: () {
                setState(() {
                  _controller.text = '';
                });
              },
            ),
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    _controller.text = widget.value ?? '';
    super.initState();
  }
}
