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:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:get/get.dart';

///关联检查界面
class AssociationExams extends FStatefulWidget {
  AssociationExams({Key? key})
      : super(
          key: key,
        );
  var selectIndex;

  @override
  FState<AssociationExams> createState() => _AssociationExamsState();
}

///关联检查界面状态
class _AssociationExamsState extends FState<AssociationExams> {
  final reportManager = Get.find<IReportManager>();
  final controller = Get.find<ConsultationController>();

  @override
  FWidget build(BuildContext context) {
    return FObx(
      () => FContainer(
        height: 400,
        child: controller.state.associatPatientInfos.length == 0
            ? _buildPatienAssociatedTitle()
            : _buildAssociatedExams(),
      ),
    );
  }

  FWidget _buildPatienAssociatedTitle() {
    return FCenter(
      child: FText(i18nBook.remedical.patientNameToAssociatePlaceholder.t),
    );
  }

  FWidget _buildAssociatedExams() {
    return FListView.builder(
      shrinkWrap: true,
      itemCount: controller.state.getRecordsPageResult.length,
      itemBuilder: (BuildContext context, int index) {
        return FContainer(
          margin: EdgeInsets.symmetric(
            vertical: 10,
            horizontal: 20,
          ),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            border: Border.all(
              color: Color.fromRGBO(235, 235, 235, 1),
              width: 1,
            ),
          ),
          child: FMaterial(
            color: Colors.white,
            borderRadius: BorderRadius.circular(8),
            child: FInkWell(
              onTap: () {
                setState(
                  () {
                    widget.selectIndex = index;
                    controller.state.rservationCode = controller
                        .state.getRecordsPageResult[index].recordCode!;
                  },
                );
              },
              child: _buildExamItem(index),
            ),
          ),
        );
      },
    );
  }

  FWidget _buildExamItem(int index) {
    return FContainer(
      margin: EdgeInsets.only(
        left: 10,
        bottom: 10,
      ),
      padding: EdgeInsets.all(10),
      child: FRow(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          //列表条目的选中效果区域
          FContainer(
            width: 20,
            height: 20,
            margin: EdgeInsets.only(
              right: 15,
            ),
            decoration: BoxDecoration(
              border: Border.all(
                color: Color.fromRGBO(141, 141, 141, 1),
                width: 0.5,
              ),
              borderRadius: BorderRadius.circular(20),
            ),
            child: _buildItemSelectedTemplate(index),
          ),
          _buildExamItemTemplate(index),
          _buildDeviceItemTemplate(index),
        ],
      ),
    );
  }

  FWidget _buildItemSelectedTemplate(int index) {
    return widget.selectIndex == index
        ? FContainer(
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
              color: Theme.of(context).primaryColor,
            ),
            child: FCenter(
              child: FContainer(
                child: FIcon(
                  Icons.check,
                  size: 15,
                  color: Colors.white,
                ),
              ),
            ),
          )
        : FContainer();
  }

  FWidget _buildExamItemTemplate(int index) {
    return FExpanded(
      child: FColumn(
        children: [
          FContainer(
            height: 10,
            width: 10,
          ),
          _RowItem(
            title: i18nBook.remedical.time.t,
            value: reportManager.getTime(
              controller.state.getRecordsPageResult[index].createTime
                  .toString(),
            ),
          ),
          FSizedBox(
            height: 20,
          ),
          _RowItem(
            title: i18nBook.common.fullName.t,
            value: controller.state.associatPatientInfos[0]['name'],
          ),
        ],
      ),
    );
  }

  FWidget _buildDeviceItemTemplate(int index) {
    return FExpanded(
      child: FColumn(
        children: [
          FContainer(
            height: 10,
          ),
          _RowItem(
            title: i18nBook.organization.department.t,
            titleWidth: 80,
            value: controller.state.getRecordsPageResult[index].deptName,
          ),
          FSizedBox(
            height: 20,
          ),
          _RowItem(
            title: i18nBook.remedical.deliveryDevice.t,
            titleWidth: 80,
            value: controller.state.getRecordsPageResult[index].displayName,
          ),
        ],
      ),
    );
  }
}

class _RowItem extends FisView<ConsultationController> {
  const _RowItem({
    Key? key,
    required this.title,
    this.titleWidth,
    this.value,
  }) : super(key: key);
  final String title;
  final double? titleWidth;
  final String? value;
  @override
  FWidget build(BuildContext context) {
    return FRow(
      children: [
        FContainer(
          width: titleWidth ?? 60,
          child: FText(
            title,
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        FExpanded(
          child: FTextTooltip(
            message: value ?? i18nBook.homePage.notYet.t,
            position: DisplayPosition.top,
            child: FContainer(
              child: FText(
                value ?? i18nBook.homePage.notYet.t,
                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
        ),
      ],
    );
  }
}

/// 单个可关联的检查
class AssociatCheck extends FisView<ConsultationController>
    implements FInteractiveContainer {
  final String? patientCode;
  final String? devicePatientCode;
  @override
  final String pageName;
  AssociatCheck(
    this.patientCode,
    this.devicePatientCode, {
    this.pageName = 'AssociatCheck',
    Key? key,
  }) : super(key: key);

  FWidget title(
    String name,
    String value,
    double textWidth,
  ) {
    return FRow(
      children: [
        FContainer(
          width: 40,
          child: FText(
            name,
            style: TextStyle(
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        FContainer(
          width: textWidth,
          child: FText(value),
        ),
      ],
    );
  }

  FWidget child() {
    return FContainer(
      child: FColumn(
        children: [
          FContainer(
            margin: EdgeInsets.symmetric(
              horizontal: 15,
              vertical: 8,
            ),
            color: Colors.white,
          ),
          AssociationExams(),
        ],
      ),
    );
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.selectCheck.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      children: [
        FContainer(
          width: 600,
          height: 500,
          child: child(),
        ),
      ],
      bottomButton: FContainer(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 15),
        child: FFlex(
          direction: Axis.horizontal,
          mainAxisSize: MainAxisSize.min,
          children: [
            FElevatedButton(
              name: "confirm:${controller.state.rservationCode}",
              businessParent: this,
              onPressed: () {
                controller.relevanceRecord(
                  controller.state.rservationCode,
                  devicePatientCode ?? '',
                );
              },
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(
                    vertical: 15,
                    horizontal: 25,
                  ),
                ),
              ),
              child: FText(i18nBook.common.confirm4Btn.t),
            ),
            FSizedBox(
              width: 20,
            ),
            FOutlinedButton(
              businessParent: this,
              name: "cancel",
              onPressed: () {
                controller.closeSetVaildPatient();
              },
              style: ButtonStyle(
                backgroundColor: MaterialStateProperty.all(
                  Colors.white,
                ),
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(vertical: 15, horizontal: 25),
                ),
              ),
              child: FText(
                i18nBook.common.cancel4Btn.t,
                style: TextStyle(
                  color: Color.fromRGBO(61, 146, 252, 1),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
