import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/utils/prompt_box.dart';
import 'package:fis_theme/theme.dart';
import 'package:get/get.dart';

/// TODO BAKA 需要封装成组件 搜索检查部位
class FMultiSelectDialog extends FStatefulWidget implements FPage {
  FMultiSelectDialog({
    Key? key,
    this.selectList,
    this.appointmentScanningPositionList,
    required this.onOKSelect,
  }) : super(key: key);
  final List<FSelectModel>? selectList;
  final ValueCallback onOKSelect;
  List<String>? appointmentScanningPositionList;

  @override
  FState<FMultiSelectDialog> createState() => _SelectPatientState();

  @override
  String get pageName => 'FMultiSelectDialog';
}

class _SelectPatientState extends FState<FMultiSelectDialog> {
  // final controller = Get.find<AppointmentConsultationController>();
  final ScrollController _scrollController = ScrollController();

  // static const C_BG_COLOR = Color.fromARGB(255, 121, 135, 151);
  static const C_CONTAINER_SIZE = 600.0;
  // late final appointmentInformation = controller.state.appointmentInformation;

  List<String> _selectedList = [];
  // late List<String> _initialValue = [];
  List<FSelectModel> _selectList = [];
  String _currentValue = '';
  late TextEditingController inputValue =
      TextEditingController(text: _currentValue);

  @override
  void initState() {
    // if (widget.appointmentScanningPositionList != null) {
    //   _initialValue = List.from(widget.appointmentScanningPositionList!);
    // }
    _selectList = widget.selectList ?? [];
    if (widget.appointmentScanningPositionList != null) {
      _selectedList = List.from(widget.appointmentScanningPositionList!);

      for (String code in widget.appointmentScanningPositionList!) {
        bool codeExists = false;

        for (FSelectModel obj in _selectList) {
          if (obj.code == code) {
            codeExists = true;
            break;
          }
        }

        if (!codeExists) {
          _selectList.insert(0, FSelectModel(code: code, name: code));
        }
      }
    }
    setState(() {});

    super.initState();
  }

  void addSelectList(String value) {
    if (value.trim().isEmpty) {
      PromptBox.toast(i18nBook.realTimeConsultation.checkPartNotEmpty.t);
      return;
    }
    for (FSelectModel i in _selectList) {
      if (i.name == value) {
        PromptBox.toast(i18nBook.realTimeConsultation.validateUniquePart.t);
        return;
      }
    }

    _selectList.insert(0, FSelectModel(name: value, code: value));
    _selectedList.add(_currentValue);
    _currentValue = '';
    inputValue = TextEditingController(text: _currentValue);
  }

  @override
  FWidget build(BuildContext context) {
    FWidget _childSelect = FContainer(
      width: 600,
      height: 600,
      child: FContainer(
        // padding: const EdgeInsets.only(bottom: 20),
        child: FColumn(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            FContainer(
              padding: const EdgeInsets.all(15),
              child: FRow(
                children: [
                  FBorderInput(
                    hintSize: 16,
                    contentSize: 16,
                    maxLength: 20,
                    borderColor: const Color.fromRGBO(234, 234, 234, 1),
                    height: 38,
                    controller: inputValue,
                    onChanged: (value) {
                      _currentValue = value;
                    },
                  ),
                  const FSizedBox(
                    width: 15,
                  ),
                  FElevatedButton(
                    onPressed: () {
                      addSelectList(_currentValue);

                      setState(() {});
                    },
                    style: ButtonStyle(
                      padding: MaterialStateProperty.all(
                        EdgeInsets.symmetric(
                          vertical: kIsMobile ? 10 : 15,
                          horizontal: 25,
                        ),
                      ),
                    ),
                    businessParent: widget,
                    name: i18nBook.common.add.t,
                    child: FText(
                      i18nBook.common.add.t,
                    ),
                  ),
                ],
              ),
            ),
            FExpanded(
              child: FAlwaysScrollListView(
                scrollController: _scrollController,
                child: QuickFWidget(ScrollConfiguration(
                  behavior: ScrollConfiguration.of(context)
                      .copyWith(scrollbars: false),
                  child: FListView.builder(
                    controller: _scrollController,
                    shrinkWrap: true,
                    itemCount: widget.selectList?.length,
                    itemBuilder: (context, index) {
                      final code = widget.selectList?[index].code ?? '';
                      final name = widget.selectList?[index].name ?? '';
                      return InkWell(
                        onTap: () {
                          // clickPatientCard(index);
                          if (!_selectedList.contains(code)) {
                            _selectedList.add(code);
                          } else {
                            _selectedList.remove(code);
                          }
                          setState(() {});
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 15,
                            vertical: 8,
                          ),
                          child: FRow(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                              FText(name),
                              // if (referOrgList.any(
                              //     (e) => e.code == orgModelList[index].code))
                              if (_selectedList.contains(code))
                                FIcon(
                                  Icons.done_rounded,
                                  size: 20,
                                  color: FTheme.ins.colorScheme.primary,
                                )
                            ],
                          ),
                        ),
                      );
                    },
                  ),
                )),
              ),
            ),
          ],
        ),
      ),
    );
    if (kIsMobile) {
      return FScaffold(
        appBar: FAppBar(
          title: FText(
            i18nBook.realTimeConsultation.pleaseSelectInspectionSite.t,
            style: TextStyle(color: Colors.white),
          ),
          centerTitle: true,
          backgroundColor: FTheme.ins.colorScheme.primary,
          elevation: 0,
          actionsIconTheme:
              IconThemeData(color: FTheme.ins.colorScheme.primary),
          actions: [],
        ),
        body: _childSelect,
        bottomNavigationBar: FBottomSubmitButton(
          businessParent: this.widget,
          title: i18nBook.common.confirm.t,
          onPressed: () {
            widget.onOKSelect(_selectedList);
            Get.back();
          },
        ),
      );
    }
    return FSimpleDialog(
      title: FText(
        i18nBook.realTimeConsultation.pleaseSelectInspectionSite.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onClose: () {
        Get.back();
      },
      onCancel: () {
        Get.back();
      },
      onOk: () {
        widget.onOKSelect(_selectedList);
        Get.back();
      },
      isDefault: true,
      children: [_childSelect],
    );
  }
}
