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/index.dart';
import 'package:flyinsono/managers/interfaces/entities/patient_Info.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/controller.dart';
import 'package:flyinsono/views/desktop/appointment_consultation/widgets/select_patient_card.dart';
import 'package:get/get.dart';

/// 搜索病人
class FSelectPatient extends FStatefulWidget implements FPage {
  const FSelectPatient({
    Key? key,
    this.selectList,
  }) : super(key: key);
  final List<PatientInfo>? selectList;

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

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

class _SelectPatientState extends FState<FSelectPatient> {
  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;

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

  void clickPatientCard(int index) {
    if (widget.selectList![index].phone?.isNotEmpty ?? true) {
      controller.state.isAddPatient = false;
    } else {
      controller.state.isAddPatient = true;
    }
    final currentPatient = widget.selectList![index];
    controller.state.currentPatient = currentPatient;
    controller.patient['Phone'] = currentPatient.phone ?? '';
    controller.patient['Sex'] = currentPatient.sex ?? '';
    controller.patient['Anamnesis'] = currentPatient.anamnesis ?? '';
    controller.patient['Name'] = currentPatient.name ?? '';
    controller.patient['IdentityCard'] = currentPatient.identityCard ?? '';
    router.back();
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.realTimeConsultation.pleaseSelectPatient.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      onClose: () {
        Get.back();
      },
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: false,
      children: [
        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,
                        focusNode: controller.keyWordSearchFocusNode,
                        borderColor: const Color.fromRGBO(234, 234, 234, 1),
                        suffixIcon: FMaterial(
                          color: Colors.transparent,
                          child: FIconButton(
                            onPressed: () {
                              controller.clickSelectPatient();
                            },
                            icon: const FIcon(
                              Icons.search,
                            ),
                            businessParent: widget,
                            name: i18nBook
                                .realTimeConsultation.pleaseSelectPatient.t,
                          ),
                        ),
                        height: 38,
                        onChanged: (value) {
                          controller.state.patientName =
                              value.toString().trim();
                        },
                      ),
                      const FSizedBox(
                        width: 15,
                      ),
                      FElevatedButton(
                        onPressed: () {
                          controller.createPatient();
                        },
                        style: ButtonStyle(
                          padding: MaterialStateProperty.all(
                            const EdgeInsets.symmetric(
                              vertical: 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) {
                          return InkWell(
                            onTap: () {
                              clickPatientCard(index);
                            },
                            child: Container(
                              padding: const EdgeInsets.symmetric(
                                horizontal: 15,
                                vertical: 8,
                              ),
                              child: FPatientCard(
                                patientInfos: widget.selectList?[index],
                              ),
                            ),
                          );
                        },
                      ),
                    )),
                  ),
                ),
              ],
            ),
          ),
        ),
      ],
    );
  }
}
