// 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 '../Testbug_viewModel/Testbug_view_model_screen.dart';
import 'Testbug_update_entity_screen.dart';

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

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

  @override
  State<TestbugDetailsScreen> createState() => _TestbugDetailsScreenState();
}

class _TestbugDetailsScreenState extends State<TestbugDetailsScreen> {
  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => TestbugViewModelScreen(),
          child: TestbugUpdateEntityScreen(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 Testbug?'),
          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<TestbugViewModelScreen>(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<TestbugViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityDetails(
          entity: widget.entity,
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          title: 'Testbug',
          displayFields: [
  {'key': 'name', 'label': 'Name', 'type': 'text'},




  
 {'key': 'bar_code', 'label': 'bar_code', 'type': 'field_group'},
 

  
 {'key': 'qr_code', 'label': 'qr_code', 'type': 'field_group'},
 




  {'key': 'value_list_fieldcc', 'label': 'Value List Fieldcc', 'type': 'value_list'},



  {'key': 'select_dyanamic', 'label': 'select dyanamic', 'type': 'select'},

  {'key': 'select_dynamic_mul', 'label': 'select dynamic mul', 'type': 'select'},

  {'key': 'select_autocompidentifier', 'label': 'select autocompidentifier', 'type': 'select'},

  {'key': 'select_autcomp_mul', 'label': 'select autcomp mul', 'type': 'select'},

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