import 'package:app/providers/user_store.dart';
import 'package:app/router/route_paths.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({super.key});

  @override
  State<SplashPage> createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    initPermission();
  }

  ///权限效验初始化
  void initPermission() async {
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      String token = await UserStore.getToken();
      if (mounted) {
        // 未登录
        if (token.isEmpty) {
          context.go(RoutePaths.login);
        } else {
          UserStore userStore = context.read<UserStore>();
          await userStore.init();
          //去学生主页
          if (userStore.userInfo?.accountType == 1) {
            context.go(RoutePaths.sHome);
          } else if(userStore.userInfo?.accountType == 2){
            context.go(RoutePaths.tHome);
          }else{
            EasyLoading.showError("无法找到首页");
          }
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold();
  }
}
