import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vnoteapp/architecture/storage/text_storage.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/exam.dart';
import 'package:vnoteapp/pages/check/models/form.dart';

class ExaminationController extends GetxController {
  ExaminationController();
  final _examManager = Get.find<IExamManager>();
  late String patientCode;
  _initData() {
    update(["examination"]);
    final p = Get.parameters;
    patientCode = p['patientCode'] ?? "";
  }

  List<MenuItem> menuList = [
    MenuItem(label: "一般状况", value: 'ZZYBZK'),
    MenuItem(label: "辅助检查", value: 'FZJC'),
    MenuItem(label: "生活方式", value: 'SHFS'),
    MenuItem(label: "脏器及查体", value: 'ZQCT'),
    MenuItem(label: "现存主要健康问题", value: 'XCZYWT'),
    MenuItem(label: "住院及用药情况", value: 'ZYYYFMYGHYFJZS'),
    MenuItem(label: "健康评价及指导", value: 'JKPJJZD'),
  ];
  void onTap() {}

  /// TODO 保存检查需要先判断一下是否本地存在
  /// 本地存在拿本地的，本地不存在拿serve
  /// serve不存在创建
  Future<void> createExam(key, templateCode, data) async {
    TextStorage template = TextStorage(
      fileName: key,
      directory: "patient/$patientCode/exam/2023",
    );
    final result = await _examManager.createExam(CreateExamRequest(
      key: key,
      patientCode: patientCode,
      templateCode: templateCode,
      examData: data,
    ));
    if (result?.isNotEmpty ?? false) {
      try {
        template.save(data);
        PromptBox.toast('保存成功');
      } catch (err) {
        print('🥟');
        PromptBox.toast('保存失败');
      }
    }
  }

  // @override
  // void onInit() {
  //   super.onInit();
  // }

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

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
