import 'package:fis_jsonrpc/services/contractRecord.m.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/managers/interfaces/contract.dart';

import 'state.dart';

class ContractRecordsController extends FControllerBase {
  final state = ContractRecordsState();
  final _contractManager = Get.find<IContractManager>();
  late String patientCode;
  @override
  void onReady() {
    super.onReady();
    _initData();
    _getContractRecordPageByPatientCodeAsync();
  }

  _initData() {
    update(["contract_records"]);
    final p = Get.parameters;
    patientCode = p['patientCode'] ?? "";
  }

  Future<void> _getContractRecordPageByPatientCodeAsync() async {
    var result = await _contractManager
        .getContractRecordPageByPatientCodeAsync(patientCode);
    state.contractRecordDTOList = result?.pageData ?? [];
  }

  String getServicePackNames(ContractRecordDTO dto) {
    return dto.servicePacks!.map((servicePack) => servicePack.name!).join(',');
  }

  Future<String?> getContractRecordDetailAsync(String patientCode) async {
    var result =
        await _contractManager.getContractRecordDetailAsync(patientCode);
    if (result != null) {
      return result.contractedFileUrl;
    } else {
      return "";
    }
  }
}
