// ignore_for_file: use_build_context_synchronously
import 'dart:convert';
import 'package:provider/provider.dart';
import '../Adv1_viewModel/Adv1_view_model_screen.dart';
import '../../../../utils/image_constant.dart';
import '../../../../utils/size_utils.dart';
import '../../../../theme/app_style.dart';
import '../../../../widgets/app_bar/appbar_image.dart';
import '../../../../widgets/app_bar/appbar_title.dart';
import '../../../../widgets/app_bar/custom_app_bar.dart';
import 'package:barcode_widget/barcode_widget.dart';
import 'package:fluttertoast/fluttertoast.dart';
import '../../../../widgets/custom_button.dart';
import '../../../../widgets/custom_text_form_field.dart';
import 'package:flutter/material.dart';
import 'package:autocomplete_textfield/autocomplete_textfield.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:intl/intl.dart';

import 'dart:math';
import '../../../../Reuseable/reusable_text_field.dart';
import '../../../../Reuseable/reusable_date_picker_field.dart';
import '../../../../Reuseable/reusable_date_time_picker_field.dart';
import '../../../../Reuseable/reusable_dropdown_field.dart';
import 'package:flutter/services.dart';
class adv1UpdateEntityScreen extends StatefulWidget {
    final Map<String, dynamic> entity;


  adv1UpdateEntityScreen({required this.entity});

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

class _adv1UpdateEntityScreenState extends State<adv1UpdateEntityScreen> {
  final _formKey = GlobalKey<FormState>();

  String? selectedismale;
 

  Future<void> _showismaleSelectionDialog(BuildContext context) async {
    final result = await showDialog<String>(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: const Text('Select ismale'),
          children: [
            
 
            RadioListTile<String>(
              title: const Text('no'),
              value: 'no',
              groupValue: selectedismale,
              onChanged: (value) {
                setState(() {
                  selectedismale = value;
                  Navigator.pop(context, value);
                });
              },
            ),
        

 
            RadioListTile<String>(
              title: const Text('yes'),
              value: 'yes',
              groupValue: selectedismale,
              onChanged: (value) {
                setState(() {
                  selectedismale = value;
                  Navigator.pop(context, value);
                });
              },
            ),
        


          ],
        );
      },
    );

