import 'package:fis_jsonrpc/services/contractRecord.m.dart';
import 'package:get/get.dart';
import 'package:vitalapp/architecture/defines.dart';
import 'package:vitalapp/architecture/utils/prompt_box.dart';
import 'package:vitalapp/managers/interfaces/contract.dart';
import 'package:vitalapp/store/store.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"]);
    patientCode = Store.user.currentSelectPatientInfo?.code ?? '';
    // 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 "";
    }
  }

  Future<void> removeContractRecordAsync(String code) async {
    var result = await _contractManager.removeContractRecordAsync(code);
    if (result) {
      PromptBox.toast('删除成功');
    } else {
      PromptBox.toast('删除失败');
    }
  }
}
