import 'dart:convert';

import 'package:fis_jsonrpc/services/diagnosis.m.dart';
import 'package:fis_jsonrpc/services/dictionary.m.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/consts/diagnosis.dart';
import 'package:vitalapp/global.dart';
import 'package:vitalapp/managers/interfaces/diagnosis.dart';
import 'package:vitalapp/managers/interfaces/dictionary.dart';
import 'package:vitalapp/managers/interfaces/models/diagnosis_aggregation_record_model.dart';
import 'package:vitalapp/pages/medical/records/state.dart';

class MedicalRecordsController extends FControllerBase {
  final state = MedicalRecordsState();
  final _diagnosisManager = Get.find<IDiagnosisManager>();
  final _dictionaryManager = Get.find<IDictionaryManager>();

  late String patientCode;
  @override
  void onReady() {
    super.onReady();
    final p = Get.parameters;
    patientCode = p['patientCode'] ?? "";
    _loadData();
  }

  _loadData() async {
    try {
      state.pageIndex = 0;
      state.dataList = [];
      var listRecord =
          await _diagnosisManager.getListByPatientCode(patientCode);
      if (listRecord != null) {
        state.appendDataList(listRecord);
      }
      await loadNextPageList();
    } catch (e) {}
  }

  Future<void> loadNextPageList() async {
    var result = await _diagnosisManager.getDiagnosisAggregationPageAsync(
        patientCode, state.pageIndex + 1, state.pageSize);
    if (result != null) {
      state.totalCount = result.dataCount;
      if (result.pageData != null) {
        state.pageIndex = state.pageIndex + 1;
        state.appendDataList(result.pageData!);
      }
    }
  }

  Future<List<List<String>>> getTableData(
      DiagnosisAggregationRecordModel dto) async {
    var currentDiagnosis = await _diagnosisManager.getTableData(dto);
    return currentDiagnosis;
  }
}
