import 'package:get/get.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/account.dart';
import 'package:vnoteapp/managers/interfaces/doctor.dart';
import 'package:vnoteapp/store/store.dart';
import 'package:fis_common/logger/logger.dart';
import 'state.dart';

class LoginController extends FControllerBase {
  final state = LoginState();

  void onSubmit() async {
    try {
      final result = await Get.find<IAccountManager>()
          .login(state.account, state.password);
      if (result) {
        await Get.find<IDoctorManager>().syncInfo();
        PromptBox.toast("登录成功");
        Get.offAllNamed("/");
        return;
      }
    } catch (e) {
      logger.e("LoginController login error", e);
    }
    PromptBox.error("登录失败");
  }

  @override
  void onReady() {
    if (Store.user.account != null) {
      state.account = Store.user.account!;
    }
    super.onReady();
  }

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

  void _doDispose() {}
}
