import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import './pages/login_page.dart'; // 导入登录页面
import './pages/main_tab.dart'; // 导入主页（带TabBar的页面）
import 'package:intl/date_symbol_data_local.dart';

void main() async {
  // 确保Flutter引擎初始化完成
  WidgetsFlutterBinding.ensureInitialized();
  await initializeDateFormatting('zh_CN', null);

  // 获取共享首选项实例
  final prefs = await SharedPreferences.getInstance();

  // 获取登录状态
  final isLoggedIn = prefs.getBool('isLoggedIn') ?? false;

  runApp(MyApp(isLoggedIn: isLoggedIn));
}

class MyApp extends StatelessWidget {
  final bool isLoggedIn;

  const MyApp({super.key, required this.isLoggedIn});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '',
      builder: (context, child) {
        return GestureDetector(
          behavior: HitTestBehavior.translucent, // 让空白区域也能点击
          onTap: () {
            // 收起键盘
            FocusScope.of(context).unfocus();
          },
          child: child,
        );
      },
      theme: ThemeData(
        dividerTheme: const DividerThemeData(
          color: Color(0xF1F1F1FF),
          thickness: 1,      // 线高
          indent: 0,         // 左缩进
          endIndent: 0,      // 右缩进
        ),
        primarySwatch: Colors.blue,
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        inputDecorationTheme: const InputDecorationTheme(
          border: InputBorder.none,
          contentPadding: EdgeInsets.symmetric(horizontal: 8),
        ),
      ),
      // 根据登录状态决定初始页面
      home: isLoggedIn ? const MainPage() : const LoginPage(),
      debugShowCheckedModeBanner: false,
    );
  }
}