import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';

import '../../Notifiers/ordersProvider/tpcAgentsProvider.dart';
import '../../Utils/app_colors.dart';
import '../../Utils/commonWidgets.dart';
import '../../Utils/dropdownTheme.dart';

class AddtpcagentScreen extends StatefulWidget {
  const AddtpcagentScreen({super.key});

  @override
  State<AddtpcagentScreen> createState() => _AddtpcagentScreenState();
}

class _AddtpcagentScreenState extends State<AddtpcagentScreen> {
  Dropdowntheme ddtheme = Dropdowntheme();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      var provider = Provider.of<Tpcagentsprovider>(
        context,
        listen: false,
      );

    });
  }


  @override
  Widget build(BuildContext context) {
    return Consumer<Tpcagentsprovider>(
      builder: (context, provider, child) {
        return WillPopScope(
          child: Scaffold(
            resizeToAvoidBottomInset: true,
            appBar: appbar2(context, "Add TPC Agent", provider.resetAll,
              SizedBox(width: 0,),),
            backgroundColor: AppColors.scaffold_bg_color,
            body: Container(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    Container(
                      decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.circular(16),
                      ),
                      margin: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                      padding: EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisSize: MainAxisSize.min,
                        children: [


                          textControllerWidget(context,provider.tpcNameController,"Enter Name",provider.onChangetpcName,TextInputType.text,false,null),
                          if(provider.tpcNameError!=null)...[
                            errorWidget(context, provider.tpcNameError)
                          ],

                          textControllerWidget(context,provider.tpcMobileNumberController,"Enter Mobile Number",provider.onChangetpcMobileNumber,TextInputType.number,false,FilteringTextInputFormatter.digitsOnly),
                          if(provider.tpcMobileNumberError!=null)...[
                            errorWidget(context, provider.tpcMobileNumberError)
                          ],

                          InkResponse(
                            onTap: () {
                              _showAttachmentSheet(context);
                            },
                            child: Container(
                              margin: EdgeInsets.symmetric(vertical: 10),
                              height: 45,
                              width: MediaQuery.of(context).size.width,
                              decoration: BoxDecoration(
                                color: Color(0xFFE6F6FF),
                                borderRadius: BorderRadius.circular(12),
                                border: Border.all(
                                  color: AppColors.app_blue,
                                  width: 0.5,
                                ),
                              ),
                              child: Center(
                                child: Text(
                                  "ID Proof",
                                  style: TextStyle(
                                    fontFamily: "JakartaMedium",
                                    color: AppColors.app_blue,
                                  ),
                                ),
                              ),
                            ),
                          ),
                          if(provider.imagePicked==1 && provider.imagePath!=null)...[
                            Padding(
                              padding: const EdgeInsets.symmetric(vertical: 4.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  Text("${provider.imagePath}",style: TextStyle(
                                      color: AppColors.semi_black,
                                      fontSize: 11,
                                      fontWeight: FontWeight.w600
                                  ),),
                                  InkResponse(
                                      onTap: () {
                                        provider.imagePicked = 0;
                                        provider.imagePath = null;
                                        provider.imageFilePath = null;
                                      },
                                      child: SvgPicture.asset("assets/svg/ic_close.svg",width: 15,height: 15,))
                                ],
                              ),
                            )
                          ],

                          textControllerWidget(context,provider.tpcBankNameController,"Enter Bank Name",provider.onChangetpcBankName,TextInputType.text,false,null),
                          if(provider.tpcBankNameError!=null)...[
                            errorWidget(context, provider.tpcBankNameError)
                          ],


                          textControllerWidget(context,provider.tpcBankBeneficiaryNameController,"Enter Bank Beneficiary Name",provider.onChangetpcBankBeneficiaryName,TextInputType.text,false,null),
                          if(provider.tpcBankBeneficiaryNameError!=null)...[
                            errorWidget(context, provider.tpcBankBeneficiaryNameError)
                          ],

                          textControllerWidget(context,provider.tpcBankAccountNumberController,"Enter Bank Account Number",provider.onChangetpcBankAccountNumber,TextInputType.number,false,FilteringTextInputFormatter.digitsOnly),
                          if(provider.tpcBankAccountNumberError!=null)...[
                            errorWidget(context, provider.tpcBankAccountNumberError)
                          ],

                          textControllerWidget(context,provider.tpcBankIFSCController,"Enter Bank IFSC",provider.onChangetpcBankIFSC,TextInputType.text,false,null),
                          if(provider.tpcBankIFSCError!=null)...[
                            errorWidget(context, provider.tpcBankIFSCError)
                          ],

                          textControllerWidget(context,provider.tpcNoteController,"Enter Description",provider.onChangetpcNote,TextInputType.text,false,null),
                          if(provider.tpcNoteError!=null)...[
                            errorWidget(context, provider.tpcNoteError)
                          ],
                          InkWell(
                            onTap: () {
                              provider.addTPCAgentAPIFunction(context);
                            },

                            child: Container(
                              alignment: Alignment.center,
                              height: 45,
                              margin: EdgeInsets.only(
                                left: 5.0,
                                right: 5.0,
                                top: 5.0,
                                bottom: 5.0,
                              ),
                              decoration: BoxDecoration(
                                color: AppColors.app_blue, //1487C9
                                borderRadius: BorderRadius.circular(14.0),
                              ),
                              child: Center(
                                child: Text(
                                  "Submit",
                                  textAlign: TextAlign.center,
                                  style: TextStyle(color: Colors.white),
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),

                  
                  ],
                ),
              ),
            ),

          ),

          onWillPop: () {

            return onBackPressed(context);
          },
        );
      },
    );
  }

 

  Future<void> _showAttachmentSheet(BuildContext context) {
    return showModalBottomSheet(
      useSafeArea: true,
      isDismissible: true,
      isScrollControlled: true,
      showDragHandle: true,
      backgroundColor: Colors.white,
      enableDrag: true,
      context: context,
      builder: (context) {
        return StatefulBuilder(
          builder: (context, setState) {
            return SafeArea(
              child: Consumer<Tpcagentsprovider>(

                builder: (context,tcpProvider, child) {
                  return Padding(

                    padding: EdgeInsets.only(
                      bottom:
                      MediaQuery
                          .of(
                        context,
                      )
                          .viewInsets
                          .bottom, // This handles keyboard
                    ),
                    child: Container(
                      margin: EdgeInsets.only(
                        bottom: 15,
                        left: 15,
                        right: 15,
                        top: 10,
                      ),

                      child: SingleChildScrollView(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Align(
                              alignment: Alignment.center,
                              child: Text(
                                "Select Source",
                                style: TextStyle(
                                    color: AppColors.app_blue,
                                    fontSize: 16
                                ),
                              ),
                            ),
                            SizedBox(height: 15,),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                tcpProvider.imgFromGallery(context);
                              },
                              child: Container(
                                height: 35,
                                child: Text("Select photo from gallery"),
                              ),
                            ),
                            SizedBox(height: 10),
                            InkWell(
                              onTap: () {
                                Navigator.of(context).pop(false);
                                tcpProvider.imgFromCamera(
                                    context);
                              },
                              child: Container(
                                height: 35,
                                child: Text("Capture photo from camera"),
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}
