import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/controller.dart';
import 'package:get/get.dart';

/// 文本输入框
class CheckListPageInput extends FStatefulWidget {
  CheckListPageInput({
    Key? key,
    required this.configuration,
    required this.buildKey,
    required this.inputFormatter,
  }) : super(key: key);
  final Map<String, dynamic> configuration;
  final String buildKey;
  final List<TextInputFormatter> inputFormatter;

  @override
  FState<CheckListPageInput> createState() => _CheckListPageInputState();
}

class _CheckListPageInputState extends FState<CheckListPageInput> {
  final controller = Get.find<CheckListPageCreationController>();
  String? initInputData() {
    var textName = controller.getFormula(
      widget.configuration,
      widget.buildKey,
    );
    if (textName != '') {
      Future.delayed(Duration(milliseconds: 100), () {
        controller.state.contents[
            widget.buildKey + '-' + widget.configuration['name']] = textName;
      });
    }

    return textName;
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    controller.state
        .contents[widget.buildKey + '-' + widget.configuration['name']] = '';
  }

  @override
  FWidget build(BuildContext context) {
    return widget.configuration['value']['Formula'] != null
        ? FObx(() {
            return FBorderInput(
              contentSize: 16,
              maxLength: 20,
              width: 300,
              readOnly: true,
              borderColor: Color.fromRGBO(230, 230, 230, 1),
              height: 38,
              controller: TextEditingController(text: initInputData() ?? ''),
            );
          })
        : FBorderInput(
            contentSize: 16,
            maxLength: 20,
            width: 300,
            borderColor: Color.fromRGBO(230, 230, 230, 1),
            height: 38,
            inputFormatters: widget.inputFormatter,
            onChanged: (value) {
              controller.setInputValue(
                value ?? '',
                widget.configuration,
                widget.buildKey,
              );
            });
  }
}