    if (result != null) {
      setState(() {
        selectedismale = result;
      });
    }
  }

  String? selectedidfemale;
 

  Future<void> _showidfemaleSelectionDialog(BuildContext context) async {
    final result = await showDialog<String>(
      context: context,
      builder: (BuildContext context) {
        return SimpleDialog(
          title: const Text('Select idfemale'),
          children: [
            
 
            RadioListTile<String>(
              title: const Text('no'),
              value: 'no',
              groupValue: selectedidfemale,
              onChanged: (value) {
                setState(() {
                  selectedidfemale = value;
                  Navigator.pop(context, value);
                });
              },
            ),
        

 
            RadioListTile<String>(
              title: const Text('yes'),
              value: 'yes',
              groupValue: selectedidfemale,
              onChanged: (value) {
                setState(() {
                  selectedidfemale = value;
                  Navigator.pop(context, value);
                });
              },
            ),
        


          ],
        );
      },
    );

    if (result != null) {
      setState(() {
        selectedidfemale = result;
      });
    }
  }


 

  bool test1cBox = false;
 



 

  bool t1cBox = false;
 



















  @override
  void initState() {
    super.initState();
    final provider = Provider.of<Adv1ViewModelScreen>(context, listen: false);
selectedismale = widget.entity['ismale'] ?? ''; // Initialize selected ismale 

selectedidfemale = widget.entity['idfemale'] ?? ''; // Initialize selected idfemale 



 
test1cBox = widget.entity['test1'] ?? false; 
 




 
t1cBox = widget.entity['t1'] ?? false; 
 


















}

 
  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<Adv1ViewModelScreen>(context, listen: false);
    return Scaffold(
      appBar: CustomAppBar(
          height: getVerticalSize(49),
          leadingWidth: 40,
          leading: AppbarImage(
              height: getSize(24),
              width: getSize(24),
              svgPath: ImageConstant.imgArrowleftBlueGray900,
              margin: getMargin(left: 16, top: 12, bottom: 13),
              onTap: () {
                Navigator.pop(context);
              }),
          centerTitle: true,
          title: AppbarTitle(text: "Update Adv1"),        actions: [
























        ],
),
      body:  SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Form(
            key: _formKey,
            child: Column(
              children: [
 Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "ismale",
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                color: Colors.blueGrey[900],
              ),
            ),
            const SizedBox(height: 6),
            GestureDetector(
              onTap: () => _showismaleSelectionDialog(context),
              child: AbsorbPointer(
                child: TextFormField(
                  decoration: InputDecoration(
                    hintText: "Enter ismale",
                    border: OutlineInputBorder(),
                    contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
                  ),
                  controller: TextEditingController(text: selectedismale),
                  onSaved: (value) {
                    widget.entity['ismale'] = value;
                  },
                ),
              ),
            ),
            const SizedBox(height: 16),
          
          ],
        ),
      ),

Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "idfemale",
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.left,
              style: TextStyle(
                fontSize: 16,
                fontWeight: FontWeight.w500,
                color: Colors.blueGrey[900],
              ),
            ),
            const SizedBox(height: 6),
            GestureDetector(
              onTap: () => _showidfemaleSelectionDialog(context),
              child: AbsorbPointer(
                child: TextFormField(
                  decoration: InputDecoration(
                    hintText: "Enter idfemale",
                    border: OutlineInputBorder(),
                    contentPadding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 12.0),
                  ),
                  controller: TextEditingController(text: selectedidfemale),
                  onSaved: (value) {
                    widget.entity['idfemale'] = value;
                  },
                ),
              ),
            ),
            const SizedBox(height: 16),
          
          ],
        ),
      ),

                Row(
                  children: [


 
                    Checkbox(
                      value: test1cBox,
                      onChanged: (newValue) {
                        setState(() {
                          test1cBox = newValue!;
                        });
                      },
                    ),
                    const SizedBox(width: 8),
                    Text('Test1'),

 

                  ],
                ),
                SizedBox(height: 16),

                Row(
                  children: [


 
                    Checkbox(
                      value: t1cBox,
                      onChanged: (newValue) {
                        setState(() {
                          t1cBox = newValue!;
                        });
                      },
                    ),
                    const SizedBox(width: 8),
                    Text('T1'),

 

                  ],
                ),
                SizedBox(height: 16),










                        ReusableTextField(
                           
                            label: "Please Enter audio Field",
                            initialValue: widget.entity['audio_field'] ?? '',

   // ValidationProperties
                            onSaved: (value) => widget.entity['audio_field'] = value,
),
                    
                          SizedBox(height: 16),



                        ReusableTextField(
                           
                            label: "Please Enter audio Field2",
                            initialValue: widget.entity['audio_field2'] ?? '',

   // ValidationProperties
                            onSaved: (value) => widget.entity['audio_field2'] = value,
),
                    
                          SizedBox(height: 16),



                        ReusableTextField(
                           
                            label: "Please Enter video Field",
                            initialValue: widget.entity['video_field'] ?? '',

   // ValidationProperties
                            onSaved: (value) => widget.entity['video_field'] = value,
),
                    
                          SizedBox(height: 16),



                        ReusableTextField(
                           
                            label: "Please Enter video Field2",
                            initialValue: widget.entity['video_field2'] ?? '',

   // ValidationProperties
                            onSaved: (value) => widget.entity['video_field2'] = value,
),
                    
                          SizedBox(height: 16),


 
          CustomButton(
                  height: getVerticalSize(50),
                  text: "Update",
                  margin: getMargin(top: 24, bottom: 5),
                  onTap: () async {
                    if (_formKey.currentState!.validate()) {
                      _formKey.currentState!.save();






     

                  widget.entity['test1'] = test1cBox;
 




     

                  widget.entity['t1'] = t1cBox;
 


















       
                        try {
                        await provider.updateEntity(
                              widget.entity[
                                  'id'], // Assuming 'id' is the key in your entity map
                              widget.entity);   

























                        Navigator.pop(context);
                      } catch (e) {
                        // ignore: use_build_context_synchronously
                        showDialog(
                          context: context,
                          builder: (BuildContext context) {
                            return AlertDialog(
                              title: const Text('Error'),
                              content:
                              Text('Failed to update Adv1: $e'),
                              actions: [
                                TextButton(
                                  child: const Text('OK'),
                                  onPressed: () {
                                    Navigator.of(context).pop();
 },
                                  ),
                                ],
                              );
                            },
                          );
                        }
                      }
                    },
                  ),
                ],
              ),
            ),
          ),
       ),
       );
  }
}