// 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 '../Basicp3_viewModel/Basicp3_view_model_screen.dart';
import 'Basicp3_update_entity_screen.dart';

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

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

  @override
  State<Basicp3DetailsScreen> createState() => _Basicp3DetailsScreenState();
}

class _Basicp3DetailsScreenState extends State<Basicp3DetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Basicp3ViewModelScreen(),
          child: Basicp3UpdateEntityScreen(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 Basicp3?'),
          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<Basicp3ViewModelScreen>(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<Basicp3ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Basicp3',
          displayFields: [
  {'key': 'toggle_switch', 'label': 'Toggle Switch', 'type': 'toggle_switch'},

  {'key': 'toggle_switch2', 'label': 'Toggle Switch2', 'type': 'toggle_switch'},

  {'key': 'url_field', 'label': 'Url Field', 'type': 'url'},

  {'key': 'url_field2', 'label': 'Url Field2', 'type': 'url'},

  {'key': 'decimal_field', 'label': 'Decimal Field', 'type': 'decimal'},

  {'key': 'decimal_field2', 'label': 'Decimal Field2', 'type': 'decimal'},

  {'key': 'percentage_field', 'label': 'Percentage Field', 'type': 'number'},

  {'key': 'percentage_field2', 'label': 'Percentage Field2', 'type': 'number'},

  {'key': 'documentsequenc', 'label': 'documentsequenc', 'type': 'document_sequence'},

  {'key': 'recaptcha', 'label': 'recaptcha', 'type': 'recaptcha'},

  {'key': 'recaptcha2', 'label': 'recaptcha2', 'type': 'recaptcha'},

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