import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';
import 'package:flyinsono/pages/remedical/consultation/state.dart';
import 'package:flyinsono/rpc.dart';
import 'package:fis_i18n/i18n.dart';

/// 转诊病人的控制器
class ReferPatientInfoController extends FControllerBase {
  late ConsultationController controller;
  late FConsultationState state;
  ReferPatientInfoController(ConsultationController controller) {
    this.controller = controller;
    this.state = controller.state;
  }

  /// 转诊病人列表
  Future<PageResult<QueryReferralRecordPageDTO>> queryReferralRecordListAsync(
      bool? isDropUp) async {
    try {
      final patients = await rpc.remedical.queryReferralRecordListAsync(
        QueryReferralRecordListRequest(
          token: Store.user.token,
          pageIndex: isDropUp! ? 1 : state.patientPageInt,
          pageSize: 10,
          keyWord: state.keyWord,
          startTime: state.startDateTime,
          endTime: state.endDateTime,
          isValid: state.isValid == 0
              ? PatientValidStatusEnum.All
              : state.isValid == 1
                  ? PatientValidStatusEnum.CheckOut
                  : PatientValidStatusEnum.CheckIn,
        ),
      );
      return patients;
    } catch (e) {
      return PageResult<QueryReferralRecordPageDTO>();
    }
  }

  ///病人详情
  Future<ClientPatientInfoDTO> queryReferralPatientByCodeAsync(
      [String? patientCode]) async {
    try {
      if (state.referralRecordCode != '') {
        final patients = await rpc.remedical.queryReferralPatientByCodeAsync(
          QueryReferralPatientByCodeRequest(
            token: Store.user.token,
            code: patientCode != null ? patientCode : state.patientCode,
            referralRecordCode: state.referralRecordCode,
          ),
        );
        return patients;
      } else {
        return ClientPatientInfoDTO();
      }
    } catch (e) {
      return ClientPatientInfoDTO();
    }
  }

  /// 转诊的所有扫查图片
  Future<PageResult<RemedicalListResult>>
      queryReferralRemedicalListPagesAsync() async {
    try {
      if (state.referralRecordCode != '') {
        final remedicalListResult =
            await rpc.remedical.queryReferralRemedicalListPagesAsync(
          QueryReferralRecordsPageRequest(
            token: Store.user.token,
            patientCode: state.patientCode,
            pageIndex: 1,
            pageSize: 10,
            referralRecordCode: state.referralRecordCode,
          ),
        );
        return remedicalListResult;
      } else {
        return PageResult<RemedicalListResult>();
      }
    } catch (e) {
      return PageResult<RemedicalListResult>();
    }
  }

  /// 分页查询转诊病人检查列表
  Future<PageResult<GetRecordsPageDTO>> queryReferralRecordInfoPagesAsync(
    String patientCode,
    int? pageIndex,
    QueryRecordStatusEnum? queryRecordStatus,
  ) async {
    try {
      if (state.referralRecordCode != '') {
        final getRecordsPage =
            await rpc.remedical.queryReferralRecordInfoPagesAsync(
          QueryReferralRecordsPageRequest(
            token: Store.user.token,
            patientCode: patientCode,
            pageIndex: pageIndex ?? 1,
            queryRecordStatus: queryRecordStatus ?? QueryRecordStatusEnum.All,
            pageSize: 60,
            referralRecordCode: state.referralRecordCode,
          ),
        );
        return getRecordsPage;
      } else
        return PageResult<GetRecordsPageDTO>();
    } catch (e) {
      return PageResult<GetRecordsPageDTO>();
    }
  }

  /// 查询转诊检查关联报告列表
  Future<List<ReportDTO>> queryReferralExamReportsAsync(
      String recordCode) async {
    final getRecordsPage = await rpc.remedical.queryReferralExamReportsAsync(
      QueryReferralExamReportRequest(
        token: Store.user.token,
        // recordCode: recordCode,
        languageCode: i18nBook.locale.toCodeString('-'),
        referralRecordCode: state.referralRecordCode,
      ),
    );
    return getRecordsPage;
  }

  /// 查询转诊病人关联报告
  Future<List<ReportDTO>> queryReferralPatientReportsAsync() async {
    try {
      if (state.referralRecordCode != '') {
        final getRecordsPage =
            await rpc.remedical.queryReferralPatientReportsAsync(
          QueryReferralPatientByCodeRequest(
            token: Store.user.token,
            code: state.patientCode,
            referralRecordCode: state.referralRecordCode,
          ),
        );
        return getRecordsPage;
      } else {
        return [];
      }
    } catch (e) {
      return [];
    }
  }
}
