import 'dart:math';

import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.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/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/edit_patient_dialog/controller.dart';
import 'package:get/get.dart';

///增加或编辑病人对话框
class AddOrEditPatientDiaLog extends FisView<AddoreditPatientDialogController>
    implements FPage {
  ///增加或编辑病人组件
  final FWidget addOrEditPatient;
  @override
  final String pageName;

  ///增加或编辑病人图片组件
  final FWidget addOrEditPatientAvatar;

  ///是否自动创建检查的标志
  final FWidget autoCreateDiagnosisFlag;
  const AddOrEditPatientDiaLog({
    Key? key,
    required this.addOrEditPatient,
    required this.addOrEditPatientAvatar,
    required this.autoCreateDiagnosisFlag,
    this.pageName = 'AddOrEditPatientDiaLog',
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: _builDialogTitle(),
      children: [
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 950 : 1200,
          child: addOrEditPatient,
        ),
        FContainer(
          /// TODO:[Gavin] 多语言-UI-整合
          width: i18nBook.isCurrentChinese ? 950 : 1200,
          child: addOrEditPatientAvatar,
        ),
        FContainer(
          width: i18nBook.isCurrentChinese ? 950 : 1200,
          child: (!_isEditMode() && _isPatientMode())
              ? autoCreateDiagnosisFlag
              : FContainer(),
        ),
      ],
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      onClose: () {
        controller.cancelPatient();
      },
      bottomButton: FContainer(
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(vertical: 15),
        child: _buildOperationButton(),
      ),
    );
  }

  FWidget _buildOperationButton() {
    return FFlex(
      direction: Axis.horizontal,
      mainAxisSize: MainAxisSize.min,
      children: [
        FElevatedButton(
          name: "savePatientInfo",
          businessParent: this,
          onPressed: () async {
            await controller.savePatient();
            var consultation = Get.find<ConsultationController>();
            consultation.changePatientInfo(controller.patientCode);
          },
          style: ButtonStyle(
            padding: MaterialStateProperty.all(
              EdgeInsets.symmetric(
                vertical: 15,
                horizontal: 25,
              ),
            ),
          ),
          child: FText(i18nBook.common.save4Btn.t),
        ),
        FSizedBox(
          width: 20,
        ),
        FOutlinedButton(
          name: "cancelPatient",
          businessParent: this,
          onPressed: () {
            controller.cancelPatient();
          },
          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),
            ),
          ),
        ),
      ],
    );
  }

  FWidget _builDialogTitle() {
    return FText(
      _isPatientMode()
          ? _isEditMode()
              ? i18nBook.remedical.editPatient.t
              : i18nBook.remedical.createPatient.t
          : _isEditMode()
              ? i18nBook.remedical.editCase.t
              : i18nBook.remedical.createCase.t,
      style: TextStyle(
        color: Colors.white,
        fontSize: 18,
      ),
    );
  }

  bool _isPatientMode() {
    final consultationController = Get.find<ConsultationController>();
    return consultationController.patientInfoController.patientType ==
        OrganizationPatientTypeEnum.Person;
  }

  bool _isEditMode() {
    final consultationController = Get.find<ConsultationController>();
    return controller.state.isEditStatus;
  }
}
