// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../../../BuilderField/shared/ui/entity_details.dart';
import '../Custom_setting_viewModel/Custom_setting_view_model_screen.dart';
import 'Custom_setting_update_entity_screen.dart';

class Custom_settingDetailsScreen extends StatefulWidget {
  final Map<String, dynamic> entity;

  const Custom_settingDetailsScreen({
    super.key,
    required this.entity,
  });

  @override
  State<Custom_settingDetailsScreen> createState() => _Custom_settingDetailsScreenState();
}

class _Custom_settingDetailsScreenState extends State<Custom_settingDetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Custom_settingViewModelScreen(),
          child: Custom_settingUpdateEntityScreen(entity: entity),
        ),
      ),
    ).then((_) {
      // Refresh the details screen with updated data
      setState(() {});
    });
  }

  void _showDeleteDialog(Map<String, dynamic> entity) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Deletion'),
          content: const Text('Are you sure you want to delete this Custom_setting?'),
          actions: [
            TextButton(
              child: const Text('Cancel'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
            TextButton(
              child: const Text('Delete'),
              onPressed: () async {
                Navigator.of(context).pop();
                final vm =
                    Provider.of<Custom_settingViewModelScreen>(context, listen: false);
                final success = await vm.deleteEntity(entity['id']);
                if (success && mounted) {
                  Navigator.pop(context); // Go back to list
                }
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Custom_settingViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Custom_setting',
          displayFields: [
  {'key': 'name', 'label': 'Name', 'type': 'text'},

  {'key': 'description', 'label': 'Description', 'type': 'textarea'},

  {'key': 'active', 'label': 'Active', 'type': 'toggle_switch'},

  {'key': 'yound_driver_surcharge', 'label': 'yound driver surcharge', 'type': 'number'},

  {'key': 'free_km', 'label': 'free km', 'type': 'number'},

  {'key': 'extra_km_charge', 'label': 'Extra km charge', 'type': 'number'},

  {'key': 'gst_rate', 'label': 'GST rate', 'type': 'number'},

  {'key': 'pst_rate', 'label': 'PST rate', 'type': 'number'},

  {'key': 'age_threshold', 'label': 'Age Threshold', 'type': 'number'},

          ],
          isLoading: viewModel.isLoading,
        );
      },
    );
  }
}
