// home_screen.dart
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mypoint_flutter_app/preference/data_preference.dart';

import '../setting/setting_screen.dart';

class HomeScreen extends StatelessWidget {
  const HomeScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center, // ✅ căn giữa dọc
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            ElevatedButton(onPressed: () => _logout(context), child: const Text('Đăng xuất')),
            ElevatedButton(onPressed: () => _showSetting(context), child: const Text('Setting')),
          ],
        ),
      ),
    );
  }

  void _logout(BuildContext context) async {
    final confirm = await showDialog<bool>(
      context: context,
      builder:
          (ctx) => AlertDialog(
            title: const Text('Xác nhận'),
            content: const Text('Bạn có chắc muốn đăng xuất?'),
            actions: [
              TextButton(onPressed: () => Navigator.of(ctx).pop(false), child: const Text('Hủy')),
              TextButton(onPressed: () => Navigator.of(ctx).pop(true), child: const Text('Đăng xuất')),
            ],
          ),
    );

    if (confirm == true) {
      DataPreference.instance.clearLoginToken();
      Get.offAllNamed('/onboarding');
    }
  }

  void _showSetting(BuildContext context) async {
    Get.to(() => const SettingScreen());
  }
}
