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

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

  void onSubmit() async {
    setBusy('登录中');
    // busy = true;
    try {
      final result = await Get.find<IAccountManager>()
          .login(state.account, state.password);
      if (result) {
        await Get.find<IDoctorManager>().syncInfo();
        // PromptBox.toast("登录成功");
        busy = false;
        Get.offAllNamed("/");
        return;
      }
    } catch (e) {
      logger.e("LoginController login error", e);
    } finally {
      // busy = false;
    }
    // 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() {}
}
