// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:base_project/BuilderField/shared/ui/entity_list.dart';
import 'Dv2_create_entity_screen.dart';
import 'Dv2_update_entity_screen.dart';
import '../Dv2_viewModel/Dv2_view_model_screen.dart';
import 'Dv2_details_screen.dart';import 'package:flutter/services.dart';
import 'package:speech_to_text/speech_to_text.dart' as stt;
import '../../../../theme/app_style.dart';
import '../../../../utils/size_utils.dart';
import '../../../../widgets/custom_icon_button.dart';
import '../../../../utils/image_constant.dart';
import '../../../../widgets/app_bar/appbar_image.dart';
import '../../../../widgets/app_bar/appbar_title.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import '../../../../theme/app_decoration.dart';
import 'package:multi_select_flutter/multi_select_flutter.dart';
import '../../../../Reuseable/reusable_text_field.dart';
import 'package:provider/provider.dart';
import '../../../../utils/toast_messages/toast_message_util.dart';
import '../../../../BuilderField/shared/fields/realted_entity_insert_field.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Dv2_entity_list_screen extends StatefulWidget {
  static const String routeName = '/entity-list';

  @override
  _Dv2_entity_list_screenState createState() => _Dv2_entity_list_screenState();
}

class _Dv2_entity_list_screenState extends State<Dv2_entity_list_screen> {
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  void _loadData() {
    WidgetsBinding.instance.addPostFrameCallback((_) {
      if (mounted) {
        final vm = Provider.of<Dv2ViewModelScreen>(context, listen: false);
        vm.getAllWithPagination(refresh: true);
      }
    });
  }

  void _navigateToCreateScreen() {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Dv2ViewModelScreen(),
          child: const Dv2CreateEntityScreen(),
        ),
      ),
    ).then((_) {
      final vm = Provider.of<Dv2ViewModelScreen>(context, listen: false);
      vm.refreshData();
    });
  }

  void _navigateToUpdateScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Dv2ViewModelScreen(),
          child: Dv2UpdateEntityScreen(entity: entity),
        ),
      ),
    ).then((_) {
      final vm = Provider.of<Dv2ViewModelScreen>(context, listen: false);
      vm.refreshData();
    });
  }

  void _navigateToDetailsScreen(Map<String, dynamic> entity) {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ChangeNotifierProvider(
          create: (context) => Dv2ViewModelScreen(),
          child: Dv2DetailsScreen(entity: entity),
        ),
      ),
    );
  }

  void _showDeleteDialog(Map<String, dynamic> entity) {
 final parentContext = context; 
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('Confirm Deletion'),
          content: const Text('Are you sure you want to delete this Dv2?'),
          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<Dv2ViewModelScreen>(parentContext, listen: false);
                await vm.deleteEntity(entity['id']);
              },
            ),
          ],
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<Dv2ViewModelScreen>(
      builder: (context, viewModel, child) {
        return EntityList(
          entities: viewModel.filteredList,
          isLoading: viewModel.isLoading,
          errorMessage:
              viewModel.errorMessage.isNotEmpty ? viewModel.errorMessage : null,
          hasMoreData: viewModel.hasMoreData,
          searchQuery: viewModel.searchQuery,
          onSearchChanged: (query) => viewModel.searchdv2(query),
          onEdit: (entity) => _navigateToUpdateScreen(entity),
          onDelete: (entity) => _showDeleteDialog(entity),
          onTap: (entity) => _navigateToDetailsScreen(entity),
          onRefresh: () => viewModel.refreshData(),
          onLoadMore: () => viewModel.getAllWithPagination(),
          title: 'Dv2',
          onAddNew: _navigateToCreateScreen,


















          displayFields: [

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

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

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



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


  {'key': 'currency', 'label': 'Currency', 'type': 'currency'},

  {'key': 'currency2', 'label': 'Currency2', 'type': 'currency'},

  {'key': 'qrcode_field', 'label': 'QRCode Field', 'type': 'qr_code'},

  {'key': 'qrcode_field2', 'label': 'QRCode Field2', 'type': 'qr_code'},

  {'key': 'barcode_field', 'label': 'BarCode Field', 'type': 'bar_code'},

  {'key': 'barcode_field2', 'label': 'BarCode Field2', 'type': 'bar_code'},

  {'key': 'static', 'label': 'Static', 'type': 'communication'},

          ],
        );
      },
    );
  }
}
