import 'package:app_business/service/abase.dart';
import 'package:app_business/service/api.dart';
import 'package:flutter/material.dart';
import 'package:track_common/service/map_watch.dart';
import 'package:track_common/view.dart';

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

  ApiService get api => Get.find();

  @override
  Widget build(BuildContext context) {
    return Center(
        child: SizedBox(
            width: 320,
            child: Card(
              margin: const EdgeInsets.symmetric(vertical: 24),
              child: ListView(
                children: [_SettingsElem(title: '退出登录', onTap: onSignOut)],
              ),
            )));
  }

  Widget divider() {
    return const Divider(color: Colors.grey);
  }

  Future<void> onSignOut() async {
    final mapWatchService = Get.find<MapWatchService>();
    mapWatchService.setMap(null);
    api.signOut();
    LoginView.to(
        canBack: false, thenToPageCall: () => Get.offAllNamed(HomeView.name));
  }
}

class _SettingsElem extends StatelessWidget {
  const _SettingsElem({required this.onTap, required this.title, this.tail});

  final VoidCallback onTap;
  final String title;
  final Widget? tail;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: onTap,
        child: Container(
          height: 60,
          color: Colors.transparent,
          padding: const EdgeInsets.symmetric(horizontal: 24),
          child: Row(
            children: [
              Text(title),
              const Spacer(),
              tail ?? const Icon(Icons.chevron_right)
            ],
          ),
        ));
  }
}
