import 'package:dart_json_mapper/dart_json_mapper.dart';
import 'package:get/get.dart';
import 'package:naiyouwl/app/common/SharedPreferencesUtil.dart';
import 'package:naiyouwl/app/common/constants.dart';

import '../../../data/model/SysConfig.dart';
import '../../../network/api_service.dart';
import '../../../routes/app_pages.dart';

class WelcomeController extends GetxController {
  var isLoading = true.obs;
  var sysConfig = SysConfig().obs;
  var errorMsg = ''.obs;

  Future<void> fetchSysConfig() async {
    try {
      isLoading.value = true;
      sysConfig.value = await ApiService().fetchSysConfig(kSysConfig);
      await SharedPreferencesUtil().setString("sysconfig", JsonMapper.serialize(sysConfig.value));

      var token = await SharedPreferencesUtil().getString("token");
      if(token != null){
        Get.offNamed(Routes.HOME);
      }else {
        Get.offNamed(Routes.LOGIN);
      }


    } catch (e) {
      errorMsg.value = e.toString();
    } finally {
      isLoading.value = false;
    }
  }
  @override
  void onInit() {
    super.onInit();
    fetchSysConfig();
  }

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

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


}
