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/router/router.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/pages/remedical/consultation_record_view/controller.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/controller.dart';
import 'package:flyinsono/pages/user/widgets/search_input.dart';
import 'package:fis_jsonrpc/services/index.dart';
import 'package:flyinsono/views/desktop/check_list_page_creation/widgets/select_patient_card.dart';
import 'package:get/get.dart';

// 检查列表搜索病人
class CheckListSelectPatient extends FisView<ConsultationRecordViewController>
    implements FPage {
  CheckListSelectPatient({
    required this.selectPatient,
  });
  final ValueCallback<PatientInfo> selectPatient;

  final ScrollController _scrollController = ScrollController();
  final checkListPageCreationController =
      Get.find<CheckListPageCreationController>();
  // static const C_BG_COLOR = Color.fromARGB(255, 121, 135, 151);
  static const C_CONTAINER_SIZE = 600.0;

  @override
  String get pageName => 'CheckListSelectPatient';

  Future<void> clickPatientCard(int index) async {
    final currentPatient =
        controller.checkController.selectPatientInfosList[index];
    selectPatient.call(currentPatient);
    router.back();
  }

  @override
  FWidget build(BuildContext context) {
    _initSelectPatientDialog();
    return FSimpleDialog(
      title: FText(
        controller.organizationPatientType == OrganizationPatientTypeEnum.Person
            ? i18nBook.realTimeConsultation.pleaseSelectPatient.t
            : i18nBook.realTimeConsultation.pleaseSelectPet.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        Get.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: false,
      isUseColumnLayout: true,
      children: [
        FExpanded(
          child: FContainer(
            width: 600,
            child: FContainer(
              padding: const EdgeInsets.only(bottom: 20),
              child: FColumn(
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  FContainer(
                    padding: const EdgeInsets.all(15),
                    child: FRow(
                      children: [
                        FExpanded(
                          child: SearchInput(
                            businessParent: this,
                            onChanged: (value) {
                              controller.checkController.patientName =
                                  value.toString().trim();
                            },
                            // controller: TextEditingController(
                            //   text: controller.checkController.patientName,
                            // ),
                            focusNode: controller.searchSelectPatientFocusNode,
                            serchOnPressed: () {
                              controller.checkController.searchPatient();
                            },
                          ),
                        ),
                        const FSizedBox(
                          width: 15,
                        ),
                        FElevatedButton(
                          onPressed: () {
                            controller.checkController.createPatient();
                          },
                          style: ButtonStyle(
                            padding: MaterialStateProperty.all(
                              const EdgeInsets.symmetric(
                                vertical: 15,
                                horizontal: 25,
                              ),
                            ),
                          ),
                          businessParent: this,
                          name: i18nBook.common.add.t,
                          child: FText(
                            i18nBook.common.add.t,
                          ),
                        ),
                      ],
                    ),
                  ),
                  FExpanded(
                    child: FisBuilder<ConsultationRecordViewController>(
                      id: 'patient_list',
                      builder: (_) {
                        return FAlwaysScrollListView(
                          scrollController: _scrollController,
                          child: QuickFWidget(ScrollConfiguration(
                            behavior: ScrollConfiguration.of(context)
                                .copyWith(scrollbars: false),
                            child: FListView.builder(
                              controller: _scrollController,
                              shrinkWrap: true,
                              itemCount: controller.checkController
                                  .selectPatientInfosList.length,
                              itemBuilder: (context, index) {
                                return InkWell(
                                  onTap: () {
                                    clickPatientCard(index);
                                  },
                                  child: Container(
                                    padding: const EdgeInsets.symmetric(
                                      horizontal: 15,
                                      vertical: 8,
                                    ),
                                    child: CheckListSelectPatientCard(
                                      patientInfos: controller.checkController
                                          .selectPatientInfosList[index],
                                      selectPatientCode:
                                          controller.currentSelectPatientCode,
                                    ),
                                  ),
                                );
                              },
                            ),
                          )),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }

  void _initSelectPatientDialog() {
    controller.currentSelectPatientCode =
        checkListPageCreationController.state.patientInfo.patientCode;

    controller.searchSelectPatientFocusNode.addListener(() {
      if (controller.searchSelectPatientFocusNode.hasFocus) {
        controller.isSelectPatientHasFocus = true;
      } else {
        Future.delayed(
          const Duration(milliseconds: 10),
          () {
            controller.isSelectPatientHasFocus = false;
          },
        );
      }
    });
  }
}
