import 'package:flutter/material.dart';
import 'package:get/get.dart';

class LoginController extends GetxController {
  final usernameController = TextEditingController();
  final passwordController = TextEditingController();
  final usernameFocusNode = FocusNode();
  final passwordFocusNode = FocusNode();
  final _isLogin = false.obs;
  bool get isLogin => _isLogin.value;
  set isLogin(bool value) {
    _isLogin.value = value;
  }

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

  @override
  void onClose() {
    usernameController.dispose();
    passwordController.dispose();
    usernameFocusNode.dispose();
    passwordFocusNode.dispose();
    super.onClose();
  }

  void checkLogin() {
    final username = usernameController.text;
    final password = passwordController.text;
    if (validatorInputValue(username) && validatorInputValue(password)) {
      isLogin = true;
    } else {
      isLogin = false;
    }
  }

  //6-20 characters (letters or numbers)
  bool validatorInputValue(String value) {
    // 只允许字母和数字并且是6-20位
    return value.length >= 6 &&
        value.length <= 20 &&
        RegExp(r'^[a-zA-Z0-9]+$').hasMatch(value);
  }
}
