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

import 'state.dart';

class PatientInfoController extends FControllerBase {
  final state = PatientInfoState();
  final _patientManager = Get.find<IPatientManager>();

  @override
  void onInit() {
    final p = Get.parameters;
    state.code = p["code"]!;

    super.onInit();
  }

  @override
  void onReady() {
    // TODO: implement onReady
    super.onReady();
    _loadData();
  }

  Future<void> _loadData() async {
    final dto = await _patientManager.getDetail(state.code);
    if (dto != null) {
      dto.birthday = dto.birthday!.toLocal();
      state.updateDto(dto);
    }
  }

  Future<bool> updatePatientAsync() async {
    UpdatePatientRequest request = UpdatePatientRequest(
      token: Store.user.token,
      code: state.code,
      patientName: state.name,
      phone: state.phoneNo,
      cardNo: state.cardNo,
      nationality: state.nation,
      birthday: state.birthday,
      cardType: state.cardType,
      patientGender: state.gender,
      patientAddress: state.address,
      permanentResidenceAddress: state.permanentResidenceAddress,
    );

    final result = await _patientManager.updatePatientAsync(request);
    return result;
  }

  /// 切换导航锚点
  void switchAnchor(NavAnchorType anchor) {
    state.anchor = anchor;
  }
}
