import 'package:get/get.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/doctor.dart';
import 'package:vnoteapp/store/store.dart';

import 'state.dart';

class DoctorSignatureSettingController extends GetxController {
  final state = DoctorSignatureSettingState();

  @override
  void onReady() {
    _loadData();
    super.onReady();
  }

  /// 重新签名
  Future<void> rewrite() async {
    final result = await Get.toNamed("/contract/signature");
    if (result != null) {
      final saved = await Get.find<IDoctorManager>().setSignature(result);
      if (saved) {
        _updateSignature(result);
      } else {
        PromptBox.toast("设置签名失败");
      }
    }
  }

  void _loadData() {
    _updateSignature(Store.user.signature);
  }

  void _updateSignature(String value) {
    if (value.startsWith('data:image')) {
      state.signaturBase64 = value.split(',')[1];
    }
  }
}
