import 'package:fis_common/index.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_common/event/event_type.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_common/helpers/encrypt.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/quality_control.dart';
import 'package:flyinsono/pages/remedical/report_preview/entitys/select_qualityType.dart';
import 'package:get/get.dart';

class ConsultationQualityControl extends FStatefulWidget {
  final String consultationRecordCode;
  final String reportCode;
  final QualityType qualityType;
  final void Function(QualityType)? onSelectedValue;

  ConsultationQualityControl({
    required this.consultationRecordCode,
    required this.reportCode,
    this.onSelectedValue,
    this.qualityType = QualityType.None,
  });

  @override
  FState<FStatefulWidget> createState() {
    return QualityControlState();
  }
}

class QualityControlState extends FState<ConsultationQualityControl> {
  final qualityControlManager = Get.find<IQualityControlManager>();

  final List<FSelectConsultationQualityType> consultationSource = [
    FSelectConsultationQualityType(
        name: i18nBook.common.qualityPass.t,
        qualityType: QualityType.Qualified),
    FSelectConsultationQualityType(
        name: i18nBook.common.infoIncomplete.t,
        qualityType: QualityType.InformationUnCompleted),
    FSelectConsultationQualityType(
        name: i18nBook.common.imageNotStandard.t,
        qualityType: QualityType.ImageNotClear),
    FSelectConsultationQualityType(
        name: i18nBook.common.lesionNotClear.t,
        qualityType: QualityType.PositiveSiteNotClear),
  ];
  String? selectedValue;

  @override
  void initState() {
    if (widget.qualityType != QualityType.None) {
      selectedValue = widget.qualityType.name;
    }
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      okString: i18nBook.common.submit.t,
      cancelString: i18nBook.common.cancel.t,
      isDefault: true,
      title: FText(
        i18nBook.common.reportQualityControl.t,
        style: TextStyle(color: Colors.white),
      ),
      onCancel: router.back,
      onOk: () {
        if (selectedValue == null) {
          return;
        }
        var type = QualityType.values
            .firstWhere((element) => element.name == selectedValue);
        var qualityTypeKey = type.index;
        var name = consultationSource
            .firstWhere((element) => element.qualityType == type)
            .name;
        widget.onSelectedValue?.call(type);

        if (widget.qualityType != QualityType.None) {
          qualityControlManager.modifyConsultationReportQualityAsync(
            consultationRecordCode: widget.consultationRecordCode,
            reportCode: widget.reportCode,
            qualityTypeKey: qualityTypeKey,
          );
        } else {
          qualityControlManager.addConsultationReportQuality(
            consultationRecordCode: widget.consultationRecordCode,
            reportCode: widget.reportCode,
            qualityTypeKey: qualityTypeKey,
          );
        }
        router.back();
      },
      children: [
        FContainer(
          width: 280,
          height: 40,
          margin: EdgeInsets.symmetric(vertical: 50, horizontal: 10),
          child: FSelect<FSelectConsultationQualityType, String>(
            source: consultationSource,
            hintText: i18nBook.common.Select.t,
            onSelectChanged: (v, i) {
              selectedValue = v;
            },
            optionValueExtractor: (data) {
              return data.qualityType.name;
            },
            value: selectedValue,
            optionLabelExtractor: (data) {
              return data.name;
            },
          ),
        ),
      ],
    );
  }
}
